歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Spring整合Quartz

Spring整合Quartz

日期:2017/3/1 10:06:13   编辑:Linux編程

1.定時執行的類

  1. package thb.quartz;
  2. public class QuartzJob {
  3. /**
  4. * 定時執行的方法
  5. */
  6. public void work() {
  7. System.out.println(System.currentTimeMillis() + ">>>執行定時任務。。。");
  8. }
  9. }

2.quartz配置文件

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xmlns:util="http://www.springframework.org/schema/util"
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.5.xsd"
  5. default-lazy-init="true">
  6. <!-- 要調用執行定時任務的類 -->
  7. <bean id="quartzJob" class="thb.quartz.QuartzJob"></bean>
  8. <!-- 定義調用對象和調用對象的方法(觸發器) -->
  9. <bean id="jobTask" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
  10. <!-- 調用的類 -->
  11. <property name="targetObject">
  12. <ref bean="quartzJob"/>
  13. </property>
  14. <!-- 調用的方法 -->
  15. <property name="targetMethod">
  16. <value>work</value>
  17. </property>
  18. </bean>
  19. <!-- 定義觸發事件(調度器) -->
  20. <bean id="doTime" class="org.springframework.scheduling.quartz.CronTriggerBean">
  21. <property name="jobDetail" ref="jobTask"></property>
  22. <!-- 10點開始,每隔1分鐘 -->
  23. <property name="cronExpression" value="0 * 10 * * ?"></property>
  24. </bean>
  25. <bean id="startQuartz" lazy-init="false" autowire="no" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
  26. <property name="triggers">
  27. <list>
  28. <ref bean="doTime"/>
  29. </list>
  30. </property>
  31. </bean>
  32. </beans>

3.測試代碼

  1. package thb.quartz;
  2. import org.springframework.context.ApplicationContext;
  3. import org.springframework.context.support.FileSystemXmlApplicationContext;
  4. public class QuartzTest {
  5. /**
  6. * 定時任務測試
  7. */
  8. public static void main(String[] args) {
  9. ApplicationContext ctx = new FileSystemXmlApplicationContext("/resource/spring/quartz.xml");
  10. System.out.println("定時任務開始執行。。。");
  11. }
  12. }
Copyright © Linux教程網 All Rights Reserved