Skip to content

Checking your reading list…

Retry

Stay curious. Build better.

Appearance

Choose how BestHub looks on this device.

Changes apply immediately.

Search articles

Open Discover with these filters

Suggested searches

Integrating Spring Boot with XXL-Job for Distributed Task Scheduling

This article explains how to integrate Spring Boot with the open‑source XXL‑Job distributed task scheduler, covering XXL‑Job fundamentals, configuration of the admin console and executor, Maven dependencies, property settings, code examples, @XxlJob annotation parameters, best practices, and includes additional promotional material.

In modern applications, scheduled tasks are indispensable, and the combination of Spring Boot and XXL‑Job offers a powerful toolset for simplifying task scheduling and management.

XXL‑Job is an open‑source distributed task scheduling platform built on Java, providing features such as distributed execution, elastic scaling, a centralized scheduling console, multiple task types (cron, Bean, Shell, HTTP, GLUE), task dependencies, failure retries, alerting, logging, and active community support.

The article compares XXL‑Job with other solutions like Quartz, Spring’s @Scheduled, Elastic‑Job, Akka Scheduler, and Cron4J, highlighting XXL‑Job’s advantages in visual management, distributed support, and alert mechanisms.

Integration steps: Clone the XXL‑Job source from Gitee or GitHub, execute the provided /xxl-job/doc/db/tables_xxl_job.sql script, and modify the admin console configuration in

/xxl-job/xxl-job-admin/src/main/resources/application.properties

. Example configuration (port, datasource, access token) is shown in a code block.

server.port=9998
spring.datasource.url=jdbc:mysql://127.0.0.1:3361/xxl_job?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&serverTimezone=Asia/Shanghai
spring.datasource.username=root
spring.datasource.password=123456
xxl.job.accessToken=eyJhbGciOiJIUzI1NiJ9

After configuring the admin console, access it at http://127.0.0.1:9998/xxl-job-admin with the default credentials.

Executor configuration: Add the Maven dependency com.xuxueli:xxl-job-core:2.4.0 to your project, set the executor properties (admin address, app name, IP, port, token, log path, retention days) in application.properties, and create a Spring bean to initialize XxlJobSpringExecutor. The Java configuration class is provided:

package com.todoitbo.baseSpringbootDasmart.config;

import com.xxl.job.core.executor.impl.XxlJobSpringExecutor;
import org.springframework.beans.factory.annotation.Value;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@Slf4j
public class XxlJobConfig {
    @Value("${xxl.job.admin.addresses}")
    private String adminAddresses;
    @Value("${xxl.job.executor.appname}")
    private String appname;
    @Value("${xxl.job.executor.ip}")
    private String ip;
    @Value("${xxl.job.executor.port}")
    private int port;
    @Value("${xxl.job.accesstoken}")
    private String accessToken;
    @Value("${xxl.job.executor.logpath}")
    private String logPath;
    @Value("${xxl.job.executor.logretentiondays}")
    private int logRetentionDays;

    @Bean
    public XxlJobSpringExecutor xxlJobExecutor() {
        log.info(">>>>>>>>>>> xxl-job config init.");
        XxlJobSpringExecutor executor = new XxlJobSpringExecutor();
        executor.setAdminAddresses(adminAddresses);
        executor.setAppname(appname);
        executor.setIp(ip);
        executor.setPort(port);
        executor.setAccessToken(accessToken);
        executor.setLogPath(logPath);
        executor.setLogRetentionDays(logRetentionDays);
        return executor;
    }
}

A sample job handler is also shown, using the @XxlJob("demoJobHandler") annotation to define a task:

package com.todoitbo.baseSpringbootDasmart.handler;

import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import org.springframework.stereotype.Component;

@Component
public class JobHandler {
    @XxlJob("demoJobHandler")
    public void demoJobHandler() throws Exception {
        XxlJobHelper.log("XXL-JOB, Hello World.");
    }
}

The article explains the three basic parameters of the @XxlJob annotation— value (task name), init (initialization method), and destroy (cleanup method)—and how they are used.

Best practices: detailed logging, parameter validation, idempotent task design, failure handling strategies, task dependencies, dynamic scheduling, monitoring and alert integration, version control, unit testing, clustering, security of the admin console, and proper documentation and training.

Finally, the article contains promotional sections offering ChatGPT services, a paid community, and various marketing links, which are unrelated to the technical tutorial.

About this article

For questions about attribution, corrections or content rights, please contactadmin@besthub.devand we will review it promptly.

distributed systemsJavaBackend DevelopmentTask SchedulingSpring Boot

Discussion 0

Questions, practical examples and thoughtful responses.

Open full discussion