首页 > 其他 > 详细

JFinal Quartz 2.2.1插件

时间:2014-07-21 23:29:10      阅读:536      评论:0      收藏:0      [点我收藏+]

    参照ext的自己写了一个,2.2.1版本运行OK,只依赖Quartz和log4j,比较简单的。

package com.forg.plugin;

import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.Properties;

import org.quartz.CronScheduleBuilder;
import org.quartz.CronTrigger;
import org.quartz.Job;
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.jfinal.log.Logger;
import com.jfinal.plugin.IPlugin;

public class QuartzPlugin implements IPlugin {

	private final Logger logger = Logger.getLogger(getClass());
	
	private static final String JOB = "job";

	private String config = "job.properties";

	private Properties properties;

	public QuartzPlugin(String config) {
		this.config = config;
	}

	public QuartzPlugin() {
	}

	private SchedulerFactory schedulerFactory;
	private Scheduler scheduler;

	@SuppressWarnings("unchecked")
	public boolean start() {
		try {
			loadProperties();
		} catch (IOException e) {
			logger.error(e.getMessage());
			return false ;
		}

		if (properties == null) {
			return false;
		}
		
		schedulerFactory = new StdSchedulerFactory();
		
		try {
			scheduler = schedulerFactory.getScheduler();
		} catch (SchedulerException e) {
			logger.error(e.getMessage());
			return false;
		}

		if (scheduler == null) {
			logger.error("scheduler is null");
			return false;
		}

		Enumeration<Object> enums = properties.keys();
		while (enums.hasMoreElements()) {
			String key = enums.nextElement() + "";
			if (!key.endsWith(JOB) || !isTrue(getJobKey(key,"enable"))) {
                continue;
            }
			
			String jobClassName = properties.get(key) + "";
			String jobName 	= 	  key.substring(0,key.lastIndexOf("."));
			String jobCronExp =   properties.getProperty(getJobKey(key,"cron")) + "";
			String jobGroup 	= properties.getProperty(getJobKey(key,"group","jobGroup1"));
			
			Class<? extends Job> jobClass = null;
			try {
				jobClass = (Class<? extends Job>) Class.forName(jobClassName);
			} catch (ClassNotFoundException e) {
				logger.error(e.getMessage());
				return false;
			}
			
			JobDetail job = JobBuilder.newJob(jobClass)
					.withIdentity(jobName, jobGroup).build();
			CronTrigger trigger = TriggerBuilder
					.newTrigger()
					.withIdentity("trigger", jobGroup)
					.withSchedule(
							CronScheduleBuilder.cronSchedule(jobCronExp))
					.build();

			try {
				scheduler.scheduleJob(job, trigger);
				scheduler.start();
			} catch (SchedulerException e) {
				logger.error(e.getMessage());
				return false;
			}
		}
		
		return true;
	}

	public boolean stop() {

		try {
			scheduler.shutdown();
		} catch (SchedulerException e) {
			logger.error(e.getMessage());
			return false;
		}

		return true;
	}

	private void loadProperties() throws IOException {
		properties = new Properties();
		InputStream is = QuartzPlugin.class.getClassLoader()
				.getResourceAsStream(config);
		properties.load(is);
	}
	
	
	private String getJobKey(String str,String type,String defaultValue) {
        String key = getJobKey(str,type);
        
        if (key == null || "".equals(key.trim()))
        	return defaultValue;
        
        return key;
        
    }
	
	private String getJobKey(String str,String type) {
        return str.substring(0, str.lastIndexOf(JOB)) + type;
    }
	
	private boolean isTrue(String key) {
        Object enable = properties.get(key);
        if (enable != null && "false".equalsIgnoreCase((enable + "").trim())) {
            return false;
        }
        return true;
    }

}



JFinal Quartz 2.2.1插件,布布扣,bubuko.com

JFinal Quartz 2.2.1插件

原文:http://my.oschina.net/kanlianhui/blog/293589

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!