jfinal 插件开发

在实现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());
	}

}

 

 

 

转载于:https://my.oschina.net/zhongwenhao/blog/3032186

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值