天天看點

XXL-JOB(2)-使用

xxl-job給出了springboot集合xxljob的執行器demo,例如:xxl-job-executor-sample-springboot,

以xxl-job-executor-sample-springboot項目中的配置為例,可以寫自己的執行器項目。

這裡以xxl-job-executor-sample-springboot項目為例,梳理下springboot整合xxljob。

執行器服務配置(xxl-job-executor-sample-springboot)

<dependency>
  <groupId>com.xuxueli</groupId>
  <artifactId>xxl-job-core</artifactId>
  <version>2.3.0</version>
</dependency>
           

application.properties

# web port
server.port=8083
# 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://10.211.55.6:8082/xxl-job-admin
### xxl-job, access token
xxl.job.accessToken=
### xxl-job executor appname
xxl.job.executor.appname=xxl-job-executor-sample
### 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
### 執行器的ip
xxl.job.executor.ip=10.211.55.6
### 執行器的端口号
xxl.job.executor.port=9999
### 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
           

XxljobConfig.java

package com.xxl.job.executor.core.config;

import com.xxl.job.core.executor.impl.XxlJobSpringExecutor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 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() {
        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;
    }
}
           

任務處理類

@Slf4j
@Component
public class SampleXxlJob {
    private static Logger logger = LoggerFactory.getLogger(SampleXxlJob.class);


    /**
     * 1、簡單任務示例(Bean模式)
     */
    @XxlJob("demoJobHandler")
    public void demoJobHandler() throws Exception {
        String param = XxlJobHelper.getJobParam();
        logger.info("\ndemoJobHandler {}", param);
        XxlJobHelper.log("XXL-JOB, Hello World.");

        for (int i = 0; i < 5; i++) {
            XxlJobHelper.log("beat at:" + i);
            TimeUnit.SECONDS.sleep(2);
        }
        // default success
    }
}
           

啟動執行器服務,檢視日志,

13:00:37.261 logback [Thread-5] INFO  com.xxl.job.core.server.EmbedServer - >>>>>>>>>>> xxl-job remoting server start success, nettype = class com.xxl.job.core.server.EmbedServer, port = 9999
           
[[email protected] executor]# netstat -auntpl|grep 9999
tcp6       0      0 :::9999                 :::*                    LISTEN      2220952/java        
[[email protected] executor]# ps -ef | grep 2220952
root     2220952    6557  0 13:00 pts/2    00:00:09 java -jar xxl-job-executor-sample-springboot-2.3.0.jar
root     2341368  341594  0 13:55 pts/4    00:00:00 grep --color=auto 2220952
[[email protected] executor]# netstat -auntpl|grep 8083
tcp6       0      0 :::8083                 :::*                    LISTEN      2220952/java        
[[email protected] executor]# ps -ef | grep 2220952
           

檢視9999/8083端口号可以看出,執行器xxl-job-executor-sample-springboot占用了兩個端口号。

XXL-JOB(2)-使用
  1. 排程中心啟動
  2. 執行器注冊到排程中心

    通過啟動執行器項目,把配置的執行器ip和執行器端口注冊到排程中心,執行器可以有多個執行節點,注冊位址:xxl-job-admin項目的api相關接口。如下圖所示。

XXL-JOB(2)-使用

會利用netty 開啟一個server, port: 9999,該server執行任務處理bean。

還會開啟有一個ExecutorRegistryThread線程,不斷地注冊自己

  1. 在排程中心配置執行器的任務
XXL-JOB(2)-使用
  1. 手動/定時執行任務,
XXL-JOB(2)-使用