1.github上下载xxl-job工程
https://github.com/xuxueli/xxl-job
2.导入idea
3. 启动调度中心
从github上才下下来的工程,主要分为三个部分,一个是后台管理系统模块,一个是提供的整合案例,一个是core核心模块(提供依赖的)
3.1 执行sql脚本
3.2 修改admin模块的application.properties配置文件的数据库相关参数,根据需要修改发送邮箱等参数;
admin模块的端口默认是8080
3.3 直接启动admin模块,访问后台管理系统
默认用户名密码:admin/123456
3.4 项目上可以将admin模块打包,以java -jar的方式启动 ,打包时会报xxl-job-core依赖找不着, 在pom中配上core的maven依赖即可
<dependency> <groupId>com.xuxueli</groupId> <artifactId>xxl-job-core</artifactId> <version>2.3.1</version> </dependency>
4.开发定时任务业务代码
4.1 新建一个maven工程
application.properties配置文件 (主要就是配项目端口,xxl.job.admin.addresses )
spring.application.name=xxl-job-fansirStudy server.port=8082
logging.config=classpath:logback.xml ### xxl-job admin address list, such as "http://address" or "http://address01,http://address02" #本机也不要填127.0.0.1! xxl.job.admin.addresses=http://172.16.16.166:8080/xxl-job-admin ### xxl-job, access token xxl.job.accessToken=default_token ### xxl-job executor appname xxl.job.executor.appname=${spring.application.name} ### 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
#本机也不要填127.0.0.1 xxl.job.executor.ip=172.16.16.166 xxl.job.executor.port=${server.port} ### 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
Config类 (xxl-job的端口是根据properties配置的项目端口+100,为了避免端口冲突)
/** * @Author fandr * @Date 2022-12-07 17:46 **/ @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); //设置xxl-job的运行端口 xxlJobSpringExecutor.setPort(port+100); xxlJobSpringExecutor.setAccessToken(accessToken); xxlJobSpringExecutor.setLogPath(logPath); xxlJobSpringExecutor.setLogRetentionDays(logRetentionDays); return xxlJobSpringExecutor; } }
简单的定时任务代码(参考xxl-job-executor-sample-springboot模块)
/** * @Author fandr * @Date 2022-12-07 17:50 **/ @Component @Slf4j public class SendMsgService { @XxlJob("sendMsgHandler") public void sendMsgHandler() throws Exception { XxlJobHelper.log("XXL-JOB, Hello World."); log.info("给用户:{} 发送短信!", UUID.randomUUID().toString()); } }
启动类
/** * @Author fandr * @Date 2022-12-07 17:39 * 发送短信的定时任务 **/ @SpringBootApplication public class SendMsgApplication_8082 { public static void main(String[] args) { SpringApplication.run(SendMsgApplication_8082.class,args); } }
5. 启动定时任务工程
6. 创建任务
7. 创建执行器
8. 启动任务
9. 执行效果
10.查看调度日志
简单快速整合xxl-job到此完成!
标签:SpringBoot,admin,address,job,executor,xxlJobSpringExecutor,xxl From: https://www.cnblogs.com/fansirHome/p/16964288.html