This article is participating in “Java Theme Month – Java Development Practice”, for details: juejin.cn/post/696719…

This is the 9th day of my participation in Gwen Challenge

The resources

Gitee:gitee.com/xuxueli0323…

We are using version 2.1.2, the following source is also 2.1.2.

Connector section

The preparatory work

We just need to add a comment before the corresponding method:

@XxlJob("jobName")
Copy the code

Declare the method signature as follows:

public ReturnT<String> methodName(String params) 
Copy the code

Then specify xxl-job information in the configuration file and configure the xxL-job information in the admin file.

We also need to start an admin service as the registration center.

Perform the service

1. The connection

We know that in Spring, in fact, most third-party components are packaged as beans to enable application startup.

Spring is used in the service, which we can use in

XxlJobSpringExecutor XXL – job – the core packages

See how the job is started.

XxlJobSpringExecutor

This class has the following class signature:

public class XxlJobSpringExecutor extends XxlJobExecutor implements ApplicationContextAware.InitializingBean.DisposableBean {
Copy the code

We can see that:

  • The initiator of the XxlJob core is XxlJobExecutor, and XxlJobSpringExecutor is the connector (executor) specified by this class in the Spring environment.
  • This executor is registered with Spring.

1. Startup code

Start is divided into four steps, the source code is as follows:

// start
@Override
public void afterPropertiesSet(a) throws Exception {

    // init JobHandler Repository
    initJobHandlerRepository(applicationContext);

    // init JobHandler Repository (for method)
    initJobHandlerMethodRepository(applicationContext);

    // refresh GlueFactory
    GlueFactory.refreshInstance(1);

    // super start
    super.start();
}

Copy the code

These four steps include:

  • Get the Bean identified by the JobHandler annotation and register it.

    • The JobHandler is an annotation annotated *@Deprecated*, now using an alias@xxlJob
  • Through the bean, get the method identified by JobHandler in the bean, and register.

  • Refresh the GlueFactory and initialize it as SpringGlueFactory

    GlueFactory is actually a tool for creating classes

    The default and Spring versions are actually supported

  • Call the start method of the parent class.

The above two methods are basically calling reflection to find configured classes or methods, as we’ll see next.

1.2 Parent start method

The start method in the parent class is as follows:

   public void start(a) throws Exception {

        // init logpath
        XxlJobFileAppender.initLogPath(logPath);

        // init invoker, admin-client
        initAdminBizList(adminAddresses, accessToken);


        // init JobLogFileCleanThread
        JobLogFileCleanThread.getInstance().start(logRetentionDays);

        // init TriggerCallbackThread
        TriggerCallbackThread.getInstance().start();

        // init executor-server
        port = port>0? port: NetUtil.findAvailablePort(9999); ip = (ip! =null&&ip.trim().length()>0)? ip: IpUtil.getIp(); initRpcProvider(ip, port, appName, accessToken); }Copy the code

The main work here is as follows:

  • Initialize the path to the log file to ensure that the log can be written to it

    • This log is also used by slF4J package, but the general configuration of the log path and the service log path is different, need to be configured separately.
    • If not, the default location is used for writing.
  • Add the admin service from the configuration file to the corresponding container

  • Start the log clearing thread

  • Start the callback thread

    • This section describes the callback mechanism of xxL-job

      In fact, at my company, callbacks for scheduled tasks are not used, because the logs are in different places and it is difficult to look up the problems directly in application logs.

      After the xxL-job task is executed, the system returns the execution result to the registry.

      The result return is performed by the callback thread, which sends the result of the execution to the registry via a POST instruction and prints the result returned from the registry.

  • Register the service with the admin registry.

This completes the initialization of xxL-job execution services, including:

  • Save and initialize the corresponding code that needs to be executed by the job

  • The log path is initialized

  • The admin service is registered locally

  • Initialize the log cleaning thread and call back the processing thread

  • Will this service registry to admin service, and initialize local actuator (generally use the default, as com. XXL. Job. Core. Biz. Impl. ExecutorBizImpl)