有时候我们的任务(Job)需要再某些任务完成之后才能进行;例如从旧的数据库批量导数据的时候;需要现将被其他数据依赖的数据导入新的数据库;然后再进行关系的导入.。在这种情况下我们就可以使用Quartz的listener来做文章了。
首先我们写一个主任务的类,命名为MainJob;她的作用是作为一系列任务的开始点。
MainJob.java
package jobs;
import org.apache.log4j.Logger;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.QuartzJobBean;
public class MainJob extends QuartzJobBean {
private Logger logger = Logger.getLogger(getClass());
@Override
protected void executeInternal(JobExecutionContext arg0)
throws JobExecutionException {
// TODO Auto-generated method stub
logger.debug("Just say hi.");
}
}
然后我们新建另外一个任务(SecondJob)作为后续任务:
SecondJob.java
package jobs;
import org.apache.log4j.Logger;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.QuartzJobBean;
public class SecondJob extends QuartzJobBean {
private Logger logger = Logger.getLogger(getClass());
@Override
protected void executeInternal(JobExecutionContext arg0)
throws JobExecutionException {
// TODO Auto-generated method stub
logger.debug("I'm the second job.");
}
}
创建一个TriggerListener,重写其triggerComplete方法,并且添加一些方便spring注入的属性和方法。
NextJobTriggerListener.java
package 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{
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 的applicationContext.xml
applicationContext.xml
<?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 id="mainJob"
class="org.springframework.scheduling.quartz.JobDetailBean">
<!-- 运行的类 -->
<property name="jobClass">
<value>jobs.MainJob</value>
</property>
</bean>
<!-- 主任务的监听器 -->
<bean id="mainTriggerListener"
class="listeners.NextJobTriggerListener">
<!-- 下个触发器 -->
<property name="nextTrigger" ref="secondTrigger"></property>
<!-- 监听器名称 -->
<property name="name" value="mainTriggerListener"></property>
</bean>
<!-- 主任务的触发器 -->
<bean id="mainTrigger"
class="org.springframework.scheduling.quartz.SimpleTriggerBean">
<property name="jobDetail">
<!-- 上面创建的任务调度对象 -->
<ref bean="mainJob" />
</property>
<!-- 启动60秒后执行任务调度的excute方法 -->
<property name="startDelay">
<value>6000</value>
</property>
<!-- 运行次数 -->
<property name="repeatCount">
<value>0</value>
</property>
<!-- 隔一个小时运行一次(貌似多余,不写会报错) -->
<property name="repeatInterval">
<value>3600000</value>
</property>
<property name="triggerListenerNames">
<list>
<value>mainTriggerListener</value>
</list>
</property>
</bean>
<!-- 后续任务 -->
<bean id="secondJob"
class="org.springframework.scheduling.quartz.JobDetailBean">
<!-- 运行的类 -->
<property name="jobClass">
<value>jobs.SecondJob</value>
</property>
</bean>
<!-- 后续任务的触发器 -->
<bean id="secondTrigger"
class="org.springframework.scheduling.quartz.SimpleTriggerBean">
<property name="jobDetail">
<!-- 上面创建的任务调度对象 -->
<ref bean="secondJob" />
</property>
<!-- 启动6秒后执行任务调度的excute方法 -->
<property name="startDelay">
<value>6000</value>
</property>
<!-- 运行次数 -->
<property name="repeatCount">
<value>0</value>
</property>
<!-- 隔一个小时运行一次(貌似多余,不写会报错) -->
<property name="repeatInterval">
<!--
<value>3600000</value>
-->
<value>6000</value>
</property>
</bean>
<!-- 任务调度工厂类 -->
<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="mainTrigger"/>
</list>
</property>
<property name="triggerListeners">
<list>
<!-- 触发器的监听器 -->
<ref bean="mainTriggerListener"/>
</list>
</property>
</bean>
</beans>
开启服务器,输出
DEBUG [MainJob.executeInternal(14)] Just say hi.
DEBUG [NextJobTriggerListener.triggerComplete(38)] inside scheduleJob.3
DEBUG [SecondJob.executeInternal(14)] I'm the second job.
DEBUG [NextJobTriggerListener.triggerComplete(43)] oldJob==null:false
DEBUG [NextJobTriggerListener.triggerComplete(44)] oldTrigger==null:false
另外这里一个任务只绑定了一个简单的触发器,这样做是为了比较方便地可以检测到任务完成的情况;至于任务的具体内容就任由大家发挥了。写这篇文章希望能有人在其中获得启发。
Let life be beautiful like summer flowers and death like autumn leaves.