spring boot admin 2.2 获取日志失败_xxl-job-spring-boot-starter简单封装成spring boot starter...

该项目将xxl-job-core封装为Spring Boot Starter,简化executor开发。通过添加依赖、配置及编写任务处理器,即可实现任务调度。文中提供了启动测试、执行器管理、任务添加及调度日志查看的详细步骤。
摘要由CSDN通过智能技术生成

简述

本项目将xxl-job-core包简单封装成spring boot starter,方便通过Spring Boot方式开发xxl-job的executor

使用方法

添加依赖

创建一个Spring Boot项目

  • 添加依赖包
org.springframework.boot spring-boot-starter-web 2.1.4.RELEASEorg.springframework.boot spring-boot-starter-tomcat org.springframework.boot spring-boot-starter-undertow 2.1.4.RELEASEcn.centychen xxl-job-spring-boot-starter 1.0.1-RELEASE

修改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")@Componentpublic class DemoJobHandler extends IJobHandler { @Override public ReturnT execute(String s) throws Exception { XxlJobLogger.log("This is a demo job."); Thread.sleep(5 * 1000L); return SUCCESS; }}

启动测试

添加执行器

在调度中心->执行器管理中增加执行器。

810c499c3a40999d611623d9da45386d.png

启动执行器

  • 启动示例执行器服务,启动成功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: default2019-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 used2019-05-10 14:26:19.067 INFO 1444 --- [ main] io.undertow.servlet : Initializing Spring embedded WebApplicationContext2019-05-10 14:26:19.067 INFO 1444 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1641 ms2019-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@660f0c2019-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.ExecutorBizImpl2019-05-10 14:26:19.699 INFO 1444 --- [ main] org.xnio : XNIO version 3.3.8.Final2019-05-10 14:26:19.714 INFO 1444 --- [ main] org.xnio.nio : XNIO NIO Implementation Version 3.3.8.Final2019-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
  • 执行器启动成功后,在调度中心的执行器记录中可以查看到注册信息。
a510f3a5b87ef5c5334379bb308d7390.png

添加调度任务

在调度中心->任务管理中添加一个调度任务,配置如下图:

993b78455c1f1265d932b5f9f973d9d7.png

执行调度任务

启动调度任务,查看调度日志。

34f4a72ec923b096babf36bf43425ecb.png
8b4e4043712c07b937475ee4d1517ffa.png

示例源码

  • 码云:centychen/xxl-job-spring-boot-starter-example
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值