首页 > 代码库 > spring框架整合使用定时任务框架java quartz的示例代码配置
spring框架整合使用定时任务框架java quartz的示例代码配置
原创整理不易,转载请注明出处:spring框架整合使用定时任务框架java quartz的示例代码配置
代码下载地址:http://www.zuidaima.com/share/1775583461723136.htm
有时候我们的任务(Job)需要再某些任务完成之后才能进行;例如从旧的数据库批量导数据的时候;需要现将被其他数据依赖的数据导入新的数据库;然后再进行关系的导入.。在这种情况下我们就可以使用Quartz的listener来做文章了。
首先我们写一个主任务的类,命名为MainJob;她的作用是作为一系列任务的开始点。
MainJob.java
package com.zuidaima.quartz.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 com.zuidaima.quartz.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 com.zuidaima.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/beanshttp://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=http://www.mamicode.com/"mainTriggerListener">>开启服务器,输出
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.
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。