第一步,导入依赖
<dependency> <groupId>com.xuxueli</groupId> <artifactId>xxl-job-core</artifactId> <version>2.3.1</version> </dependency>
第二步:配置application.properties
xxl: job: admin: # 调度中心服务部署的地址 addresses: http://localhost:8080/xxl-job-admin # 执行器通讯TOKEN,要和调度中心服务部署配置的accessToken一致,要不然无法连接注册 accessToken: default_token executor: # 执行器AppName appname: demo-service-impl # 执行器注册 [选填]:优先使用该配置作为注册地址,为空时使用内嵌服务 ”IP:PORT“ 作为注册地址。从而更灵活的支持容器类型执行器动态IP和动态映射端口问题。 address: ip: #执行器端口号:小于等于0则自动获取;默认端口为9999,单机部署多个执行器时,注意要配置不同执行器端口; port: 8889 # 执行器运行日志文件存储磁盘路径 [选填] :需要对该路径拥有读写权限;为空则使用默认路径; logpath: # 执行器日志文件保存天数 [选填] : 过期日志自动清理, 限制值大于等于3时生效; 否则, 如-1, 关闭自动清理功能; logretentiondays: 30 server: port: 8081
package com.example.demo; import com.xxl.job.core.executor.impl.XxlJobSpringExecutor; import lombok.RequiredArgsConstructor; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration @RequiredArgsConstructor public class BeanConfig { private final XxlJobProperties xxlJobProperties; @Bean public XxlJobSpringExecutor xxlJobExecutor() { XxlJobSpringExecutor xxlJobSpringExecutor = new XxlJobSpringExecutor(); xxlJobSpringExecutor.setAdminAddresses(xxlJobProperties.getAdminAddresses()); xxlJobSpringExecutor.setAppname(xxlJobProperties.getAppname()); xxlJobSpringExecutor.setAddress(xxlJobProperties.getAdminAddresses()); xxlJobSpringExecutor.setAddress(xxlJobProperties.getIp()); xxlJobSpringExecutor.setPort(xxlJobProperties.getPort()); xxlJobSpringExecutor.setAccessToken(xxlJobProperties.getAccessToken()); xxlJobSpringExecutor.setLogPath(xxlJobProperties.getLogPath()); xxlJobSpringExecutor.setLogRetentionDays(xxlJobProperties.getLogRetentionDays()); return xxlJobSpringExecutor; } }
package com.example.demo; import lombok.Data; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Configuration; @Configuration @Data public class XxlJobProperties { @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; }
package com.example.demo; import com.xxl.job.core.biz.model.ReturnT; import com.xxl.job.core.handler.annotation.XxlJob; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Component; @Slf4j @Component @RequiredArgsConstructor public class xxljobtest { @XxlJob("demo-service-impl") public ReturnT<String> xxlJobTest(String date) { log.info("---------xxlJobTest定时任务执行成功--------"); return ReturnT.SUCCESS; } }
package com.example.demo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.scheduling.annotation.EnableScheduling; @EnableScheduling @SpringBootApplication public class Demo1Application { public static void main(String[] args) { SpringApplication.run(Demo1Application.class, args); } }