上一篇 <<<XXL-Job Admin平台搭建
下一篇 >>>XXL-Job Admin任务调度的配置及预警示例
1.依赖包引入
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.xuxueli</groupId>
<artifactId>xxl-job-core</artifactId>
<version>2.1.2</version>
</dependency>
</dependencies>
2.配置信息
# tomcat服务
server.port=7071
# log config
logging.config=classpath:logback.xml
### xxl-job管理平台地址
xxl.job.admin.addresses=http://127.0.0.1:8888/xxl-job-admin
### XXL-JOB通讯的实际端口
xxl.job.executor.appname=my-xxl-job
xxl.job.executor.ip=
xxl.job.executor.port=8881
### xxl-job, access token
xxl.job.accessToken=
### xxl-job log path
xxl.job.executor.logpath=./logs/jobhandler
### xxl-job log retention days
xxl.job.executor.logretentiondays=30
3.配置代码
@Configuration
public class XxlJobConfig {
private Logger logger = LoggerFactory.getLogger(XxlJobConfig.class);
@Value("${xxl.job.admin.addresses}")
private String adminAddresses;
@Value("${xxl.job.executor.appname}")
private String appName;
@Value("${xxl.job.executor.ip}")
private String ip;
@Value("${xxl.job.executor.port}")
private int port;
@Value("${xxl.job.accessToken}")
private String accessToken;
@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.setIp(ip);
xxlJobSpringExecutor.setPort(port);
xxlJobSpringExecutor.setAccessToken(accessToken);
xxlJobSpringExecutor.setLogPath(logPath);
xxlJobSpringExecutor.setLogRetentionDays(logRetentionDays);
return xxlJobSpringExecutor;
}
}
/**
* 针对多网卡、容器内部署等情况,可借助 "spring-cloud-commons" 提供的 "InetUtils" 组件灵活定制注册IP;
*
* 1、引入依赖:
* <dependency>
* <groupId>org.springframework.cloud</groupId>
* <artifactId>spring-cloud-commons</artifactId>
* <version>${version}</version>
* </dependency>
*
* 2、配置文件,或者容器启动变量
* spring.cloud.inetutils.preferred-networks: 'xxx.xxx.xxx.'
*
* 3、获取IP
* String ip_ = inetUtils.findFirstNonLoopbackHostInfo().getIpAddress();
*/
4.具体代码
@Component
public class MyJob {
@Value("${xxl.job.executor.port}")
private String executorPort;
/**
* 可以根据分片数做相应的事情
* @param value
* @return
*/
@XxlJob("demoMyJob")
public ReturnT<String> demoMyJob(String value) {
ShardingUtil.ShardingVO shardingVO = ShardingUtil.getShardingVo();
XxlJobLogger.log("分片参数:当前分片序号 = {}, 总分片数 = {}", shardingVO.getIndex(), shardingVO.getTotal());
System.out.println("时间:"+System.currentTimeMillis()+" 端口号:"+ executorPort+" 总分片数 = "+shardingVO.getTotal()+ ",当前分片序号 = "+shardingVO.getIndex()+", 请求参数" + value);
return ReturnT.SUCCESS;
}
@XxlJob("userMyJob")
public ReturnT<String> userMyJob(String value) {
System.out.println("userMyJob:定时任务触发:" + value);
return ReturnT.SUCCESS;
}
}
推荐阅读:
<<<传统任务调度实现方案及demo示例
<<<传统任务调度存在的缺陷
<<<如何保证我们的定时任务只会触发一次
<<<定时任务宕机后如何处理
<<<分布式任务调度平台的特点
<<<XXL-Job和Elastic-job的区别
<<<Elastic-job的设计理念及原理说明
<<<SpringBoot整合Elastic-Job示例
<<<修改Elastic-Job默认session过期时间提升故障转移速度
<<<XXL-job入门介绍
<<<XXL-job的架构与执行逻辑说明
<<<XXL-Job Admin平台搭建
<<<XXL-Job Admin任务调度的配置及预警示例
<<<XXL-Job使用分片集群机制将100W的短信在5min内发完
<<<XXL-Job如何搭建集群