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<String,Object> map=new HashMap<String,Object>();
		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<String> 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);
			<span style="color:#FF0000;">scheduler.unscheduleJob(cronTrigger.getObject().getKey());</span>
			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


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值