xxljob分布式定时任务框架解决了在分布式环境下,定时任务重复执行的问题;
1.加入pom依赖:
<!-- 许雪里定时任务 -->
<dependency>
<groupId>com.xuxueli</groupId>
<artifactId>xxl-job-core</artifactId>
<version>2.2.0</version>
</dependency>
2.在yml配置文件加入配置项:
#许雪里定时任务配置
xxl:
job:
accessToken:
admin:
addresses: http://xxl-job-admin-dev.dgl.com/xxl-job-admin
executor:
address:
ip:
appname: dgl-api-job-executor
#随机端口启动
port: ${random.int[20000,59999]}
logpath: /home/data/xxljob/dgl
logretentiondays: 14
3.新建配置类
import com.xxl.job.core.executor.impl.XxlJobSpringExecutor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* xxl-job config
*
* @author xuxueli 2017-04-28
*/
@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);
xxlJobSpringExecutor.setPort(port);
xxlJobSpringExecutor.setAccessToken(accessToken);
xxlJobSpringExecutor.setLogPath(logPath);
xxlJobSpringExecutor.setLogRetentionDays(logRetentionDays);
return xxlJobSpringExecutor;
}
}
3.代码中新建处理器类:
/**
* XxlJob开发示例(Bean模式)
* 许雪里分布式定时任务
* 开发步骤:
* 1、在Spring Bean实例中,开发Job方法,方式格式要求为 "public ReturnT<String> execute(String param)"
* 2、为Job方法添加注解 "@XxlJob(value="自定义jobhandler名称", init = "JobHandler初始化方法", destroy = "JobHandler销毁方法")",注解value值对应的是调度中心新建任务的JobHandler属性的值。
* 3、执行日志:需要通过 "XxlJobLogger.log" 打印执行日志;
*/
@Component
@Slf4j
public class XxJobHandler {
@Autowired
XxJobHandlerService xxJobHandlerService;
@XxlJob(value = "xxljobHandler")
public ReturnT<String> rrcRecreatePurchaseOrderHandler(String param) throws Exception {
log.info("xxljobHandler, param = {}", param);
xxJobHandlerService.xxljobHandler();
return ReturnT.SUCCESS;
}
}
4.打开http://xxl-job-admin-dev.dgl.com/xxl-job-admin 进行配置
1).执行器管理,新增执行器,APPname是dgl-api-job-executor(上面你自己配置的appname)
2).任务管理器,新增任务:输入cron表达式 ,JobHandler 写入你代码中定义的JobHandler 本案例中是:xxljobHandler
注意:你需要在对应的服务器上事先部署一套xxljob服务才能打开网址,然后修改host文件,指向你部署的服务即可.
见下图:
xxljob官网:https://www.xuxueli.com/xxl-job/#/
版权声明:本文为weixin_43181007原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。