As the most popular distributed scheduling framework, XXL-Job has received a lot of developers. This paper will start from practice, from 0 to 1 to build a XXL-job scheduling platform.

1. Build XXL – job

1.1 Download the xxL-job source code

Code cloud address: gitee.com/xuxueli0323… Official website address:

1.2 Executing SQL Scripts

The doc/db/tables_xxl_job. SQL directory has the database required for XXL-job

#
# XXL-JOB v23.. 0
# Copyright (c) 2015-present, xuxueli.

CREATE database if NOT EXISTS `xxl_job` default character set utf8mb4 collate utf8mb4_unicode_ci;
use `xxl_job`;

SET NAMES utf8mb4;

CREATE TABLE `xxl_job_info` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `job_group` int(11) NOT NULL COMMENT 'Primary key ID of actuator',
  `job_desc` varchar(255) NOT NULL,
  `add_time` datetime DEFAULT NULL,
  `update_time` datetime DEFAULT NULL,
  `author` varchar(64) DEFAULT NULL COMMENT 'the writer',
  `alarm_email` varchar(255) DEFAULT NULL COMMENT 'Alarm email',
  `schedule_type` varchar(50) NOT NULL DEFAULT 'NONE' COMMENT 'Schedule type',
  `schedule_conf` varchar(128) DEFAULT NULL COMMENT 'Scheduling configuration, value meaning depends on scheduling type',
  `misfire_strategy` varchar(50) NOT NULL DEFAULT 'DO_NOTHING' COMMENT 'Schedule Expiration Policy',
  `executor_route_strategy` varchar(50) DEFAULT NULL COMMENT 'Executive Routing Policy',
  `executor_handler` varchar(255) DEFAULT NULL COMMENT 'Executor task Handler',
  `executor_param` varchar(512) DEFAULT NULL COMMENT 'Executor Task Parameters',
  `executor_block_strategy` varchar(50) DEFAULT NULL COMMENT 'Blocking Handling Strategy',
  `executor_timeout` int(11) NOT NULL DEFAULT '0' COMMENT 'Task execution timeout in seconds',
  `executor_fail_retry_count` int(11) NOT NULL DEFAULT '0' COMMENT 'Failed retry times',
  `glue_type` varchar(50) NOT NULL COMMENT 'GLUE type',
  `glue_source` mediumtext COMMENT 'GLUE source code ',
  `glue_remark` varchar(128) DEFAULT NULL COMMENT 'GLUE notes',
  `glue_updatetime` datetime DEFAULT NULL COMMENT 'GLUE update time ',
  `child_jobid` varchar(255) DEFAULT NULL COMMENT 'Subtask ID, separated by multiple commas',
  `trigger_status` tinyint(4) NOT NULL DEFAULT '0' COMMENT 'Scheduling status: 0- Stopped, 1- Running',
  `trigger_last_time` bigint(13) NOT NULL DEFAULT '0' COMMENT 'Last dispatch time',
  `trigger_next_time` bigint(13) NOT NULL DEFAULT '0' COMMENT 'Next dispatch time'.PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE `xxl_job_log` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `job_group` int(11) NOT NULL COMMENT 'Primary key ID of actuator',
  `job_id` int(11) NOT NULL COMMENT 'Task, primary key ID',
  `executor_address` varchar(255) DEFAULT NULL COMMENT 'Executor address, address of this execution',
  `executor_handler` varchar(255) DEFAULT NULL COMMENT 'Executor task Handler',
  `executor_param` varchar(512) DEFAULT NULL COMMENT 'Executor Task Parameters',
  `executor_sharding_param` varchar(20) DEFAULT NULL COMMENT 'Executor task fragment parameter of the form 1/2',
  `executor_fail_retry_count` int(11) NOT NULL DEFAULT '0' COMMENT 'Failed retry times',
  `trigger_time` datetime DEFAULT NULL COMMENT 'Schedule - Time',
  `trigger_code` int(11) NOT NULL COMMENT 'Schedule - Result',
  `trigger_msg` text COMMENT 'Schedule - Log',
  `handle_time` datetime DEFAULT NULL COMMENT 'Execution-time',
  `handle_code` int(11) NOT NULL COMMENT 'Execution-status',
  `handle_msg` text COMMENT 'Execution-Log',
  `alarm_status` tinyint(4) NOT NULL DEFAULT '0' COMMENT 'Alarm status: 0- Default, 1- No alarm, 2- Alarm succeeded, 3- Alarm failed'.PRIMARY KEY (`id`),
  KEY `I_trigger_time` (`trigger_time`),
  KEY `I_handle_code` (`handle_code`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE `xxl_job_log_report` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `trigger_day` datetime DEFAULT NULL COMMENT 'Schedule - Time',
  `running_count` int(11) NOT NULL DEFAULT '0' COMMENT 'Running - Log Quantity',
  `suc_count` int(11) NOT NULL DEFAULT '0' COMMENT 'Execution succeeded - Number of logs',
  `fail_count` int(11) NOT NULL DEFAULT '0' COMMENT 'Failed execution - Number of logs',
  `update_time` datetime DEFAULT NULL.PRIMARY KEY (`id`),
  UNIQUE KEY `i_trigger_day` (`trigger_day`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE `xxl_job_logglue` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `job_id` int(11) NOT NULL COMMENT 'Task, primary key ID',
  `glue_type` varchar(50) DEFAULT NULL COMMENT 'GLUE type',
  `glue_source` mediumtext COMMENT 'GLUE source code ',
  `glue_remark` varchar(128) NOT NULL COMMENT 'GLUE notes',
  `add_time` datetime DEFAULT NULL,
  `update_time` datetime DEFAULT NULL.PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE `xxl_job_registry` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `registry_group` varchar(50) NOT NULL,
  `registry_key` varchar(255) NOT NULL,
  `registry_value` varchar(255) NOT NULL,
  `update_time` datetime DEFAULT NULL.PRIMARY KEY (`id`),
  KEY `i_g_k_v` (`registry_group`,`registry_key`,`registry_value`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE `xxl_job_group` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `app_name` varchar(64) NOT NULL COMMENT 'Executor AppName',
  `title` varchar(12) NOT NULL COMMENT 'Executor name',
  `address_type` tinyint(4) NOT NULL DEFAULT '0' COMMENT 'Actuator address type: 0= automatic registration, 1= manual entry',
  `address_list` text COMMENT 'List of actuator addresses separated by commas',
  `update_time` datetime DEFAULT NULL.PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE `xxl_job_user` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `username` varchar(50) NOT NULL COMMENT 'account',
  `password` varchar(50) NOT NULL COMMENT 'password',
  `role` tinyint(4) NOT NULL COMMENT 'Roles: 0- Common user, 1- Administrator',
  `permission` varchar(255) DEFAULT NULL COMMENT 'Permissions: list of executor ids, separated by multiple commas'.PRIMARY KEY (`id`),
  UNIQUE KEY `i_username` (`username`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE `xxl_job_lock` (
  `lock_name` varchar(50) NOT NULL COMMENT 'Lock name'.PRIMARY KEY (`lock_name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

INSERT INTO `xxl_job_group`(`id`, `app_name`, `title`, `address_type`, `address_list`, `update_time`) VALUES (1.'xxl-job-executor-sample'.'Sample executor'.0.NULL.'the 2018-11-03 22:21:31' );
INSERT INTO `xxl_job_info`(`id`, `job_group`, `job_desc`, `add_time`, `update_time`, `author`, `alarm_email`, `schedule_type`, `schedule_conf`, `misfire_strategy`, `executor_route_strategy`, `executor_handler`, `executor_param`, `executor_block_strategy`, `executor_timeout`, `executor_fail_retry_count`, `glue_type`, `glue_source`, `glue_remark`, `glue_updatetime`, `child_jobid`) VALUES (1.1.'Test Task 1'.'the 2018-11-03 22:21:31'.'the 2018-11-03 22:21:31'.'XXL'.' '.'CRON'.'0, 0, 0 * *? * '.'DO_NOTHING'.'FIRST'.'demoJobHandler'.' '.'SERIAL_EXECUTION'.0.0.'BEAN'.' '.'GLUE code initialization '.'the 2018-11-03 22:21:31'.' ');
INSERT INTO `xxl_job_user`(`id`, `username`, `password`, `role`, `permission`) VALUES (1.'admin'.'e10adc3949ba59abbe56e057f20f883e'.1.NULL);
INSERT INTO `xxl_job_lock` ( `lock_name`) VALUES ( 'schedule_lock');

commit;


Copy the code

1.3 Modifying the Configuration File

To modify the configuration file, you need to configure the database connection information as the database information and the mailbox information as the mailbox account information

xxl-job/xxl-job-admin/src/main/resources/application.properties

Modify the log configuration file logback. XML to change the path to a relative path

<property name="log.path" value="./data/applogs/xxl-job/xxl-job-admin.log"/>
Copy the code

In the resources directory, add the /data/applogs/xxl-job/xxl-job-admin.log file

1.4 Starting the Project

Com. XXL. Job. Admin. XxlJobAdminApplication startup project, start the success as follows, the default port 8080.

Connected to the target VM, address: '127.0.0.1:53860', transport: 'socket' . ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| (_ | | | | | | | |)))) 'there comes | |. __ _ - | | | _ _ - | | | _ \ __, | / / / / = = = = = = = = = | _ | = = = = = = = = = = = = = = = | ___ / / _ / _ / _ / : : Spring Boot :: (v2.4.2) 10:22:57. 256 logback [main] INFO C.X.J ob. Admin. XxlJobAdminApplication - Starting XxlJobAdminApplication using Java 1.8.0_211 on zhaowenyiDSC008130. Local with PID 83345 (/Users/zhaowenyi/Documents/project/volvo/cms-x/xxl-job/xxl-job-admin/target/classes started by zhaowenyi in / Users/zhaowenyi/Documents/project/Volvo/CMS - x/XXL - job) 10:22:57. 259 logback [main] INFO c.x.job.admin.XxlJobAdminApplication - No active profile set, falling back to default profiles: The default 10:23:00. 583 logback [main] INFO O.S.B.W.E.T omcat. TomcatWebServer - Tomcat initialized with the port (s) : 8080 (HTTP) 10:23:00. 607 logback [main] INFO O.A.C oyote. Http11. Http11NioProtocol - the Initializing ProtocolHandler [HTTP - nio - 8080 ""] 10:23:00. 609 logback [main] INFO O.A.C atalina. Core. StandardService - Starting a service/Tomcat 10:23:00. 609 logback [main] INFO O.A.C atalina. Core. StandardEngine - Starting the Servlet engine: [Apache Tomcat/9.0.41] 10:23:00.732 logback [main] INFO O.A.C.C.C. [.[.[/xxl-job-admin] - Initializing Spring Embedded WebApplicationContext 10:23:00. 732 logback [main] INFO O.S.B.W.S.C.S ervletWebServerApplicationContext - Root WebApplicationContext: Initialization completed in 2577 ms 10:23:01. 904 logback [main] INFO C.X.J.A.C.S cheduler. XxlJobScheduler - > > > > > > > > > 10:23:01.951 logback [xxl-job, The admin JobLogReportHelper] INFO com. Zaxxer. Hikari. HikariDataSource - HikariCP - Starting... 10:23:02. 133 logback [main] The INFO O.S.S.C.T hreadPoolTaskExecutor - the Initializing the ExecutorService 'applicationTaskExecutor 10:23:02. 350 logback [main]  INFO o.s.b.a.w.s.WelcomePageHandlerMapping - Adding welcome page template: The index 10:23:03. 153 logback [XXL - job, The admin JobLogReportHelper] INFO com. Zaxxer. Hikari. HikariDataSource - HikariCP - Start completed. 10:23:03. 257 logback [the main] INFO O.S.B.A.E.W eb. EndpointLinksResolver - Exposing 2 the endpoint (s) beneath the base path '/ physical 10:23:03. 308 Logback [main] INFO O.A.C oyote. Http11. Http11NioProtocol - Starting ProtocolHandler [HTTP - nio - 8080 ""] 10:23:03. 349 logback [main] INFO o.a.c.c.C.[.[.[/xxl-job-admin] - Initializing Spring DispatcherServlet 'dispatcherServlet' 10:23:03. 349 logback [main] INFO O.S.W eb. Servlet. DispatcherServlet - the Initializing the servlet 'DispatcherServlet' 10:23:03. 352 logback [main] INFO O.S.W eb. Servlet. The DispatcherServlet - Completed initialization in 2 ms 10:23:03. 353 logback [main] INFO o.s.b.w.e.tomcat.TomcatWebServer - Tomcat started on port(s): 8080 (HTTP) with the context path '/ XXL - job - admin 10:23:03. 393 logback [main] INFO C.X.J ob. Admin. XxlJobAdminApplication - Started XxlJobAdminApplication in 7.021 seconds (JVM running for 8.851) 10:23:06.002 logback [xxL-job, Started XxlJobAdminApplication in 7.021 seconds (JVM running for 8.851) 10:23:06.002 admin JobScheduleHelper#scheduleThread] INFO c.x.j.a.c.thread.JobScheduleHelper - >>>>>>>>> init xxl-job admin scheduler  success.Copy the code

Open http://localhost:8080/xxl-job-admin/toLogin in your browser, open the following interface shows that the deployment was successful

Account name: admin Password: 123456

2. Configure the XXL-job actuator

In xxL-job, each item is an executor, and an executor can be configured with multiple scheduled tasks.

2.1 Adding a Dependency

  • Start by adding dependencies to projects that need to introduce scheduled tasks
<dependency>
    <groupId>com.xuxueli</groupId>
    <artifactId>xxl-job-core</artifactId>
    <version>2.3.0</version>
</dependency>
Copy the code

2.2 Modifying the Configuration

  • Added xxl-job configurations in the YML configuration file
xxl:
  job:
    admin:
      Management console address
      addresses: http://127.0.0.1:8080/xxl-job-admin
    accessToken:
    executor:
      The name of the current project
      appname: cms-content
      address:
      ip:
      port: 9999
      logpath:
      logretentiondays: 30
Copy the code

2.3 Adding the Config Configuration File

Add it directly without modifying it

/**
 * xxl-job config
 *
 * @author xuxueli 2017-04-28
 */
@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);

        return xxlJobSpringExecutor;
    }

    /** * The "InetUtils" component provided by "Spring-Cloud-Commons" can be used to flexibly customize the registration IP for multiple network cards and in-container deployment. * * 1, introduce dependencies:  * 
      
        * 
       
        org.springframework.cloud
        * 
       
        spring-cloud-commons
        * < version > ${version} < / version > * < / dependency > * * 2, configuration files, or container startup variables * spring in cloud. Inetutils. Preferred - networks: 'XXX. XXX. XXX. 3, obtain IP * * * String ip_ = inetUtils. FindFirstNonLoopbackHostInfo () getIpAddress (); * /
      


}
Copy the code

2.4 Adding an actuator

Actuator Management – Add an actuator

  • AppName Indicates the application name
  • Name Enter the description
  • Registration Mode Automatic registration
  • No need to fill in the machine address, will be customized scan

3. Configure tasks

3.1 Adding a Task

  • To add a scheduled task, we need to add a job to the executor project we just configured
@Component
@Slf4j
public class AutoPublishJob{

    @XxlJob("AutoPublishJob")
    public ReturnT<String> execute(String s) throws Exception {
        log.info("--------------- Start mission -------------");
        return null; }}Copy the code

3.2 Configuration Tasks

Enter the management interface, task Management – New task, enter the required information

  • Actuator: Select the newly added actuator
  • Scheduling type: Cron expression
  • JobHandler: defined in the xxlJob annotation
  • Run mode: Bean mode

3.3 Starting the Project

After the project is started, it registers with the task scheduling center. The log is as follows

The 2021-08-17 12:01:56. 88467-293 the INFO [main] C.S.C Ms. Base. Web. Config. XxlJobConfig: > > > > > > > > > > > XXL - job config init. 12:01:56 2021-08-17. 88467-372 the INFO [main] O.S.S.C oncurrent. ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor' 2021-08-17 12:01:56.481 INFO 88467 -- [main] o.s.s.c.ThreadPoolTaskScheduler : Initializing ExecutorService 'nacos-watch-task-Scheduler' 2021-08-17 12:01:56.617 WARN 88467 - [main] iguration$LoadBalancerCaffeineWarnLogger : Spring Cloud LoadBalancer is currently working with the default cache. You can switch to using Caffeine cache, By adding it to the classpath. The 2021-08-17 12:01:56. 88467-650 the INFO [main] c.x xl. Job. Core. The executor. XxlJobExecutor: >>>>>>>>>>> xxl-job register jobhandler success, name:AutoPublishJob, jobHandler:com.xxl.job.core.handler.impl.MethodJobHandler@59c862af[class Com. Souche. CMS. Base. Web. Job. AutoPublishJob# execute] 12:01:56 2021-08-17. 88467-727 WARN [main] c.xxl.job.core.executor.XxlJobExecutor : >>>>>>>>>>> xxl-job accessToken is empty. To ensure system security, Both please set the accessToken. 2021-08-17 12:01:56. 900 INFO 88467 - [the main] com. Alibaba. Nacos. Client. Naming: Initializer Namespace from System Property: NULL 2021-08-17 12:01:56.900 INFO 88467 -- [main] com.alibaba.nacos.client.naming : Initializer Namespace from System Environment: NULL 2021-08-17 12:01:56.901 INFO 88467 -- [main] com.alibaba.nacos.client.naming : Initializer namespace from System Property: NULL 2021-08-17 12:01:56.993 INFO 88467 -- [Thread-22] com.xxl.job.core.server.EmbedServer : >>>>>>>>>>> xxl-job remoting server start success, nettype = class com.xxl.job.core.server.EmbedServer, Port = 9999 2021-08-17 12:01:57. 88467-145 the INFO [main] O.S.B.W.E mbedded. Tomcat. TomcatWebServer: Tomcat started on port(s): 8072 (http) with context path ''Copy the code

3.4 Starting a Task

After the scheduled task is successfully started, the scheduled task is successfully invoked

4. To summarize

So far we have successfully set up xxL-jobs and scheduled tasks. Some working mechanisms and principles of XXL-job have not been covered yet, and it is planned to be deeply discussed in subsequent articles.