首页 > 代码库 > quartz定时器实例

quartz定时器实例

配置web

<!--启动任务监听器-->
<listener>
	<listener-class>com.suntek.web.pay.listener.ContextListener</listener-class>
</listener>

监听类

public class ContextListener implements ServletContextListener {

	public void contextDestroyed(ServletContextEvent context) {
		// TODO Auto-generated method stub
		// 停止所有任务
		String[] jobNameSet = null;
		try {
			jobNameSet = JobScheduler.scheduler.getJobNames(Scheduler.DEFAULT_GROUP);
		} catch (SchedulerException e1) {
			// TODO Auto-generated catch block
			CommonLogger.logger.error(e1.getMessage(), e1);
		}
		
		for(String jobName : jobNameSet)
		{
			try {
				JobScheduler.scheduler.deleteJob(jobName, Scheduler.DEFAULT_GROUP);
			} catch (SchedulerException e) {
				// TODO Auto-generated catch block
				CommonLogger.logger.error(e.getMessage(), e);
			}
		}
		
		try {
			if( !JobScheduler.scheduler.isShutdown() )
				JobScheduler.scheduler.shutdown();
		} catch (SchedulerException e) {
			// TODO Auto-generated catch block
			CommonLogger.logger.error(e.getMessage(), e);
		}
		
	}

	public void contextInitialized(ServletContextEvent context) {
		// TODO Auto-generated method stub
	
		JobScheduler.addStockWarningJob(StockWarningJob.StockWarningJobName, StockWarningJob.StockWarningJobTrigger);
	}

}

JobScheduler

public class JobScheduler {

	public static Scheduler scheduler;
	
	static {
		try {
			scheduler = new StdSchedulerFactory().getScheduler();
		} catch (SchedulerException e) {
			// TODO Auto-generated catch block
			
		}
	}
	private static final String TRIGGER_GROUP_NAME = Constants.APP_NAME + "_trigger_group";
	/** 
	 * @param jobName
	 * @param triggerName
	 * @author cwqi
	 */
	public static void addStockWarningJob(String jobName, String triggerName) {
		try {
			// 判断job是否存在
			if( isJobExist(jobName) ){
				scheduler.deleteJob(jobName, Scheduler.DEFAULT_GROUP);
			}
			CronTrigger trigger = new CronTrigger(triggerName, TRIGGER_GROUP_NAME);
			
			String cronExpression=Configure.getProperty("SEND_SMS_EXEC_INTERVAL");
			//每个月周一到周五的9点到17点,每隔30分钟执行一次
			//cronExpression ="0 0/1 9-17 ? * 2-6";
			cronExpression=StringUtils.isBlank(cronExpression)?"0 0/30 9-17 ? * 2-6":cronExpression+" ? * 2-6";
			trigger.setCronExpression( cronExpression);
			
			//Date nextFireTime = trigger.getNextFireTime();
			Date nextFireTime = trigger.computeFirstFireTime(new BaseCalendar());
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String nextFireTimeStr = sdf.format(nextFireTime);
			
			CommonLogger.logger.debug("[JobScheduler.addStockWarningJob]JobName"+jobName+"next execute time:"+nextFireTimeStr);
			JobDetail jobDetail = new JobDetail(jobName, Scheduler.DEFAULT_GROUP, StockWarningJob.class);
			
			JobDataMap dataMap = jobDetail.getJobDataMap();
			dataMap.put("TASK_STOCKWARNINGJOB_NAME", jobName);
			
			scheduler.scheduleJob(jobDetail, trigger);
			if(scheduler.isShutdown()){
				scheduler.start();
			}
			
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			CommonLogger.logger.error("[JobScheduler.addStockWarningJob]fail:"+jobName);
			CommonLogger.logger.error("添加定时任务异常", e);
		}
	}
	
	
	private static boolean isJobExist(String jobName) {
		boolean isExist = false;
		try {
			String[] jobNames = scheduler.getJobNames(jobName);
			if( jobNames == null )
				return false;
			
			if( Arrays.asList(jobNames).contains(jobName) )
				return true;
		} catch (SchedulerException e) {
			// TODO Auto-generated catch block
			CommonLogger.logger.error(e.getMessage(), e);
		}
		return isExist;
	}
	
}

Job

public class StockWarningJob implements Job {
	
	// 备份任务触发器名称、任务名称
	public static final String StockWarningJobTrigger = Constants.APP_NAME + "_StockWarningJobTrigger";
	public static final String StockWarningJobName = Constants.APP_NAME + "_StockWarningJobName";
	protected Logger logger = CommonLogger.logger;
	String stockValveString=Configure.getProperty("QBSTOCK_NOTENOUGH_WARNING");
	int stockValve=StringUtils.isBlank(stockValveString)?10:Integer.parseInt(stockValveString);
	
	public void execute(JobExecutionContext context) throws JobExecutionException {
		// TODO Auto-generated method stub
		JobDetail jobDetail = context.getJobDetail();
		JobDataMap dataMap = jobDetail.getJobDataMap();		
		String taskName = dataMap.getString("TASK_STOCKWARNINGJOB_NAME");	
		logger.debug("[StockWarningJob.execute]start:"+taskName);

	}
}

此外还有一种定时器是使用timerTask。