preface

Hello, everyone. I’m Fu Long Yuan Lay scholar. Today, I’d like to talk about the use of XXL-job. Xxl-job is a distributed task scheduling platform used by the three companies I worked for. The first two served traditional industries (a large mobile base and a large power grid), and now this one serves the Internet industry (it has strategic cooperation with Tencent Ali). From 1.9x version, xxL-Job has been used to 2.3.0 version. In my opinion, xxL-job is becoming more and more simple and efficient. Many bugs have been fixed when it was just released, and it has kept up with the pace of technological development in recent years. It can be predicted that xxL-job will continue to be favored by more small and medium-sized enterprises.


Recommended reasons

1) After several years of development, it is favored by many, especially small and medium-sized enterprises; 2) Continued to update and iterate, and fixed many bugs. Version 2.0 began to introduce new features and reduced coupling; 3) The setup is quite simple, almost foolproof, out of the box, which is why I like it the most; 4) there are lots of things to learn, source code, although there are criticized by some senior programmer at first, but after a lot of online environment, has been very stable, people used to have read the source code, almost all is the basis of a Java development way, plain to you think you through to a decade ago, but for yourself and write not to come out, is such a wonderful feeling.


Installation method

Here I use the latest version of XXL-Job 2.3.0 as an example to explain the official document: www.xuxueli.com/xxl-job/

1, download

Source repository address

Source repository address Release Download
Github.com/xuxueli/xxl… Download
Gitee.com/xuxueli0323… Download

Central warehouse address

<! -- http://repo1.maven.org/maven2/com/xuxueli/xxl-job-core/ -->
<dependency>    
<groupId>com.xuxueli</groupId>    
<artifactId>xxl-job-core</artifactId>    
<version>${latest stable version}</version>
</dependency>
Copy the code

Tips: I recommend using the source code installation method, and then jar package deployment, because xxljob itself uses the Springboot framework, can be released to K8S with other projects, and later easier to maintain.


2, installation,

Xxljob is mainly divided into scheduling center and executor. To put it simply, the scheduling center is background management, and the executor is to execute scheduled tasks.

1) Execute SQL

In the DB directory, put it in MySQL and run it directly.


2) Dispatching center

The scheduling center is xxl-job-admin. Open application.properties and change the address and account password in the data source.

Then start the project directly, in the real world is to publish jar package execution. Starts, visit: http://localhost:8080/xxl-job-admin (behind the address will be used to install the actuator, as the callback address.)


3) actuator

The executor is an item in the XXl-Job-executor-samples project, so we can just pick the second Springboot item, or you can take it out separately, but I’ll just leave it alone, so it doesn’t matter. When you open application.properties, you need to change the address of the scheduler, because the executor must register with the scheduler to perform tasks. Other options are optional for the executor, so it is not necessary to change the address.

Similarly, after checking the configuration, start the SpringBoot project, or publish it as a JAR package. After startup, it will generally register with the scheduling center in a short time. If you open the executor management in the background of the scheduling center, you can find that it has been connected.

At this point, xxl-job is actually built, and you can see that the author wanted to use it out of the box from the beginning, so it’s very simple.


Best use method

Here I would like to mention that xxL-job has been used in the two companies I worked in Guangzhou to the current Internet company. The former companies used two scheduling methods respectively. Until the current company used the new version, it used the third method, which I think is the most friendly to programmers. This approach is called native built-in bean-mode tasks and is covered in a small section on the official website that will be posted later.

1) Write test methods

Here we write two simple GET user information interface (GET) and new order interface (POST)

package com.example.demo.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/** * <p> * Test controller * </p> **@authorFu Lung Yuen Lay, public id: Java Sharing Inn *@sinceThe 2022-02-23 him * /
@RestController
@RequestMapping("/api")
public class TestController {

    private static final Logger log = LoggerFactory.getLogger(TestController.class);

    /** * Get user information */
    @GetMapping("/getUser")
    public String getUser(@RequestParam(value = "userId") String userId,
                    @RequestParam(value = "param") String param) {

            log.debug("[XXLJOB]>>>> Obtain user information.... userId={}, param={}", userId, param);

            return "Success";
    }

    /** * New order information */
    @PostMapping(value = "/saveOrder", produces = MediaType.APPLICATION_JSON_VALUE)
    public String saveOrder(@RequestBody(required = false) Map<String, String> reqMap) {

            log.debug("[XXLJOB]>>>> New order information.... reqMap={}", reqMap);

            return "Success"; }}Copy the code


2) GET

The JobHandler column must be httpJobHandler. This is the built-in handler for xxL-job. Pass the parameter after the URL


3) POST mode

The only difference is that the parameter object is passed in JSON format. In this case, the parameter object in data in POST mode is passed in JSON format.


4) Effect

Start the two interfaces of the test project, and then open the two new scheduled tasks in the scheduling center to see whether the two test interfaces will be executed after 10 seconds and 15 seconds respectively.

It can be seen that both interfaces will start to execute scheduling tasks according to the time configured by crON expression, indicating that our whole scheduling platform construction is successful.


instructions

The most commonly used scheduling method is the BEAN mode class, which requires manual development of the Job class, and then the scheduling center to create a new task to point to this JobHandler. I used this method in my previous two companies, but I always felt uncomfortable at that time. In the red box is the built-in pattern in the form of BEAN methods mentioned in this article, that is, directly call the HTTP interface, no need to write additional Job class, this is actually the most consistent with the principle of high cohesion and low coupling, the more tasks scheduled in the project, the more you will find how clean it is to maintain.

The xxL_job_info table stores the information of the newly created scheduled task.

Then xxL_job_log table stores the log generated by our execution of the scheduling task, can be found in a few dozen, so will the online environment of the log table burst, see the following figure.

In the application.properties file of the scheduling center and the executor, there is a last line with the log retention days. The default is 30 days, which can be modified according to the project size. In fact, it’s been used for several years, and I don’t think this log is useful enough to keep for long, or default to small projects.


conclusion

XXL – the use of the job is very simple, small and medium-sized factory is enough, don’t even need to cluster the dispatching platform even hang up, after the restart still can continue to execute, basic does not affect the original business logic, and I spent the three companies are standalone deployment, online also didn’t appear any problems, that is why I strongly recommended to everyone, studious, It’s a necessity for lazy people.


If you think there is a drop of help, please click… Stretch out flourishing jade hand to like and collect ~~