Personal learning series-simple construction of xxl-job

Posted May 27, 20209 min read

When I think that the timing task that comes with springboot is already very useful, the people around me have asked me a demand. I want a timing task that can adjust the effective time at any time and want to configure the interface. I am dumbfounded. . .

xxl-job

Introduction

XXL-JOB is a distributed task scheduling platform whose core design goal is rapid development, simple learning, lightweight and easy to expand. The source code is now open and connected to the online product lines of multiple companies, out of the box.

Use

1 . Clone the project

1.1 Log in to gitee to download the code for xxl-job at https://gitee.com/xuxueli0323...
1.2 Clone according to https address

Picture.png

1.3 Successful cloning

Picture.png

2 . Use IDEA to open the xxl-job project

Picture.png

3.1 First, you need to configure the mysql database, and change the corresponding location to your own database address:

Picture.png

3.2 Need to create a new database first, the name is xxl-job

PICTURE.png

3.3 Run the project to prepare the SQL for us

Picture.png

4 . Run the project

4.1 Run the main method under the xxl-job-admin module

Picture.png

4.2 Open the configuration interface

The local login address is: http://localhost :8080/xxl-job-admin/toLogin
The account password is inside the SQL you run:

    Account:admin
    Password:123456

Picture.png
Picture.png

Own project configuration xxl-job

1 . Add maven dependency

Recommend a maven package search URL: https://mvnrepository.com/

<!-Add the web package->
<dependency>
    <groupId> org.springframework.boot </groupId>
    <artifactId> spring-boot-starter-web </artifactId>
</dependency>
<!-You can use your favorite version, I am used to the latest->
<dependency>
    <groupId> com.xuxueli </groupId>
    <artifactId> xxl-job-core </artifactId>
    <version> 2.2.0 </version>
</dependency>

2 . Add actuator configuration to configuration file

The content of the configuration file can be found in the cloned project:

/xxl-job/xxl-job-executor-samples/xxl-job-executor-sample-springboot/src/main/resources/application.properties

# The port number should not conflict with the admin program, otherwise it cannot be started
server.port = 8081
# no web
# spring.main.web-environment = false

# log config
logging.config = classpath:logback.xml
# Dispatch center deployment and address [optional]:If there are multiple addresses in the dispatch center cluster deployment, separate them with commas The executor will use this address for "executor heartbeat registration" and "task result callback"; if it is empty, close the automatic registration;
xxl.job.admin.addresses = http://127.0.0.1:8080/xxl-job-admin
# Actuator communication TOKEN [optional]:activated when not empty;
xxl.job.accessToken =
# Actuator AppName [optional]:Grouping basis for actuator heartbeat registration; automatic registration is disabled
xxl.job.executor.appname = xxl-job-executor-sample
# Actuator registration [Optional]:Use this configuration as the registration address first, and use the embedded service "IP:PORT" as the registration address when it is empty. Thereby, more flexible support for container type actuator dynamic IP and dynamic mapping port issues.
xxl.job.executor.address =
# Actuator IP [optional]:The default is empty means that the IP is automatically obtained. When multiple network cards are used, the specified IP can be manually set. The IP will not be bound to the Host and is only used as a communication utility; The center requests and triggers tasks ";
xxl.job.executor.ip =
# Actuator port number [Optional]:Less than or equal to 0 is automatically obtained; the default port is 9999, when deploying multiple actuators in a single machine, pay attention to configure different actuator ports
xxl.job.executor.port = 9999
# Actuator running log file storage disk path [optional]:You need to have read and write permissions to this path; use the default path if it is empty
xxl.job.executor.logpath =/data/applogs/xxl-job/jobhandler
# Actuator log file storage days [optional]:Automatic cleaning of expired logs, effective when the limit value is greater than or equal to 3; otherwise, such as -1, disable automatic cleaning function;
xxl.job.executor.logretentiondays = 30

3 . Actuator component configuration

The actuator component configuration content can be found in the cloned project:

/xxl-job/xxl-job-executor-samples/xxl-job-executor-sample-springboot/src/main/java/com/xxl/job/executor/core/config/XxlJobConfig.java

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
*
* @author xuxueli 2017-04-28
* /
@Configuration
public class XxlJobConfig {
private Logger logger = LoggerFactory.getLogger(XxlJobConfig.class);

    @Value("${xxl.job.admin.addresses}")
    private String adminAddresses;

    @Value("${xxl.job.accessToken}")
    private String accessToken;

    @Value("${xxl.job.executor.appname}")
    private String appname;

    @Value("${xxl.job.executor.address}")
    private String address;

    @Value("${xxl.job.executor.ip}")
    private String ip;

    @Value("${xxl.job.executor.port}")
    private int port;

    @Value("${xxl.job.executor.logpath}")
    private String logPath;

    @Value("${xxl.job.executor.logretentiondays}")
    private int logRetentionDays;


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

        return xxlJobSpringExecutor;
    }

   /**
     * For multiple network cards, deployment in containers, etc., you can use the "InetUtils" component provided by "spring-cloud-commons" to flexibly customize registration IP
     *
     * 1. Introduce dependencies:
     * <dependency>
     * <groupId> org.springframework.cloud </groupId>
     * <artifactId> spring-cloud-commons </artifactId>
     * <version> ${version} </version>
     * </dependency>
     *
     * 2. Configuration files, or container startup variables
     * spring.cloud.inetutils.preferred-networks:'xxx.xxx.xxx.'
     *
     * 3. Get IP
     * String ip_ = inetUtils.findFirstNonLoopbackHostInfo(). GetIpAddress();
     * /

}

4 . Create a xxl-job test class

The official test class can be used directly here:

/xxl-job/xxl-job-executor-samples/xxl-job-executor-sample-springboot/src/main/java/com/xxl/job/executor/service/jobhandler/SampleXxlJob.java

/**
* XxlJob development example(Bean mode)
*
* Development steps:
* 1. In the Spring Bean instance, develop the Job method, and the method format is "public ReturnT execute(String param)"
* 2. Add a note to the Job method "@XxlJob(value =" custom jobhandler name ", init =" JobHandler initialization method ", destroy =" JobHandler destruction method ")" The value of the JobHandler attribute.
* 3. Execution log:need to print the execution log through "XxlJobLogger.log";
*
* @author xuxueli 2019-12-11 21:52:51
* /
@Component
public class SampleXxlJob {
private static Logger logger = LoggerFactory.getLogger(SampleXxlJob.class);

   /**
     * 1. Simple task example(Bean mode)
     * /
    @XxlJob("demoJobHandler")
    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);
            TimeUnit.SECONDS.sleep(2);
        }
        return ReturnT.SUCCESS;
    }


   /**
     * 2. Fragmented broadcast task
     * /
    @XxlJob("shardingJobHandler")
    public ReturnT <String> shardingJobHandler(String param) throws Exception {

        //Sharding parameters
        ShardingUtil.ShardingVO shardingVO = ShardingUtil.getShardingVo();
        XxlJobLogger.log("Sharding parameters:current fragment 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("Slice {}, hit the fragment to start processing", i);
            } else {
                XxlJobLogger.log("Slice {}, ignore", i);
            }
        }

        return ReturnT.SUCCESS;
    }


   /**
     * 3. Command line tasks
     * /
    @XxlJob("commandJobHandler")
    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) {
                XxlJobLogger.log(line);
            }

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

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


   /**
     * 4. Cross-platform Http task
     * Parameter example:
     * "url:http://www.baidu.com \ n" +
     * "method:get \ n" +
     * "data:content \ n";
     * /
    @XxlJob("httpJobHandler")
    public ReturnT <String> httpJobHandler(String param) throws Exception {

        //param parse
        if(param == null || param.trim(). length() == 0) {
            XxlJobLogger.log("param [" + param + "]invalid.");
            return ReturnT.FAIL;
        }
        String []httpParams = param.split("\ n");
        String url = null;
        String method = null;
        String data = null;
        for(String httpParam:httpParams) {
            if(httpParam.startsWith("url:")) {
                url = httpParam.substring(httpParam.indexOf("url:") + 4) .trim();
            }
            if(httpParam.startsWith("method:")) {
                method = httpParam.substring(httpParam.indexOf("method:") + 7) .trim(). toUpperCase();
            }
            if(httpParam.startsWith("data:")) {
                data = httpParam.substring(httpParam.indexOf("data:") + 5) .trim();
            }
        }

        //param valid
        if(url == null || url.trim(). length() == 0) {
            XxlJobLogger.log("url [" + url + "]invalid.");
            return ReturnT.FAIL;
        }
        if(method == null ||! Arrays.asList("GET", "POST"). contains(method)) {
            XxlJobLogger.log("method [" + method + "]invalid.");
            return ReturnT.FAIL;
        }

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

            //connection setting
            connection.setRequestMethod(method);
            connection.setDoOutput(true);
            connection.setDoInput(true);
            connection.setUseCaches(false);
            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
            connection.connect();

            //data
            if(data! = null && data.trim(). length()> 0) {
                DataOutputStream dataOutputStream = new DataOutputStream(connection.getOutputStream());
                dataOutputStream.write(data.getBytes("UTF-8"));
                dataOutputStream.flush();
                dataOutputStream.close();
            }

            //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) {
                result.append(line);
            }
            String responseMsg = result.toString();

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

    }

   /**
     * 5. Examples of life cycle tasks:support custom-defined logic during task initialization and destruction;
     * /
    @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() {
        logger.info("init");
    }
    public void destroy() {
        logger.info("destory");
    }


}

5 . Start the program for debugging

Add tasks on the task management page
Picture.png
Execution time, operation mode and JobHandler should pay attention to
Picture.png
Operate in the operation menu
Picture.png

Okay, this is the end!

http://www.zhouzhaodong.xyz