任务调度使用
1. pom引入依赖
<dependency>
<groupId>com.newyetai</groupId>
<artifactId>framework-job</artifactId>
</dependency>
配置文件见framework-job的config下application.properties文件
2. XxlJobHandlerConstants类下添加任务名称(须类下唯一);
- 注释: 写清楚该任务是具体做什么的;
- 命名: 统一用单数,不带s,Handler结尾(见下面demo);
package com.newyetai.usercenter.common.constant;
/**
* 任务调度常量类
*
* @author sr on 2020-11-25
*/
public interface XxlJobHandlerConstants {
/** 统计机构最近登录时间 HCY **/
String STATISTIC_ORG_LAST_LOGIN_TIME = "statisticOrgLastLoginTimeHandler";
}
3. 任务执行方法
- 方法参数返回值: 必须跟示例一致
- 异常处理: 执行方法体必须try-catch,不允许抛异常;
/**
* do what
*/
@Component
public class Xxx {
/**
* do what
*/
@XxlJob(XxlJobHandlerConstants.STATISTIC_ORG_LAST_LOGIN_TIME)
public ReturnT<String> xxxMethod(String param) {
try {
log.info(" ***** start ");
// you do what you want...
log.info(" ***** end ");
return ReturnT.SUCCESS;
} catch (Exception e) {
return ReturnTUtils.error(e.getMessage());
}
}
}
4. 上线配置
提交 任务配置.txt
- 名称: statisticOrgLastLoginTimeHandler
- 描述: 统计机构最近登录时间
- 负责人: HCY
- 周期: 0 0 1 * * ? 每天1点
任务调度服务(8040)上对应执行器添加任务
- 根据以上提供的txt配置文件新增任务