Springboot project integration xxl-job

XXL-JOB is a lightweight distributed task scheduling platform whose core design goal is to develop quickly, learn simply, lightweight, and be easy to expand.The source code is now open and connected to several company online product lines, ready to use out of the box.

Download the github source first: https://github.com/xuxueli/xxl-job

In this paper, the 2.2.0-SNAPSHOT development test is used, if the corresponding version needs to be downloaded: https://github.com/itwsj/xxl-job

Code directory structure:

Execute the database script in the db folder under the doc directory in the mysql database to generate some base tables for the xxl.

Modify the application.properties file under xxl-job-admin, change the database link to the url corresponding to your database, and most importantly, specify the service port and the name of the context.


Then configure mysql links and corresponding drivers so that the enclosed areas need to be added or time zones and driver link errors will be reported


You can then start the project, or you can package the project into a jar file.

Visit Background Administration Page

Enter address: localhost:8080/job username: admin password 123456


In your own springboot project, integrate xxl-job:
Add the following configuration to the configuration file:


(1) Introducing xxl-job dependency in pom.xml

 <!-- xxl-job-core -->

(2) Configure information in application.yml/application.properties

1. Configure Background Address
2. The appname value is very important, and the configuration executor needs to be kept this constant


# web port

# log config

### xxl-job admin address list, such as "http://address" or "http://address01,http://address02"

### xxl-job executor address

### xxl-job, access token

### xxl-job log path
### xxl-job log retention days

Write the configuration class:

package com.xxl.job.executor.core.config;

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

 * xxl-job config
public class XxlJobConfig {
    private Logger logger = LoggerFactory.getLogger(XxlJobConfig.class);

    private String adminAddresses;

    private String appName;

    private String ip;

    private int port;

    private String accessToken;

    private String logPath;

    private int logRetentionDays;

    public XxlJobSpringExecutor xxlJobExecutor() {
        logger.info(">>>>>>>>>>> xxl-job config init.");
        XxlJobSpringExecutor xxlJobSpringExecutor = new XxlJobSpringExecutor();

        return xxlJobSpringExecutor;

     * For multi-network card, container deployment, etc., the registration IP can be flexibly customized by using the InetUtils component provided by "spring-cloud-commons";
     *      1,Introduce dependencies:
     *          <dependency>
     *             <groupId>org.springframework.cloud</groupId>
     *             <artifactId>spring-cloud-commons</artifactId>
     *             <version>${version}</version>
     *         </dependency>
     *      2,Configuration file, or container startup variable
     *          spring.cloud.inetutils.preferred-networks: 'xxx.xxx.xxx.'
     *      3,Get IP
     *          String ip_ = inetUtils.findFirstNonLoopbackHostInfo().getIpAddress();


Write your own timed tasks

package com.xxl.job.executor.service.jobhandler;

import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.IJobHandler;
import com.xxl.job.core.handler.annotation.XxlJob;
import com.xxl.job.core.log.XxlJobLogger;
import com.xxl.job.core.util.ShardingUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.concurrent.TimeUnit;

 * XxlJob Development Samples (Bean Mode)
 * Development steps:
 * 1,In the Spring Bean instance, develop the Job method in the form of "public ReturnT < String > execute (String param)"
 * 2,Add the comment'@XxlJob(value='custom jobhandler name', init =', JobHandler initialization method', destroy =', JobHandler destroy method')' to the Job method, and the comment value corresponds to the value of the JobHandler property of the new task created by the Dispatch Center.
 * 3,Execution log: Execution log needs to be printed through "XxlJobLogger.log";
 * @author xuxueli 2019-12-11 21:52:51
public class SampleXxlJob {
    private static Logger logger = LoggerFactory.getLogger(SampleXxlJob.class);

     * 1,Simple Task Example (Bean Mode)
    public ReturnT<String> demoJobHandler(String param) throws Exception {
        XxlJobLogger.log("XXL-JOB, Hello World.");

        for (int i = 0; i < 5; i++) {
            XxlJobLogger.log("beat at:" + i);
        return ReturnT.SUCCESS;

     * 2,Fragmented broadcast tasks
    public ReturnT<String> shardingJobHandler(String param) throws Exception {

        // Slicing parameters
        ShardingUtil.ShardingVO shardingVO = ShardingUtil.getShardingVo();
        XxlJobLogger.log("Slice parameter: current slice number = {}, Total number of fragments = {}", shardingVO.getIndex(), shardingVO.getTotal());

        // Business logic
        for (int i = 0; i < shardingVO.getTotal(); i++) {
            if (i == shardingVO.getIndex()) {
                XxlJobLogger.log("No. {} slice, Hit fragment to start processing", i);
            } else {
                XxlJobLogger.log("No. {} slice, ignore", i);

        return ReturnT.SUCCESS;

     * 3,Command Line Tasks
    public ReturnT<String> commandJobHandler(String param) throws Exception {
        String command = param;
        int exitValue = -1;

        BufferedReader bufferedReader = null;
        try {
            // command process
            Process process = Runtime.getRuntime().exec(command);
            BufferedInputStream bufferedInputStream = new BufferedInputStream(process.getInputStream());
            bufferedReader = new BufferedReader(new InputStreamReader(bufferedInputStream));

            // command log
            String line;
            while ((line = bufferedReader.readLine()) != null) {

            // command exit
            exitValue = process.exitValue();
        } catch (Exception e) {
        } finally {
            if (bufferedReader != null) {

        if (exitValue == 0) {
            return IJobHandler.SUCCESS;
        } else {
            return new ReturnT<String>(IJobHandler.FAIL.getCode(), "command exit value("+exitValue+") is failed");

     * 4,Cross-platform Http tasks
    public ReturnT<String> httpJobHandler(String param) throws Exception {

        // request
        HttpURLConnection connection = null;
        BufferedReader bufferedReader = null;
        try {
            // connection
            URL realUrl = new URL(param);
            connection = (HttpURLConnection) realUrl.openConnection();

            // connection setting
            connection.setReadTimeout(5 * 1000);
            connection.setConnectTimeout(3 * 1000);
            connection.setRequestProperty("connection", "Keep-Alive");
            connection.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
            connection.setRequestProperty("Accept-Charset", "application/json;charset=UTF-8");

            // do connection

            //Map<String, List<String>> map = connection.getHeaderFields();

            // valid StatusCode
            int statusCode = connection.getResponseCode();
            if (statusCode != 200) {
                throw new RuntimeException("Http Request StatusCode(" + statusCode + ") Invalid.");

            // result
            bufferedReader = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
            StringBuilder result = new StringBuilder();
            String line;
            while ((line = bufferedReader.readLine()) != null) {
            String responseMsg = result.toString();

            return ReturnT.SUCCESS;
        } catch (Exception e) {
            return ReturnT.FAIL;
        } finally {
            try {
                if (bufferedReader != null) {
                if (connection != null) {
            } catch (Exception e2) {


     * 5,Life cycle task example: Support custom related logic when task is initialized and destroyed;
    @XxlJob(value = "demoJobHandler2", init = "init", destroy = "destroy")
    public ReturnT<String> demoJobHandler2(String param) throws Exception {
        XxlJobLogger.log("XXL-JOB, Hello World.");
        return ReturnT.SUCCESS;
    public void init(){
    public void destroy(){


Run your own projects, configure tasks

After the project starts successfully, configure the executor and create a new task:

1. Backend page configuration executor: Note: appname and configuration are consistent

2. Create a new task and start: Note that the jobHandler needs to be consistent with the comment value

Then start the task: when the following proof appears:

Prove that jobHandler is registered successfully.

Then the console clicks Enable, dubug view

View the console after execution: and output the results, you can perform the task you want to perform.


206 original articles were published. 1042 were praised. 560,000 visits+
His message board follow

Tags: Java Spring Database github

Posted on Tue, 04 Feb 2020 19:30:48 -0500 by Dagwing