spring的任務調度spring+quartz和spring+jdk的Time的結合的講解

openkk 12年前發布 | 34K 次閱讀 Spring JEE框架

//這是spring+quartz的整合講解

<?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:util="http://www.springframework.org/schema/util"
 xmlns:p="http://www.springframework.org/schema/p"
 xmlns:aop="http://www.springframework.org/schema/aop"
 xmlns:tx="http://www.springframework.org/schema/tx"
 xmlns:context="http://www.springframework.org/schema/context"
 xsi:schemaLocation="
 http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.0.xsd
 http://www.springframework.org/schema/utilhttp://www.springframework.org/schema/util/spring-util-2.0.xsd
 http://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-2.0.xsd
 http://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-2.0.xsd
   http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-2.5.xsd">

 <!--這是要執行任務的方法  -->
 <bean id="taskExample" class="cn.itcast.tasks.TaskExample"></bean>
 <bean id="jobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
  <property name="targetObject" ref="taskExample"></property><!--這是要執行的對象  -->
  <property name="targetMethod" value="display"></property><!--這是要執行對象的某個方法  -->
  <property name="concurrent" value="false"></property><!-- 這是保證下面的兩個觸發器不并發運行,因為在默認情況下是默認并發運行的 -->
 </bean>
 <!-- 要執行的時間,這是simpleTrigger類型的觸發器 -->
 <bean id="simpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean">
  <property name="jobDetail" ref="jobDetail"></property><!-- 這是要對哪個任務的執行進行時間的限制 -->
  <property name="startDelay" value="10000"></property><!--這是要啟動spring容器后延遲十秒就執行  -->
  <property name="repeatInterval" value="1000"></property><!-- 這是每隔1秒就重復執行 -->
 </bean>
 <!-- 這是cronTrigger類型的觸發器 -->
  <!-- 要執行時間
       秒 分 時 日 月 星期 年
       *和? 代表任意時間
       - 區間
       / 每隔多久觸發
     -->
 <bean id="conTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
  <property name="jobDetail" ref="jobDetail"></property>
  <property name="cronExpression" value="1/3 * * * * ?"></property>
 </bean>
 <!-- 這是開啟計劃 -->
 <bean  class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
  <property name="triggers">
   <list>
    <ref bean="simpleTrigger"/>
    <ref bean="conTrigger"/>
   </list>
  </property>
 </bean>
</beans>

 

//這是spring+jdk的timer的整合 

<?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:util="http://www.springframework.org/schema/util"
 xmlns:p="http://www.springframework.org/schema/p"
 xmlns:aop="http://www.springframework.org/schema/aop"
 xmlns:tx="http://www.springframework.org/schema/tx"
 xmlns:context="http://www.springframework.org/schema/context"
 xsi:schemaLocation="
 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
 http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.0.xsd
 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
   http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-2.5.xsd">

 <!--這是要執行任務的方法  -->
 <bean id="taskExample" class="cn.itcast.tasks.TaskExample"></bean>
 <bean id="timerTask" class="org.springframework.scheduling.timer.MethodInvokingTimerTaskFactoryBean">
  <property name="targetObject" ref="taskExample"></property><!-- 這是要執行的對象 -->
  <property name="targetMethod" value="display"></property><!-- 這是要執行的對象的方法 -->
 </bean>
 <!--執行的時間  -->
 <bean id="scheduledTask" class="org.springframework.scheduling.timer.ScheduledTimerTask">
  <property name="delay" value="1000"></property><!--這是在spring容器實例化后的1秒后執行  -->
  <property name="period" value="1000"></property><!--這是每隔1秒后執行  -->
  <property name="timerTask" ref="timerTask"></property><!--這是要對哪個任務進行再執行  -->
 </bean>
 <!--開啟計劃  -->
 <bean class="org.springframework.scheduling.timer.TimerFactoryBean">
  <property name="scheduledTimerTasks">
   <list>
    <ref bean="scheduledTask"/><!--這是要執行的任務的時間規劃加載進來  -->
   </list>
  </property>
 </bean>
</beans>

 

 

//這樣用spring定義定時器的類

package cn.itcast.tasks;

public class TaskExample {
 
 
 public void display()
 {
  System.out.println("this is really love!!!");
 }

}

//這是測試

package cn.itcast.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean;

public class TestTaskExample {
 
 public static void main(String[] args) {
  
  ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
  
 }

}

 本文由用戶 openkk 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!