JFinal QuartzPlugin

前段时间折腾了个cron4j plugin,用了一段时间发现还是要把周期控制到秒级,所以又来弄个quartz的,以前从来没用过原生的quartz,对这玩意儿也不熟悉,下了最新的2.1.5的包,从例子中拷了些代码出来把基本功能实现了。

完成了上次没做到的配置enable参数决定是否开启任务的功能。目前是用配置文件的形式实现的。

 

package com.jfinal.plugin.quartz;



import static org.quartz.CronScheduleBuilder.cronSchedule;

import static org.quartz.JobBuilder.newJob;

import static org.quartz.TriggerBuilder.newTrigger;



import java.io.IOException;

import java.io.InputStream;

import java.util.Date;

import java.util.Enumeration;

import java.util.Properties;



import org.quartz.CronTrigger;

import org.quartz.JobDetail;

import org.quartz.Scheduler;

import org.quartz.SchedulerException;

import org.quartz.SchedulerFactory;

import org.quartz.impl.StdSchedulerFactory;

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;



import com.jfinal.plugin.IPlugin;



public class QuartzPlugin implements IPlugin {

	private Logger logger = LoggerFactory.getLogger(getClass());

	private SchedulerFactory sf = null;

	private Scheduler sched = null;

	private String config = "job.properties";

	private Properties properties;



	public QuartzPlugin(String config) {

		this.config = config;

	}



	public QuartzPlugin() {

	}



	@Override

	public boolean start() {

		sf = new StdSchedulerFactory();

		try {

			sched = sf.getScheduler();

		} catch (SchedulerException e) {

			new RuntimeException(e);

		}

		loadProperties();

		Enumeration enums = properties.keys();

		while (enums.hasMoreElements()) {

			String key = enums.nextElement() + "";

			if (!key.endsWith("job")) {

				continue;

			}

			String cronKey = key.substring(0, key.indexOf("job")) + "cron";

			String enable = key.substring(0, key.indexOf("job")) + "enable";

			if (isDisableJob(enable)) {

				continue;

			}

			String jobClassName = properties.get(key) + "";

			String jobCronExp = properties.getProperty(cronKey) + "";

			Class clazz;

			try {

				clazz = Class.forName(jobClassName);

			} catch (ClassNotFoundException e) {

				throw new RuntimeException(e);

			}

			JobDetail job = newJob(clazz).withIdentity(jobClassName,

					jobClassName).build();

			CronTrigger trigger = newTrigger()

					.withIdentity(jobClassName, jobClassName)

					.withSchedule(cronSchedule(jobCronExp)).build();

			Date ft = null;

			try {

				ft = sched.scheduleJob(job, trigger);

				sched.start();

			} catch (SchedulerException e) {

				new RuntimeException(e);

			}

			logger.info(job.getKey() + " has been scheduled to run at: " + ft

					+ " and repeat based on expression: "

					+ trigger.getCronExpression());

		}

		return true;

	}



	private boolean isDisableJob(String enable) {

		return Boolean.valueOf(properties.get(enable) + "") == false;

	}



	private void loadProperties() {

		properties = new Properties();

		InputStream is = QuartzPlugin.class.getClassLoader()

				.getResourceAsStream(config);

		try {

			properties.load(is);

		} catch (IOException e) {

			throw new RuntimeException(e);

		}

	}



	@Override

	public boolean stop() {

		try {

			sched.shutdown();

		} catch (SchedulerException e) {

			logger.error("shutdown error", e);

			return false;

		}

		return true;

	}



}

smsup.job=com.xx.xx.job.SmsUpReceiveJob 
smsup.cron=*/10 * * * * ? 
smsup.enable=true

转载于:https://my.oschina.net/b1412/blog/68082

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值