使用xxl-job-spring-boot-starter开发xxl-job执行器
简述
本文简单描述如何使用xxl-job-spring-boot-starter开发xxl-job的执行器服务。
开发步骤
添加依赖
创建一个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配置,也可不配置,不配置则使用默认值。
xxl-job: admin: admin-addresses: http://localhost:8080/xxl-job-admin executor: app-name: xxl-job-spring-boot-starter-example #默认为 xxl-job-executor access-token: #默认为空 log-path: logs/applogs/xxl-job/jobhandler #默认为 logs/applogs/xxl-job/jobhandler log-retention-days: 10 #默认为 10 ip: #默认为空 port: 9999 #默认为 9999
编写任务处理器
创建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; } }
启动测试
添加执行器
在调度中心->执行器管理中增加执行器。
启动执行器
- 启动示例执行器服务,启动成功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
- 执行器启动成功后,在调度中心的执行器记录中可以查看到注册信息。
添加调度任务
在调度中心->任务管理中添加一个调度任务,配置如下图:
执行调度任务
启动调度任务,查看调度日志。
示例源码

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
- 上一篇
程序员笔记|编写高性能的Java代码需要注意的4个问题
一、并发 Unable to create new native thread …… 问题1:Java中创建一个线程消耗多少内存? 每个线程有独自的栈内存,共享堆内存 问题2:一台机器可以创建多少线程? CPU,内存,操作系统,JVM,应用服务器 我们编写一段示例代码,来验证下线程池与非线程池的区别: //线程池和非线程池的区别 public class ThreadPool { public static int times = 100;//100,1000,10000 public static ArrayBlockingQueue arrayWorkQueue = new ArrayBlockingQueue(1000); public static ExecutorService threadPool = new ThreadPoolExecutor(5, //corePoolSize线程池中核心线程数 10, 60, TimeUnit.SECONDS, arrayWorkQueue, new ThreadPoolExecutor.DiscardOldestPolicy() )...
- 下一篇
Logback日志跨线程追踪实践
Logback日志跨线程追踪实践 当我们程序在服务器上面跑的时候,是不是很多时候很难定位问题? 当一大堆繁杂的日志文件丢给你的时候,你如何能从中定位到问题? 本项目源码已经上传Github: https://github.com/liushunqiu/log-track 1. 自定义日志模板参数:Logback的Pattern模板 当一个请求过来,我们想要知道当前请求具体跑了那些流程该怎么做呢? 噔噔噔噔..我们的男主Logback自定义Pattern模板即将登场. 在我们打印日志的时候,通常我们都会把一些重要的参数信息写到日志里面,方便我们后期从日志里面定位问题,其他的内部系统调用我们的程序的时候,我们可以要求他们那边在header头里面增加trace-id这样的唯一标识,如果没有该参数的话,我们自己手动生成一个唯一的标识,方便我们将整条请求链路构建起来. <property name="CONSOLE_LOG_PATTERN" value="[%yellow(%date{yyyy-MM-dd HH:mm:ss})] [%highlight(%-5level)] [%cyan...
相关文章
文章评论
共有0条评论来说两句吧...
文章二维码
点击排行
推荐阅读
最新文章
- SpringBoot2全家桶,快速入门学习开发网站教程
- Eclipse初始化配置,告别卡顿、闪退、编译时间过长
- Jdk安装(Linux,MacOS,Windows),包含三大操作系统的最全安装
- CentOS7编译安装Gcc9.2.0,解决mysql等软件编译问题
- MySQL8.0.19开启GTID主从同步CentOS8
- CentOS7,CentOS8安装Elasticsearch6.8.6
- 2048小游戏-低调大师作品
- CentOS8,CentOS7,CentOS6编译安装Redis5.0.7
- Windows10,CentOS7,CentOS8安装Nodejs环境
- CentOS7安装Docker,走上虚拟化容器引擎之路