Springboot project integration xxl-job

XXL-JOB is a lightweight distributed task scheduling platform whose core design goal is to develop quickly, learn simply...

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

serverTimezone=UTC,com.mysql.cj.jdbc.Driver

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 --> <dependency> <groupId>com.xuxueli</groupId> <artifactId>xxl-job-core</artifactId> <version>2.2.0-SNAPSHOT</version> </dependency>

(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 server.port=8081 # log config logging.config=classpath:logback.xml ### xxl-job admin address list, such as "http://address" or "http://address01,http://address02" xxl.job.admin.addresses=http://127.0.0.1:8080/job ### xxl-job executor address xxl.job.executor.appname=myapp xxl.job.executor.ip= xxl.job.executor.port=9999 ### xxl-job, access token xxl.job.accessToken= ### xxl-job log path xxl.job.executor.logpath=/data/applogs/xxl-job/jobhandler ### xxl-job log retention days xxl.job.executor.logretentiondays=30

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 */ @Configuration public class XxlJobConfig { private Logger logger = LoggerFactory.getLogger(XxlJobConfig.class); @Value("$") private String adminAddresses; @Value("$") private String appName; @Value("$") private String ip; @Value("$") private int port; @Value("$") private String accessToken; @Value("$") private String logPath; @Value("$") private int logRetentionDays; @Bean public XxlJobSpringExecutor xxlJobExecutor() { logger.info(">>>>>>>>>>> xxl-job config init."); XxlJobSpringExecutor xxlJobSpringExecutor = new XxlJobSpringExecutor(); xxlJobSpringExecutor.setAdminAddresses(adminAddresses); xxlJobSpringExecutor.setAppName(appName); xxlJobSpringExecutor.setIp(ip); xxlJobSpringExecutor.setPort(port); xxlJobSpringExecutor.setAccessToken(accessToken); xxlJobSpringExecutor.setLogPath(logPath); xxlJobSpringExecutor.setLogRetentionDays(logRetentionDays); 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> * </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 */ @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 tasks */ @XxlJob("shardingJobHandler") 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 */ @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 tasks */ @XxlJob("httpJobHandler") 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.setRequestMethod("GET"); 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(); //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) { 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,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(){ logger.info("init"); } public void destroy(){ logger.info("destory"); } }

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.

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

4 February 2020, 19:30 | Views: 3755

Add new comment

For adding a comment, please log in
or create account

0 comments