关于spring+quartz的版本问题

          没事写写博客也是提高自己的一种途径!

 quartz2较quartz1变化较大,具体请看http://quartz-scheduler.org/documentation/quartz-2.x/new-in-quartz-2官网说明。

spring3.1以下版本支持quartz1,spring3.1及其以上版本支持quartz2和quartz1。大家选择的时候一定要注意版本!

以下是测试demo:

1、quartz1 写法

<bean id="demoService" class="demo.service.impl.DemoServiceImpl">
    	  <property name="scheduler" ref="schedulerFactory"/>
	</bean>
 
    <bean id="demoJob" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean" depends-on="demoService">
		<property name="targetObject" ref="demoService"/>
		<property name="targetMethod" value="demoQuartz">
		
		</property>
		<property name="concurrent" value="false"/>
	</bean>
	<bean id="demoTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
		<property name="jobDetail" ref="demoJob" />
		<property name="cronExpression" value="0 */1 * * * ?" />
		
	</bean>
	<bean id="schedulerFactory" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
		<property name="triggers">
			<list>
			    <ref local="demoTrigger"/>
			</list>
		</property>
	</bean>

2、quartz2写法

<bean id="demoService" class="demo.service.impl.DemoServiceImpl">
    	<property name="scheduler" ref="schedulerFactory"/>
	</bean>
	 <bean id="demoJobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
	  <property name="targetObject" ref="demoService" />
	  <property name="targetMethod" value="demoQuartz" />
	  <property name="concurrent" value="false" />
	</bean>
	 <bean id="demoTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
		<property name="jobDetail" ref="demoJobDetail" />
		<property name="cronExpression" value="0 */1 * * * ?" />
	</bean>
	<bean id="schedulerFactory" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
    <property name="triggers">
        <list>
            <ref bean="demoTrigger" />
        </list>
    </property>

spring配置差别不是很大,主要是trigger引用不一样。

但是java写法会变化很多,下面是我的demoservice

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.quartz.CronExpression;
import org.quartz.JobDetail;
import org.quartz.JobKey;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.Trigger;
import org.quartz.TriggerKey;
import org.quartz.impl.triggers.CronTriggerImpl;
import org.springframework.scheduling.quartz.CronTriggerBean;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;

import demo.service.DemoService;

public class DemoServiceImpl implements DemoService {
	private static final Log LOG = LogFactory.getLog(DemoServiceImpl.class);
	private Scheduler scheduler;
	@Override
	public void demoQuartz() {
		LOG.info("quartz开始启动"+new SimpleDateFormat("yyyy-MM-d HH:mm:ss.SSS").format(new Date()));
		//quartz 1.8.5及其以下版本下发
		/*try {
			
			Trigger trigger =scheduler.getTrigger("demoTrigger",Scheduler.DEFAULT_GROUP);
			LOG.info(trigger.getCronExpression());
			
			TriggerKey.
			Thread.sleep(10000);
			
			Calendar ca = Calendar.getInstance();
			int second = ca.get(Calendar.SECOND);             //秒
			int minute = ca.get(Calendar.MINUTE);             //分
			int hour_of_day = ca.get(Calendar.HOUR_OF_DAY);   //时
			int day_of_month = ca.get(Calendar.DAY_OF_MONTH); //日
			int month = ca.get(Calendar.MONTH);               //月
			int year = ca.get(Calendar.YEAR);                 //年
			trigger.setCronExpression(""+second+" "+minute+" "+hour_of_day+" "+" "+day_of_month+" "+(month+1)+" ?");
	  
			scheduler.rescheduleJob("demoTrigger", Scheduler.DEFAULT_GROUP, trigger);
			LOG.info(trigger.getCronExpression()+"  现在时间:"+new SimpleDateFormat("yyyy-MM-d hh:mm:ss.SSS").format(new Date()));
		} catch (SchedulerException e) {
			e.printStackTrace();
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} */
		//quartz 2.0 、2.1
		try {
			CronTriggerImpl trigger = (CronTriggerImpl) this.scheduler.getTrigger(new TriggerKey("demoTrigger", Scheduler.DEFAULT_GROUP));
			LOG.info(trigger.getCronExpression());
			Thread.sleep(10000);
			
			Calendar ca = Calendar.getInstance();
			int second = ca.get(Calendar.SECOND);             //秒
			int minute = ca.get(Calendar.MINUTE);             //分
			int hour_of_day = ca.get(Calendar.HOUR_OF_DAY);   //时
			int day_of_month = ca.get(Calendar.DAY_OF_MONTH); //日
			int month = ca.get(Calendar.MONTH);               //月
			int year = ca.get(Calendar.YEAR);                 //年
			trigger.setCronExpression(""+second+" "+minute+" "+hour_of_day+" "+" "+day_of_month+" "+(month+1)+" ?");
			scheduler.rescheduleJob(new TriggerKey("demoTrigger", Scheduler.DEFAULT_GROUP), trigger);
		} catch (SchedulerException e) {
			e.printStackTrace();
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
	public void setScheduler(Scheduler scheduler) {
		this.scheduler = scheduler;
	}
	
}
这个demo是动态指定cron,就是自定义时间!
就写到这吧!

第一次写博客,大家见谅!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值