SpringMVC整合Quartz

Spring 提供了两种方法使用org.quartz.JobDetail:MethodInvokingJobDetailFactoryBean和JobDetailFactoryBean。

1)MethodInvokingJobDetailFactoryBean方法:

在创建Job类时,不需要继承quartz的Job接口,但是需要在JavaConfig中配置Job的执行方法

MethodInvokingJobDetailFactoryBean创建Bean:

	@Bean
	public MethodInvokingJobDetailFactoryBean methodInvokingJobDetailFactoryBean(){
		MethodInvokingJobDetailFactoryBean obj=new MethodInvokingJobDetailFactoryBean();
		obj.setTargetBeanName("jobOne");
		obj.setTargetMethod("myTask");
		return obj;
	}
创建Job类:

import org.springframework.stereotype.Service;

@Service("jobOne")
public class MyJobOne {
	protected void myTask(){
		System.out.println("this is my jobOne task!");
	}
}
创建SimpleTrigger:

	@Bean
	public SimpleTriggerFactoryBean simpleTriggerFactoryBean(){
		SimpleTriggerFactoryBean stFactory=new SimpleTriggerFactoryBean();
		stFactory.setJobDetail(methodInvokingJobDetailFactoryBean().getObject());
		stFactory.setStartDelay(30000);
		stFactory.setRepeatCount(5);
		stFactory.setRepeatInterval(30000);
		return stFactory;
	}

2)JobDetailFactoryBean方法:

在创建Job类时,需要继承QuartzJobBean类

JobDetailFactoryBean创建Bean:

	@Bean
	public JobDetailFactoryBean jobDetailFactoryBean(){
		JobDetailFactoryBean factory=new JobDetailFactoryBean();
		factory.setJobClass(MyJobTwo.class);
		Map map=new HashMap();
		map.put("name", "RAM");
		map.put(MyJobTwo.COUNT, 1);
		factory.setJobDataAsMap(map);
		factory.setGroup("myGroup");
		factory.setName("myJob");
		return factory;
	}

创建Job类:

import org.quartz.DisallowConcurrentExecution;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.JobKey;
import org.quartz.PersistJobDataAfterExecution;
import org.springframework.scheduling.quartz.QuartzJobBean;
@PersistJobDataAfterExecution //可以保存参数值,使JobDetail下次执行时使用保存值
@DisallowConcurrentExecution //禁止Job实例build的JobDetail并发执行
public class MyJobTwo extends QuartzJobBean {
	public static final String COUNT="count";
	private String name;
	@Override
	protected void executeInternal(JobExecutionContext ctx) throws JobExecutionException {
		// TODO Auto-generated method stub
		JobDataMap dataMap=ctx.getJobDetail().getJobDataMap();
		int cnt=dataMap.getInt(COUNT);
		JobKey jobKey=ctx.getJobDetail().getKey();
		System.out.println(jobKey+":"+name+":"+cnt);
		cnt++;
		dataMap.put(COUNT, cnt);
	}
	
	public void setName(String name){
		this.name=name;
	}
}

创建CronTrigger:

import org.quartz.DisallowConcurrentExecution;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.JobKey;
import org.quartz.PersistJobDataAfterExecution;
import org.springframework.scheduling.quartz.QuartzJobBean;
@PersistJobDataAfterExecution
@DisallowConcurrentExecution
public class MyJobTwo extends QuartzJobBean {
	public static final String COUNT="count";
	private String name;
	@Override
	protected void executeInternal(JobExecutionContext ctx) throws JobExecutionException {
		// TODO Auto-generated method stub
		JobDataMap dataMap=ctx.getJobDetail().getJobDataMap();
		int cnt=dataMap.getInt(COUNT);
		JobKey jobKey=ctx.getJobDetail().getKey();
		System.out.println(jobKey+":"+name+":"+cnt);
		cnt++;
		dataMap.put(COUNT, cnt);
	}
	
	public void setName(String name){
		this.name=name;
	}
}
在JobDetail和Trigger配置完成后,配置Scheduler:

	@Bean
	public SchedulerFactoryBean schedulerFactoryBean(){
		SchedulerFactoryBean scheduler=new SchedulerFactoryBean();
		//scheduler.setTriggers(simpleTriggerFactoryBean().getObject(),cronTriggerFactoryBean().getObject());
		scheduler.setTriggers(cronTriggerFactoryBean().getObject());
		return scheduler;
	}

在SpringMVC Controller中控制Scheduler的移除方法:

	
        @Autowired
	SchedulerFactoryBean schedulerFactory;
	@Autowired
	CronTriggerFactoryBean cronTrigger;
        .......
	private void outTriggerGroupNames(Scheduler scheduler) throws SchedulerException{
		List groupNames=scheduler.getTriggerGroupNames();
		for(String groupName:groupNames){
			System.out.println(groupName);
		}
	}
	@RequestMapping(value="/cronStop")
	public void cronStop(){
		Scheduler scheduler=schedulerFactory.getScheduler();
		try{
			System.out.println("enter cronStop");
			System.out.println("before stop cron");
			outTriggerGroupNames(scheduler);
			scheduler.unscheduleJob(cronTrigger.getObject().getKey());
			System.out.println("after stop cron");
			outTriggerGroupNames(scheduler);
		}catch(SchedulerException e){
			e.printStackTrace();
		}
	}

参考资料:

http://www.concretepage.com/spring-4/spring-4-quartz-2-scheduler-integration-annotation-example-using-javaconfig

http://my.oschina.net/xuleo/blog/212749


你可能感兴趣的:(Java,Spring)