一,前言
今天公司要升级job平台,需要把以前的调度任务全部平移到新的job平台中,
所以学习了解一下job平台的使用,方便以后回顾。
二,操作
1,拉取代码,启动任务调度平台
gitee地址:https://gitee.com/aimmuselisa/xxl-job.git
注意修改自己的数据库信息,同时执行sql,sql位置 xxl-job\doc\db\tables_xxl_job.sql
直接启动入口类 :XxlJobExecutorApplication
访问连接:http://localhost:8090/xxl-job-admin/jobgroup
初始账户密码:admin/123456
tips:job项目和我的项目是连接的同一个数据库哦哦哦哦😊
2,引入依赖,添加配置(针对自己需要引入job的项目)
pom引入依赖
<dependency><groupId>com.xuxueli</groupId><artifactId>xxl-job-core</artifactId><version>2.4.0</version></dependency>
application.properties 添加配置
### xxl-job admin address list, such as "http://address" or "http://address01,http://address02"
### 需要和job-admin服务地址保持一致
xxl.job.admin.addresses=http://127.0.0.1:8090/xxl-job-admin### xxl-job, access token
xxl.job.accessToken=default_token### 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
xxl.job.executor.ip=
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
添加配置类
@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;@Beanpublic 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;}}
写个demo监听器
@Component
public class demoJobHandler {private static Logger logger = LoggerFactory.getLogger(demoJobHandler.class);/*** 1、简单任务示例(Bean模式)*/@XxlJob("demoJobHandler")public void demoJobHandler() throws Exception {XxlJobHelper.log("XXL-JOB, Hello World.");for (int i = 0; i < 5; i++) {XxlJobHelper.log("beat at:" + i);TimeUnit.SECONDS.sleep(2);}}
}
再次刷新平台
-----------------------------------------------------有以下情况就算引入成功
a,执行器有机器注册地址
b,有测试任务
c,任务执行成功日志