quartz 定时任务


package quartz.counter;

/**
 * @ClassName: CounterJob
 * @Description: TODO
 * @author Zhou Shengshuai
 * @date 2014年9月19日 下午2:33:45
 * 
 */
public class CounterJob {
	public void count() {
		System.out.println("Counter Job...");
	}
}

package quartz.mapper;

/**
 * @ClassName: MapperJob
 * @Description: TODO
 * @author Zhou Shengshuai
 * @date 2014年9月19日 下午2:34:07
 * 
 */
public class MapperJob {
	public void map() {
		System.out.println("Mapper Job...");
	}
}


package quartz;

import org.quartz.CronScheduleBuilder;
import org.quartz.CronTrigger;
import org.quartz.Job;
import org.quartz.JobBuilder;
import org.quartz.JobDataMap;
import org.quartz.JobDetail;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.TriggerBuilder;

import quartz.counter.CounterJob;
import quartz.mapper.MapperJob;

/**
 * @ClassName: CounterMapper
 * @Description: TODO
 * @author Zhou Shengshuai
 * @date 2014年9月19日 下午2:35:36
 * 
 */
public class CounterMapper implements Job {
	private Scheduler scheduler = null;

	private CounterJob counterJob = null;
	private MapperJob mapperJob = null;

	private String cronExpression = null;

	public void setCounterJob(CounterJob counterJob) {
		this.counterJob = counterJob;
	}

	public void setMapperJob(MapperJob mapperJob) {
		this.mapperJob = mapperJob;
	}

	public void setCronExpression(String cronExpression) {
		this.cronExpression = cronExpression;
	}

	public void setScheduler(Scheduler scheduler) {
		this.scheduler = scheduler;
	}

	public void initial() {
		JobDetail jobDetail = JobBuilder.newJob(CounterMapper.class).withIdentity("Counter", "Mapper").build();

		jobDetail.getJobDataMap().put("counter", counterJob);
		jobDetail.getJobDataMap().put("mapper", mapperJob);

		CronTrigger cronTrigger = TriggerBuilder.newTrigger().withIdentity("Counter", "Mapper").withSchedule(CronScheduleBuilder.cronSchedule(cronExpression)).build();

		try {
			scheduler.scheduleJob(jobDetail, cronTrigger);
		} catch (SchedulerException e) {
			e.printStackTrace();
		}
	}

	@Override
	public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
		JobDataMap jobDataMap = jobExecutionContext.getMergedJobDataMap();

		CounterJob counter = (CounterJob) jobDataMap.get("counter");
		counter.count();

		MapperJob mapper = (MapperJob) jobDataMap.get("mapper");
		mapper.map();
	}

	public void destroy() {

	}
}

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
	http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd">

	<!-- 配置CounterMapper -->
	<bean id="CounterMapper" class="quartz.CounterMapper" init-method="initial" destroy-method="destroy" scope="singleton">
		<property name="scheduler" ref="Scheduler" />

		<property name="counterJob" ref="CounterJob" />
		<property name="mapperJob" ref="MapperJob" />

		<property name="cronExpression" value="*/1 * * * * ?" />
	</bean>

	<!-- 配置Scheduler -->
	<bean name="Scheduler" lazy-init="false" autowire="no" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
	</bean>

	<!-- 配置Counter -->
	<bean name="CounterJob" autowire="no" class="quartz.counter.CounterJob">
	</bean>

	<!-- 配置Mapper -->
	<bean name="MapperJob" autowire="no" class="quartz.mapper.MapperJob">
	</bean>
</beans>

package quartz;

import org.springframework.context.support.FileSystemXmlApplicationContext;

/**
 * @ClassName: Main
 * @Description: TODO
 * @author Zhou Shengshuai
 * @date 2014年9月19日 下午3:06:44
 * 
 */
public class Main {

	public static void main(String[] args) {
		// 装载程序
		final FileSystemXmlApplicationContext context = new FileSystemXmlApplicationContext("spring-quartz.xml");

		// 添加钩子
		Runtime.getRuntime().addShutdownHook(new Thread() {
			@Override
			public void run() {
				// 销毁实例
				context.getBeanFactory().destroySingletons();
			}
		});
	}
}



上一篇:Android实现局域网二维码分享图片(带密码验证)


下一篇:回溯法求解N皇后问题(Java实现)