spring里quartz调度的连续触发

作法是通过spring在执行的触发器的监听器的里面注入下一个要执行的监听器实例,这

样就可以达到连续地触发,而至于具体怎样的触发:比如说什么时候触发,下一个相隔多久才

触发这些则可以通过配置文件进行配置。

这是用于连续触发的Listener:

package com.sww.quartz.listeners;

import org.apache.log4j.Logger;
import org.quartz.JobDetail;
import org.quartz.JobExecutionContext;
import org.quartz.Scheduler;
import org.quartz.Trigger;
import org.quartz.listeners.TriggerListenerSupport;
import org.springframework.scheduling.quartz.QuartzJobBean;
import org.springframework.scheduling.quartz.SimpleTriggerBean;

public class NextJobTriggerListener extends TriggerListenerSupport {
private Logger logger = Logger.getLogger(getClass());
private String name;
public String getName() {
return this.name;
}
public void setName(String name)
{
this.name=name;
}
private SimpleTriggerBean nextTrigger;
public void setNextTrigger(SimpleTriggerBean nextTrigger) {
this.nextTrigger = nextTrigger;
}
@Override
public void triggerComplete(Trigger trigger, JobExecutionContext context, int code) {
try{
logger.debug("Trigger :"+trigger.getGroup()+"."+trigger.getName()+" completed with code:"+code+",next trigger is "+nextTrigger.getGroup()+"."+nextTrigger.getName());
if(code!=3)return;
Scheduler schduler=context.getScheduler();
JobDetail nextJob=nextTrigger.getJobDetail();
//查找名称和即将加入的任务一样的任务
JobDetail oldJob=schduler.getJobDetail(nextJob.getName(),nextJob.getGroup());
//查找名称和即将加入的触发器一样的触发器
Trigger oldTrigger=schduler.getTrigger( nextTrigger.getName(),nextTrigger.getGroup());

if(oldJob==null&&oldTrigger==null)//同名的任务和触发器都不存在
{
logger.debug("inside scheduleJob."+code);
schduler.scheduleJob(nextJob,nextTrigger);
}else//同名的任务或触发器
{

logger.debug("oldJob==null:"+(oldJob==null));
logger.debug("oldTrigger==null:"+(oldTrigger==null));
}
super.triggerComplete(trigger, context, code);
}catch(Exception e)
{
e.printStackTrace();
}
}


}


这是具体怎样触发的spring的配置文件:

<?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:jee="http://www.springframework.org/schema/jee"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.0.xsd">

<bean
class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="quartzProperties">
<props>
<prop key="org.quartz.threadPool.class">
org.quartz.simpl.SimpleThreadPool
</prop>
<prop key="org.quartz.threadPool.threadCount">10</prop>
<prop key="org.quartz.threadPool.threadPriority">
5
</prop>
<prop
key="org.quartz.threadPool.threadsInheritContextClassLoaderOfInitializingThread">
true
</prop>
</props>
</property>
<property name="triggers">
<list>
<ref bean="provinceTrigger"/>
</list>
</property>
<property name="triggerListeners">
<list>
<ref bean="provinceTriggerListener"/>
<ref bean="cityTriggerListener"/>
<ref bean="linkTriggerListener"/>
</list>
</property>
</bean>

<bean id="provinceJob"
class="org.springframework.scheduling.quartz.JobDetailBean">
<property name="jobClass">
<value>com.tms.quartz.jobs.ProvinceJob</value>
</property>
</bean>
<bean id="provinceTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail">
<ref bean="provinceJob" />
</property>
<property name="cronExpression">
<value>0 30 0 * * ?</value>
</property>
<property name="triggerListenerNames">
<list>
<value>provinceTriggerListener</value>
</list>
</property>
</bean>
<bean id="provinceTriggerListener" class="com.tms.quartz.listeners.NextJobTriggerListener">
<property name="nextTrigger" ref="cityTrigger"></property>
<property name="name" value="provinceTriggerListener"></property>
</bean>


<bean id="cityJob"
class="org.springframework.scheduling.quartz.JobDetailBean">
<property name="jobClass">
<value>com.tms.quartz.jobs.CityJob</value>
</property>
</bean>
<bean id="cityTrigger"
class="org.springframework.scheduling.quartz.SimpleTriggerBean">
<property name="jobDetail">
<ref bean="cityJob" />
</property>
<property name="startDelay">
<value>60000</value>
</property>
<property name="repeatCount">
<value>0</value>
</property>
<property name="repeatInterval">
<value>60000</value>
</property>
<property name="triggerListenerNames">
<list>
<value>cityTriggerListener</value>
</list>
</property>
</bean>
<bean id="cityTriggerListener" class="com.tms.quartz.listeners.NextJobTriggerListener">
<property name="nextTrigger" ref="linkTrigger"></property>
<property name="name" value="cityTriggerListener"></property>
</bean>

<!--
<bean id="linkJob"
class="org.springframework.scheduling.quartz.JobDetailBean">
<property name="jobClass">
<value>com.tms.quartz.jobs.CityLinkJob</value>
</property>
</bean>
<bean id="linkTrigger"
class="org.springframework.scheduling.quartz.SimpleTriggerBean">
<property name="jobDetail">
<ref bean="linkJob" />
</property>
<property name="startDelay">
<value>60000</value>
</property>
<property name="repeatCount">
<value>0</value>
</property>
<property name="repeatInterval">
<value>60000</value>
</property>
<property name="triggerListenerNames">
<list>
<value>linkTriggerListener</value>
</list>
</property>
</bean>
<bean id="linkTriggerListener" class="com.tms.quartz.listeners.NextJobTriggerListener">
<property name="nextTrigger" ref="sightTrigger"></property>
<property name="name" value="linkTriggerListener"></property>
</bean>
-->

<bean id="sightJob"
class="org.springframework.scheduling.quartz.JobDetailBean">
<property name="jobClass">
<value>com.tms.quartz.jobs.SightJob</value>
</property>
</bean>
<bean id="sightTrigger"
class="org.springframework.scheduling.quartz.SimpleTriggerBean">
<property name="jobDetail">
<ref bean="sightJob" />
</property>
<property name="startDelay">
<value>60000</value>
</property>
<property name="repeatCount">
<value>0</value>
</property>
<property name="repeatInterval">
<value>60000</value>
</property>
</bean>
</beans>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值