spring+quartz任务定制总结

来源:互联网 发布:eclipse改json字体 编辑:程序博客网 时间:2024/05/04 16:48

在北京出差,周末在宾馆有空,把spring任务机制重新捋了一下。

用到的版本:spring3.1,quartz1.8.5,jdk1.6

要实现定时执行任务的功能,

首先有个非常重要的信息:任务定时间需要单独新建一个xml文件进行配置,切不可与spring的其他配置文件(如applicationConfig.xml)放在一起,否则任务不能执行,在此,我们新建了一个配置文件:applicationConfig-jobCfg.xml。
其次需要有需要执行的方法类,由于下面有java本身的任务定时器,也有基于quartz的,所以下面我们给这两种方式分别新建了一个类:
如下:
---------------------------------------------------------------------------------------------------------------

这个是用quartz实现的(如果是用JobDetailBean来配置任务,则不需要继承QuartzJobBean)

public class QuartzTest extends QuartzJobBean{private  static Integer INFO = 1;private  static Integer INFO1 = 1;    public void printHello(){        System.out.println("----------QuartzTest,我是第1个方法哦---------- hello:"+INFO+"   时间:"+new Date());        INFO++;    }        public void printHello1(){        System.out.println("----------QuartzTest,我是第2个方法哦---------- hello:"+INFO1+"   时间:"+new Date());        INFO1++;    }@Overrideprotected void executeInternal(JobExecutionContext arg0)throws JobExecutionException {// TODO Auto-generated method stubprintHello();}}

-------------------------------------------

这个是基于java实现的:
public class MyTimeTask extends TimerTask {private  static Integer INFO = 1;@Overridepublic void run() {// TODO Auto-generated method stubSystem.out.println("----------MyTimeTask---------- hello,"+INFO+"   时间:"+new Date());INFO++;}}

---------------------------------------------------------------------------------------------------------------

以下每一个代码框中的配置文件是各种情况下的独立配置,可以直接运行的(后面三个需要加上第一个的头文件 )。  大笑    


<?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:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd"><!-- quartz定制任务 -开始  --><!-- 用MethodInvokingJobDetailFactoryBean来定制任务,是不需要继承QuartzJobBean的。 --><bean id="quartzTest" class="com.veivo.ppq.action.admin.QuartzTest"/><!-- 调度业务  --><bean id="mytestQuartz" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"><property name="targetObject" ref="quartzTest"></property><!-- 任务调用的类  --><property name="targetMethod" value="printHello"></property><!-- 任务调用类的方法,不可定义多个方法,若一个方法中有多个方法需要调用,则可以定义多个Trigger,本文最后有个例子  --><property name="concurrent" value="false"/><!-- 设置不可并发  --></bean><!-- 增加调度触发器  --><bean id="cronTrigger" lazy-init="false"  class="org.springframework.scheduling.quartz.CronTriggerBean"><!--每隔10秒执行一次。--><property name="jobDetail" ref="mytestQuartz"></property><property name="cronExpression" value="*/10 * * * * ? "></property></bean><!-- 设置调度   --><bean id="scheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean" ><property name="triggers"><list><ref bean="cronTrigger"/></list></property><property name="autoStartup" value="true"/> </bean><!-- 定制任务 -结束  --></beans>

-----------------------------------------------------------------------------------------------------------------------------------------

<!-- quartz定制任务 -开始  --><!-- 用JobDetailBean就不需要配置targetObject或targetBean,如下配置就可,但是需要继承QuartzJobBean,在QuartzTest中会继承一个方法executeInternal来执行任务 --><bean id="mytestQuartz" class="org.springframework.scheduling.quartz.JobDetailBean">   <property name="jobClass" value="com.veivo.ppq.action.admin.QuartzTest"/></bean><!-- 增加调度触发器  --><bean id="cronTrigger" lazy-init="false"  class="org.springframework.scheduling.quartz.SimpleTriggerBean"><!--每隔5秒执行一次,延迟10秒执行。--><property name="jobDetail" ref="mytestQuartz"></property><property name="startDelay">      <value>10000</value>   </property>   <property name="repeatInterval">      <value>5000</value>   </property>  </bean><!-- 设置调度   --><bean id="scheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean" ><property name="triggers"><list><ref bean="cronTrigger"/></list></property><property name="autoStartup" value="true"/> </bean>

------------------------------------------------------------------------------------------------------------------------

<!-- java定制任务 -开始  --><!-- 用JobDetailBean需要继承TimerTask,在MyTimeTask中会继承一个方法run来执行任务 --><bean id="myTimeTask" class="com.veivo.ppq.action.admin.MyTimeTask"/><bean id="scheduleTask" class="org.springframework.scheduling.timer.ScheduledTimerTask"><property name="timerTask" ref="myTimeTask" /><!--每隔5秒执行一次,延迟10秒执行。--><property name="period"><value>5000</value></property><property name="delay"><value>10000</value></property></bean><bean class="org.springframework.scheduling.timer.TimerFactoryBean"><property name="scheduledTimerTasks"><list><ref bean="scheduleTask" /></list></property></bean><!-- java定制任务 -结束  -->

------------------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------------------
<!-- quartz定制多个任务   --><!-- 定制任务 -开始  --><bean id="quartzTest" class="com.veivo.ppq.action.admin.QuartzTest"/><!-- 调度业务  --><bean id="mytestQuartz" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"><property name="targetObject" ref="quartzTest"></property><property name="targetMethod" value="printHello"></property><property name="concurrent" value="false"/></bean><bean id="mytestQuartz1" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"><property name="targetObject" ref="quartzTest"></property><property name="targetMethod" value="printHello1"></property><property name="concurrent" value="false"/></bean><!-- 增加调度触发器  --><bean id="cronTrigger" lazy-init="false"  class="org.springframework.scheduling.quartz.CronTriggerBean"><!--每隔2秒执行一次。--><property name="jobDetail" ref="mytestQuartz"></property><property name="cronExpression" ><value>*/2 * * * * ?</value></property></bean><bean id="cronTrigger1" lazy-init="false"  class="org.springframework.scheduling.quartz.CronTriggerBean"><!--每隔4秒执行一次。--><property name="jobDetail" ref="mytestQuartz1"></property><property name="cronExpression" ><value>*/4 * * * * ?</value></property></bean><!-- 设置调度   --><bean id="scheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean" ><!-- <property name="dataSource" ref="dataSource"/><property name="configLocation" value="classpath:quartz.properties"/>--><property name="triggers"><list><ref bean="cronTrigger"/><ref bean="cronTrigger1"/></list></property></bean><!-- 定制任务 -结束  -->


原创粉丝点击