在实现iplugin这个接口中,实现start和stop
start() 与 stop() 方法会别会在 jfinal 启动与停止时被回调,开发插件,只需要实现 IPlugin 接口,在 start() 里面放入希望项目启动时的操作,在 stop() 中放入希望项目停止时的操作即可
1开发插件
package com.eova.common.plugin.quartz;
import java.util.List;
import org.quartz.CronScheduleBuilder;
import org.quartz.CronTrigger;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory;
import com.eova.model.Task;
import com.jfinal.kit.LogKit;
import com.jfinal.plugin.IPlugin;
public class QuartzPlugin implements IPlugin {
private SchedulerFactory sf = null;
public static Scheduler scheduler = null;
/**
* 启动Quartz
*/
@Override
@SuppressWarnings({ "rawtypes", "unchecked" })
public boolean start() {
// 创建调度工厂
sf = new StdSchedulerFactory();
try {
scheduler = sf.getScheduler();
List<Task> tasks = Task.dao.findAll();
for (Task task : tasks) {
String jobClassName = task.getStr("clazz");
String jobCronExp = task.getStr("exp");
int state = task.getInt("state");
// String params = task.getInt("params");
Class clazz;
try {
clazz = Class.forName(jobClassName);
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
JobDetail job = JobBuilder.newJob(clazz).withIdentity(jobClassName, jobClassName).build();
// job.getJobDataMap().put("type", "eova");
CronTrigger trigger = TriggerBuilder.newTrigger().withIdentity(jobClassName, jobClassName).withSchedule(CronScheduleBuilder.cronSchedule(jobCronExp)).build();
try {
scheduler.scheduleJob(job, trigger);
if (state == Task.STATE_STOP) {
// 暂停触发
scheduler.pauseTrigger(trigger.getKey());
}
} catch (SchedulerException e) {
new RuntimeException(e);
}
LogKit.info(job.getKey() + " loading and exp: " + trigger.getCronExpression());
}
scheduler.start();
} catch (SchedulerException e) {
new RuntimeException(e);
}
return true;
}
/**
* 停止Quartz
*/
@Override
public boolean stop() {
try {
scheduler.shutdown();
} catch (SchedulerException e) {
LogKit.error("shutdown error", e);
return false;
}
return true;
}
}
2注册插件
/**
* 配置插件
*/
@Override
public void configPlugin(Plugins plugins) {
System.err.println("Config Plugins Starting...");
// 配置Quartz
boolean isQuartz = xx.getConfigBool("isQuartz", true);
if (isQuartz) {
QuartzPlugin quartz = new QuartzPlugin();
plugins.add(quartz);
}
}
3、使用插件
package com.eova.core.task;
import org.quartz.JobKey;
import com.eova.common.Easy;
import com.eova.common.plugin.quartz.QuartzPlugin;
import com.eova.model.Task;
import com.jfinal.core.Controller;
/**
* 定时任务
*
* @author Jieven
*
*/
public class TaskController extends Controller {
// 启动任务
public void start() {
int id = getParaToInt(0);
Task task = Task.dao.findById(id);
try {
String className = task.getStr("clazz");
// 恢复任务
JobKey jobKey = JobKey.jobKey(className, className);
QuartzPlugin.scheduler.resumeJob(jobKey);
Task.dao.updateState(id, Task.STATE_START);
} catch (Exception e) {
e.printStackTrace();
renderJson(new Easy("任务启动失败!"));
}
renderJson(new Easy());
}
// 暂停任务
public void stop() {
int id = getParaToInt(0);
Task task = Task.dao.findById(id);
try {
String className = task.getStr("clazz");
// 暂停任务
JobKey jobKey = JobKey.jobKey(className, className);
QuartzPlugin.scheduler.pauseJob(jobKey);
Task.dao.updateState(id, Task.STATE_STOP);
} catch (Exception e) {
e.printStackTrace();
renderJson(new Easy("任务停止失败!"));
}
renderJson(new Easy());
}
// 立即运行一次任务
public void trigger() {
int id = getParaToInt(0);
Task task = Task.dao.findById(id);
try {
String className = task.getStr("clazz");
// 立即触发一次
JobKey jobKey = JobKey.jobKey(className, className);
QuartzPlugin.scheduler.triggerJob(jobKey);
} catch (Exception e) {
e.printStackTrace();
renderJson(new Easy("任务停止失败!"));
}
renderJson(new Easy());
}
}