【前言】因为项目需要后台动态调度远程服务的任务,其中动态调度选用Quartz调度框架,远程服务还是沿用原来的Hessian,整合到JFinal后台(客户端)。本文主要记录JFinal下的Quartz控制台搭建,实现任务新建、修改和启停基本功能。
【正文】
参考资料(来源:JFinal中使用QuartzManager实现动态定时任务)感谢JFinal社区@wangqian0628
先放一张成果图:
maven依赖(其他的就是JFinal项目通用依赖)
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>2.2.1</version>
</dependency>
根据实际需求,对参考资料做了修改,以下是修改后的工具类:
package top.rushpeak.edu03.admin.util;
import static org.quartz.CronScheduleBuilder.cronSchedule;
import static org.quartz.JobBuilder.newJob;
import static org.quartz.TriggerBuilder.newTrigger;
import java.util.List;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.quartz.Job;
import org.quartz.JobDetail;
import org.quartz.JobKey;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.Trigger;
import org.quartz.TriggerKey;
import org.quartz.impl.StdSchedulerFactory;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import top.rushpeak.edu03.admin.job.DynamicJob;//这个是后面的任务实现类
public class QuartzManager {
private Logger log = LogManager.getLogger(QuartzManager.class);
private Scheduler scheduler = null;
public QuartzManager() {
try {
scheduler = new StdSchedulerFactory().getScheduler();
log.info("初始化调度器 ");
} catch (SchedulerException ex) {
log.error("初始化调度器=> [失败]:" + ex.getLocalizedMessage());
}
}
//初始化启动任务
public void initJob(){
List<Record> jobs = Db.find("SELECT * FROM job_manager WHERE 1=1 AND is_enabled = 'Y'");
for(Record job:jobs){
if("Y".equals(job.getStr("is_enabled"))){
String className = job.getStr("class");
Class<? extends Job> jobClazz = null;
try {
jobClazz = Class.forName(className).asSubclass(Job.class);
} catch (Exception e) {
System.out.println(className+"没有继承job,e=="+e);
log.error(className+"没有继承job,e=="+e);
continue;
}
String name = job.getStr("name");
String group = job.getStr("group");
String cronExpression = job.getStr("cron_expression");
this.addJob(name, group, jobClazz, cronExpression);
}
}
this.start();
}
//添加任务
public void addJob(String name, String group, Class<? extends Job> clazz, String cronExpression) {
try {
// 构造任务
JobDetail job = newJob(clazz).withIdentity(name, group).build();
// 构造任务触发器
Trigger trg = newTrigger().withIdentity(name, group).withSchedule(cronSchedule(cronExpression)).build();
// 将作业添加到调度器
scheduler.scheduleJob(job, trg);
log.info("创建作业=> [作业名称:" + name + " 作业组:" + group + "] ");
System.out.println("创建作业=> [作业名称:" + name + " 作业组:" + group + "] ");
} catch (SchedulerException e) {
e.printStackTrace();
log.error("创建作业=> [作业名称:" + name + " 作业组:" + group + "]=> [失败]");
}
}
//移除任务
public void removeJob(String name, String group) {
try {
TriggerKey tk = TriggerKey.triggerKey(name, group);
scheduler.pauseTrigger(tk);// 停止触发器
scheduler.unscheduleJob(tk);// 移除触发器
JobKey jobKey = JobKey.jobKey(name, group);
scheduler.deleteJob(jobKey);// 删除作业
log.info("删除作业=> [作业名称:" + name + " 作业组:" + group + "] ");
System.out.println("删除作业=> [作业名称:" + name + " 作业组:" + group + "] ");
} catch (SchedulerException e) {
e.printStackTrace();
log.error("删除作业=> [作业名称:" + name + " 作业组:" + group + "]=> [失败]");
}
}
public void start() {
try {
scheduler.start();
log.info("启动调度器 ");
System.out.println("启动调度器 ");
} catch (SchedulerException e) {
e.printStackTrace();
log.error("启动调度器=> [失败]");
}
}
public void shutdown() {
try {
scheduler.shutdown();
log.info("停止调度器 ");
System.out.println("停止调度器 ");
} catch (SchedulerException e) {
e.printStackTrace();
log.error("停止调度器=> [失败]");
}
}
//测试
public static void main(String[] args) throws InterruptedException{
QuartzManager qm = new QuartzManager();
String name = "DynamicJobName";
String group = "DynamicJobGroup";
String cronExpression = "*/3 * * * * ?";
String className = "top.rushpeak.edu03.admin.job.DynamicJob";
Class<? extends Job> jobClazz = null;
try {
jobClazz = Class.forName(className).asSubclass(Job.class);
} catch (Exception e) {
System.out.println(className+"没有继承job,e=="+e);
}
if(jobClazz==null){
System.exit(0);
}
qm.addJob(name, group, jobClazz, cronExpression);
qm.start();
Thread.sleep(7000);
qm.removeJob(name, group);
Thread.sleep(3000);
String name2 = "DynamicJobName2";
String group2 = "DynamicJobGroup2";
String cronExpression2 = "*/3 * * * * ?";
qm.addJob(name2, group2, DynamicJob.class, cronExpression2);
Thread