The statement

Before reading this article, you should be familiar with the use of XXL-job.

The body of the

This article is based on xxL-job V2.2.0 source code.

I. Scheduling center and actuator

The following is the architecture diagram of XXL-job v2.x, which contains two core modules, namely the scheduling center and the actuator.

  • Dispatch center

    Simply speaking, it is a management system. Users can create tasks, edit tasks, manually trigger tasks and view task execution logs through the management interface provided by the system. In addition, the system background will constantly scan out the tasks to be executed from the task table in the data center and then trigger tasks one by one.

  • actuator

    When a task is triggered, either periodically or manually, the scheduling center sends an HTTP request to the executor, who is responsible for executing the task.

Xxl-job source directory

Create a task

As can be seen from the figure:

  • Task execution parameters contain many contents, such as task blocking processing policy, execution policy and so on.
  • Run mode selection beans, which is the focus of this article.
  • Before the task is executed, specify a specific JobHandler to execute it.
  • If it is a periodic task, we can adjust the execution parameters at any time during its execution. For example JobHandler (this is important!!) .

Define tasks

@Component
public class SampleXxlJob {
    /** * 1, simple task example (Bean mode) */
    @XxlJob("demoJobHandler")
    public ReturnT<String> demoJobHandler(String param) throws Exception {
        // doSomething
        returnReturnT.SUCCESS; }}Copy the code

The Spring framework annotations and xxL-Job framework annotations above define a task, When the executor starts, it scans for the demoJobHandler method in the SampleXxlJob, This method is encapsulated as a JobHandler (implemented as MethodJobHandler) object (execute method in this Handler is the specific execution logic of the task), and the JobHandler is registered in container 1. Container 1 is defined as follows:

// key is the bean name in the XxlJob annotation
/ / value for JobHandler
ConcurrentMap<String, IJobHandler> jobHandlerRepository = new ConcurrentMap();
Copy the code

Iv. Task execution process

When a task is triggered (either manually or automatically) :

  1. Dispatch centerEncapsulates the task execution parameters toTriggerParamthroughPOSTThe request toactuator.
  2. If the task is executed for the first time, yesnewA new thread is started.JobThreadOnce the thread is started, it is registered with container 2. Container 2 is defined as followsTriggerParamPush to a queue maintained in the thread.
The new thread is named JobThread and is bound to JobHandler one by one. We can get this JobHandler through JobThread.// key is the task ID, jobId
// value is the JobThread object
private static ConcurrentMap<Integer, JobThread> jobThreadRepository = new ConcurrentHashMap<Integer, JobThread>();
Copy the code
  1. JobThreadOnce the thread is started,runMethods will continue to followThe queueTo read the task execution parameters.
  2. When the execution parameters are read, the execution parameters are passed toJobHandler#executeMethod to perform the task.

A queue is maintained in the JobThread. Each time a job is executed, the execution parameters are pushed to the queue. After the JobThread starts, the execution parameters are read from the queue until the job stops.

If the task is executed a second time, the previously cached JobThread is retrieved from the container jobThreadRepository via jobId.

  • If JobThread is not empty, fetch JobHandler from JobThread.

  • As mentioned earlier, JobHandler can be changed every time a task is executed. So this is going to determine if this job is executing the same JobHandler as the last one. If not, replace the old with the new. Unregister the cached JobThread. New a new JobThread.

  • If JobHandler has not changed, reuse it.

5. Source code entry

com.xxl.job.core.biz.impl.ExecutorBizImpl#run

com.xxl.job.core.thread.JobThread#run

Copy the code

Reference documentation

Xxl-job Official document