References:
Consult blog resources
XXL – job source code
Xxl-job official website document
Recently, bloggers have been using distributed timed tasks to document the learning process
1 Problems of traditional scheduled tasks
In traditional SSM projects, the Quartz timed task framework is often used. In a normal SpringBoot project, you can use the @Schedule annotation to implement the scheduled task functionality.
The above functions, in the standalone environment, there will be some problems. For example, when can it be triggered manually? Do you want to change the scheduled task time? There is no fault tolerance mechanism, the time is up to the scheduled task did not run, how to remedy? How to view the run logs of scheduled tasks?
In a clustered environment, the above features can cause a lot of problems. It’s possible to manage control through human configuration files, but it’s limited to small projects, clusters of a few machines, and at the same time, it adds a lot of difficulty to maintenance. And it hasn’t solved all of these problems.
2 XXL – the job description
Xxl-job: as a distributed task scheduling system, it implements functions such as high availability, fault-tolerant management, load balancing, and management mechanism. And the learning cost is low, easy to use, use.
3 Simple use of xxL-job
1 Clone code from the code cloud
2 Modify database connections
Files in a directory: / XXL – job/XXL – job – admin/SRC/main/resources/application. The properties
3 Run the initial SQL script for the database
The file is in the /xxl-job/doc/db/tables_xxl_job.sql directory
4 Run XxlJobAdminApplication in the xxl-job-admin module to start the project
Access the address: http://localhost:8080/xxl-job-admin
User name and password: admin/123456
4 Description of the task scheduling center
1 Running Report
The tag page collects statistics about scheduled tasks. Including, number of tasks, scheduling times, number of actuators. Success/failure ratio, scheduling time, etc.
2 Task Management
Periodic task management, including, add, edit, delete, query and other basic functions, but also provides stop, close, execute a periodic task and other functions.
3 Scheduling Logs
Execution logs of scheduled tasks include success logs and failure logs. You can view failure causes.
4 Actuator management
Actuator AppName: including the executor to add, edit, delete, query and other functions, the executor is mainly used for task scheduling, in simple terms, as a carrier of scheduled tasks, save the address of the machine and other information.
New actuator description:
AppName: uniquely identifies the AppName of each actuator cluster. The actuator automatically registers the AppName periodically. You can use this configuration to automatically discover the registered actuator for task scheduling.
Name: Optional, can describe the specific function of the machine name, mainly because AppName is mostly composed of letters and numbers, to ensure uniqueness, so the name is needed to describe its functionality.
Automatic registration: The actuator automatically registers the actuator. The scheduling center can dynamically discover the address of the actuator through the underlying registry.
Manual registration: Manually input the address information of the actuator, separated by commas, for the dispatch center to call.
Machine address: manual input actuator valid, multiple addresses separated by commas.
5 User Management
Task scheduling center users to add, edit, delete, query, and permission management.
6 Use Tutorial
Xxl-job Usage document of the distributed task scheduling center.
5 Customize scheduled tasks
According to the Demo case, we customize the scheduled task.
1 Create a module to introduce Maven dependencies
<! Jar package version -->
<dependencyManagement>
<dependencies>
<dependency>
<! -- Import dependency Management from Spring Boot --> Import dependency management from Spring Boot
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<! -- spring-boot-starter-web (spring-webmvc + tomcat) -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<! -- xxl-job-core -->
<dependency>
<groupId>com.xuxueli</groupId>
<artifactId>xxl-job-core</artifactId>
<version>${project.parent.version}</version>
</dependency>
</dependencies>
Copy the code
2 Create the configuration file application.properties
# 1 Change the port number of server.port
Appname = xxl.job. Executor. Appname = xxl.job
# 3 Change the port number of xxl.job.executor
# web port
server.port=8082
# no web
#spring.main.web-environment=false
# 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/xxl-job-admin
### xxl-job, access token
xxl.job.accessToken=
### xxl-job executor appname
xxl.job.executor.appname=xxl-job-executor-sample-springboot2
### xxl-job executor registry-address: default use address to registry , otherwise use ip:port if address is null
xxl.job.executor.address=
### xxl-job executor server-info
xxl.job.executor.ip=
xxl.job.executor.port=9998
### xxl-job executor log-path
xxl.job.executor.logpath=/data/applogs/xxl-job/jobhandler
### xxl-job executor log-retention-days
xxl.job.executor.logretentiondays=30
Copy the code
3 Create the XxlJobConfig configuration class
@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(a) {
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);
returnxxlJobSpringExecutor; }}Copy the code
4 Create a scheduled task
@Component
public class SampleXxlJob {
private static Logger logger = LoggerFactory.getLogger(SampleXxlJob.class);
@XxlJob("customJobHandler")
public void customJobHandler(a) throws Exception {
XxlJobHelper.log("XXL-JOB, Hello World.");
System.out.println("============ I am a custom scheduled task 2========="); }}Copy the code
5 Create a startup class
@SpringBootApplication
public class XxlJobExecutorApplication {
public static void main(String[] args) { SpringApplication.run(XxlJobExecutorApplication.class, args); }}Copy the code
The entire project directory structure is as follows:
6 Start the project and add an executor to the task scheduling center
7 Add a scheduled task
8 Enable the scheduled task and view the running result
6 Usage of XXL-Jobs in the cluster
1 Modify the actuator configuration. Add cluster addresses. Separate multiple addresses with commas (,)
2 Add a scheduled task
Note that the routing policy is modified. To see the effect, we choose polling, that is, scheduled tasks on each machine are triggered in turn.
3 Enable the scheduled task and view the result
7 summary
Xxl-job, as an open source, simple to use, out of the box distributed task scheduling, very easy to use and maintenance. At first, did not use out of date, thought obscure, follow the official documents and blog to learn down, received quite a lot of goods, also understand, all things, do not try, just stay at the level of watching, it is always difficult.