码迷,mamicode.com
首页 > 其他好文 > 详细

JFinal Quartz 2.2.1插件

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

标签:java   io   art   for   re   c   

    参照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插件

标签:java   io   art   for   re   c   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!