使用xxl-job-spring-boot-starter開發(fā)xxl-job執(zhí)行器

簡述

本文簡單描述如何使用xxl-job-spring-boot-starter開發(fā)xxl-job的執(zhí)行器服務(wù)。

開發(fā)步驟

添加依賴

創(chuàng)建一個Spring Boot項目

  • 添加依賴包
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <version>2.1.4.RELEASE</version>
    <exclusions>
        <!-- exclude tomcat -->
        <exclusion>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
        </exclusion>
    </exclusions>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-undertow</artifactId>
    <version>2.1.4.RELEASE</version>
</dependency>

<dependency>
    <groupId>cn.centychen</groupId>
    <artifactId>xxl-job-spring-boot-starter</artifactId>
    <version>1.0.0-RELEASE</version>
</dependency>

修改xxl-job配置

添加以下xxl-job配置,也可不配置,不配置則使用默認(rèn)值。

xxl-job:
  admin:
    admin-addresses: http://localhost:8080/xxl-job-admin
  executor:
    app-name: xxl-job-spring-boot-starter-example #默認(rèn)為 xxl-job-executor
    access-token: #默認(rèn)為空
    log-path: logs/applogs/xxl-job/jobhandler #默認(rèn)為 logs/applogs/xxl-job/jobhandler
    log-retention-days: 10 #默認(rèn)為 10
    ip: #默認(rèn)為空
    port: 9999 #默認(rèn)為 9999

編寫任務(wù)處理器

創(chuàng)建DemoJobHandler.class類,繼承IJobHandler抽象類,示例代碼如下:

import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.IJobHandler;
import com.xxl.job.core.handler.annotation.JobHandler;
import com.xxl.job.core.log.XxlJobLogger;
import org.springframework.stereotype.Component;

@JobHandler("demoJobHandler")
@Component
public class DemoJobHandler extends IJobHandler {

    @Override
    public ReturnT<String> execute(String s) throws Exception {
        XxlJobLogger.log("This is a demo job.");
        Thread.sleep(5 * 1000L);
        return SUCCESS;
    }
}

啟動測試

添加執(zhí)行器

在調(diào)度中心->執(zhí)行器管理中增加執(zhí)行器。


add-executor-image.png

啟動執(zhí)行器

  • 啟動示例執(zhí)行器服務(wù),啟動成功log如下:

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.1.4.RELEASE)

2019-05-10 14:26:16.523  INFO 1444 --- [           main] c.c.s.s.xxljob.example.Application       : Starting Application on centdeMacBook-Pro.local with PID 1444 (/Users/cent/source-java/xxl-job-spring-boot-starter-example/target/classes started by cent in /Users/cent/source-java/xxl-job-spring-boot-starter-example)
2019-05-10 14:26:16.532  INFO 1444 --- [           main] c.c.s.s.xxljob.example.Application       : No active profile set, falling back to default profiles: default
2019-05-10 14:26:19.039  WARN 1444 --- [           main] io.undertow.websockets.jsr               : UT026010: Buffer pool was not set on WebSocketDeploymentInfo, the default pool will be used
2019-05-10 14:26:19.067  INFO 1444 --- [           main] io.undertow.servlet                      : Initializing Spring embedded WebApplicationContext
2019-05-10 14:26:19.067  INFO 1444 --- [           main] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 1641 ms
2019-05-10 14:26:19.351  INFO 1444 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2019-05-10 14:26:19.483  INFO 1444 --- [           main] c.c.s.s.x.a.XxlJobAutoConfiguration      : >>>>>>>>>>> xxl job config init...
2019-05-10 14:26:19.490  INFO 1444 --- [           main] c.xxl.job.core.executor.XxlJobExecutor   : >>>>>>>>>>> xxl-job register jobhandler success, name:demoJobHandler, jobHandler:cn.centychen.springboot.starter.xxljob.example.handler.DemoJobHandler@660f0c
2019-05-10 14:26:19.543  INFO 1444 --- [           main] c.x.r.r.provider.XxlRpcProviderFactory   : >>>>>>>>>>> xxl-rpc, provider factory add service success. serviceKey = com.xxl.job.core.biz.ExecutorBiz, serviceBean = class com.xxl.job.core.biz.impl.ExecutorBizImpl
2019-05-10 14:26:19.699  INFO 1444 --- [           main] org.xnio                                 : XNIO version 3.3.8.Final
2019-05-10 14:26:19.714  INFO 1444 --- [           main] org.xnio.nio                             : XNIO NIO Implementation Version 3.3.8.Final
2019-05-10 14:26:19.809  INFO 1444 --- [           main] o.s.b.w.e.u.UndertowServletWebServer     : Undertow started on port(s) 8080 (http) with context path ''
2019-05-10 14:26:19.814  INFO 1444 --- [           main] c.c.s.s.xxljob.example.Application       : Started Application in 4.35 seconds (JVM running for 6.302)
2019-05-10 14:26:19.831  INFO 1444 --- [      Thread-14] com.xxl.rpc.remoting.net.Server          : >>>>>>>>>>> xxl-rpc remoting server start success, nettype = com.xxl.rpc.remoting.net.impl.netty_http.server.NettyHttpServer, port = 9999
  • 執(zhí)行器啟動成功后,在調(diào)度中心的執(zhí)行器記錄中可以查看到注冊信息。


    executor-info-image.png

添加調(diào)度任務(wù)

在調(diào)度中心->任務(wù)管理中添加一個調(diào)度任務(wù),配置如下圖:


add-task-image.png

執(zhí)行調(diào)度任務(wù)

啟動調(diào)度任務(wù),查看調(diào)度日志。


image
image

示例源碼

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容