FastChar-Job 是FastChar框架的任务调度插件包,直接引用到项目里即可使用!目前FastChar-Job中集成了Quartz任务调度,Quartz介绍这里不再赘述。FastChar-Job封装Quartz的目的是为了使用起来更简单,具体使用步骤如下:
1、下载并引用FastChar-Job到项目中。或通过maven如下:
<dependency>
<groupId>com.fastchar</groupId>
<artifactId>fastchar-job</artifactId>
<version>1.0</version><!--具体版本请前往maven中心查看 https://mvnrepository.com/artifact/com.fastchar/fastchar-job -->
</dependency>
2、在IFastWeb系统初始化时配置Quartz,如下代码:
public void onInit(FastEngine engine) throws Exception {
/**省略其他配置代码**/
engine.getConfig(FastQuartzConfig.class)//获取quartz配置类
.setDebug(true)//是否开启调试模式
.setUseDatabase(true)//是否将quartz持久化到数据库中
.setCreateTable(true);//是否自动创建quartz相关的数据库表格
}
3、编写并实现任务类,下面以《红包过期自动退回到余额》为例,如下:
//红包过期退还的任务类 public class RedTimeoutJob extends FastJobBase<RedTimeoutJob> {
private static final long serialVersionUID = -6343357716900016545L;
private int redId;//自定义属性:红包Id
public int getRedId() {
return redId;
}
public RedTimeoutJob setRedId(int redId) {
this.redId = redId;
setCode("RedTimeout" + redId);
return this;
}
@Override
public void run() {
/**处理红包过期的业务逻辑**/
}
}
启动任务:
new RedTimeoutJob()//实例化一个红包过期退还任务类,一般一个红包一个任务
.setRedId(getId())//配置红包的Id
.setWhileCount(1)//任务执行次数
.setDateTime(getDate("redOutTime"))//任务触发的时间
.start();//开始启动