定时任务类统一接口以及实现(1)

public interface ITaskRunner {
	/**
	 * 
	 * @方法描述:任务运行接口
             * @date Jul 1, 2009
	 *
	 */
	public void run();
}

 

/**
 * 
 */
package cn.com.starit.pon.service.scheduler.impl;


import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.quartz.CronTrigger;
import org.quartz.Job;
import org.quartz.JobDetail;
import org.quartz.Scheduler;

import cn.com.starit.pon.service.scheduler.CommonSchedulerContainer;
import cn.com.starit.pon.service.scheduler.ITaskRunner;


/**
 * @类型: AbstractTaskRunner
 * @说明: 抽象任务运行器,为实现具体的任务运行提供统一抽象父类
  */
public  class BaseTaskRunner implements ITaskRunner {
	
	protected String taskName = null;任务名称
	protected Job clzName = null;一个job
	protected String timeRule = null;按什么事件触发
	public BaseTaskRunner(){}
	public BaseTaskRunner(String taskName, Job jobClzName, String timeRule) {
		super();
		this.taskName = taskName;
		this.clzName = jobClzName;
		this.timeRule = timeRule;
	}
	private static Log log = LogFactory.getLog(BaseTaskRunner.class);
	
	public void run() {	
		log.info("调度器< "+taskName+" > 初始化定义...开始");
		if(this.schedule())
			log.info("调度器< "+taskName+" > 初始化定义...成功");				
		else		
			log.info("调度器< "+taskName+" > 初始化定义...失败");
	}
	
	private boolean schedule() {
		if((taskName==null)||(clzName==null)||(timeRule == null))
			return false;
		try {
			Scheduler scheduler = CommonSchedulerContainer.getScheduler();
			JobDetail jobDetail =  new JobDetail(clzName.toString(),clzName.toString(),
					clzName.getClass());
			CronTrigger trigger = new CronTrigger(getTriggerName(taskName), null,
					timeRule);	
	
			scheduler.scheduleJob(jobDetail, trigger);		
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		return true;
		
	}
	
	
	public String getTaskName() {
		return taskName;
	}
	public void setTaskName(String taskName) {
		this.taskName = taskName;
	}
	public Job getClzName() {
		return clzName;
	}
	public void setClzName(Job clzName) {
		this.clzName = clzName;
	}
	public String getTimeRule() {
		return timeRule;
	}
	public void setTimeRule(String timeRule) {
		this.timeRule = timeRule;
	}
	protected  static String getJobGroupName(String name){
		return name+"_JobGroup";
	}
	protected static String getTriggerName(String name){
		return name+"_Trigger";	
	}

}

 

 

 

/**
 * 
 */
package cn.com.starit.pon.service.scheduler;

import org.apache.log4j.Logger;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.impl.StdSchedulerFactory;


/**
 * 通用调度库
 * @类型: CommonScheduler
 */
public class CommonSchedulerContainer {
	
	private static Logger	log	= Logger.getLogger(CommonSchedulerContainer.class);
	private static Scheduler scheduler=null;
	
	public static Scheduler getScheduler() {
		try {
			if(scheduler == null)
				scheduler = StdSchedulerFactory.getDefaultScheduler();
		} catch (SchedulerException e) {
			log.error(e);
			e.printStackTrace();
			throw new RuntimeException("初始调度器失败");
		}
		
		return scheduler;
	}
}

 

 

 

 

配置文件中:
<bean id="dDMDayJob" class="cn.com.starit.nms.query.summary.job.DDMDayJob">
	</bean>
	<bean id="dDMMonJob" class="cn.com.starit.nms.query.summary.job.DDMMonJob">
	</bean>
	<bean id="adslDayJob" class="cn.com.starit.nms.query.summary.job.AdslDayJob">
	</bean>
	<bean id="adslMonJob" class="cn.com.starit.nms.query.summary.job.AdslMonJob">
	</bean>
	
	<bean id='dDMDayJobRunner' class="cn.com.starit.pon.service.scheduler.impl.BaseTaskRunner">
		<property name="taskName" value="DDM性能数据统计任务(日)"></property>
		<property name="timeRule" value="0 0 3 * * ?"></property>
		<property name="clzName" ref='dDMDayJob'></property>
	</bean> 
	<bean id='dDMMonJobRunner' class="cn.com.starit.pon.service.scheduler.impl.BaseTaskRunner">
		<property name="taskName" value="DDM性能数据统计任务(月)"></property>
		<property name="timeRule" value="0 0 1 1 * ?"></property>
		<property name="clzName" ref='dDMMonJob'></property>
	</bean> 
	
	<bean id='adslDayJobRunner' class="cn.com.starit.pon.service.scheduler.impl.BaseTaskRunner">
		<property name="taskName" value="ADSL性能数据统计任务(日)"></property>
		<property name="timeRule" value="0 12 11 * * ?"></property>
		<property name="clzName" ref='adslDayJob'></property>
	</bean>
	
	<bean id='adslMonJobRunner' class="cn.com.starit.pon.service.scheduler.impl.BaseTaskRunner">
		<property name="taskName" value="ADSL性能数据统计任务(月)"></property>
		<property name="timeRule" value="0 0 1 1 * ?"></property>
		<property name="clzName" ref='adslMonJob'></property>
	</bean>

 

 

 

 

 

你可能感兴趣的:(定时任务)