spring 中使用quartz实现定时任务

一般开发系统,使用定时任务非常常见。当然也可以用Java实现。比如定时器。大致如下:

   1:      public static void main(String[] args) {
   2:          Timer timer = new Timer();
   3:          timer.schedule(new java.util.TimerTask() {
   4:              int flag = 1;
   5:              
   6:              @Override
   7:              public void run() {
   8:                  System.out.println("print" + flag++);
   9:              }
  10:          }, 1000, 5000);
  11:      }

使用Timer类,缺点是对于任务执行的时间不是很好控制。而使用quartz则有强大的 cronExpression。方便定制时间。

使用spring 去quartz:

  1. 1. 在配置文件中配置quartz
       1:  <bean id="startQuertz"
       2:    class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
       3:      <property name="triggers">
       4:          <list>
       5:              <ref bean="testPrintTrigger" />
       6:          </list>
       7:      </property>
       8:  </bean>
       9:   
      10:  <!-- 触发类 -->
      11:  <bean id="testPrintTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
      12:      <property name="jobDetail">
      13:          <ref bean="printTask" />
      14:      </property>
      15:      <property name="cronExpression" value="30 * * * * ?" />
      16:  </bean>
      17:   
      18:  <!--工作工厂类-->
      19:  <bean id="printTask"
      20:        class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
      21:      <property name="targetObject">
      22:          <ref bean="printActivitor" />
      23:      </property>
      24:      <property name="targetMethod">
      25:          <value>excutePrint</value>
      26:      </property>
      27:      <property name="concurrent" value="false" />
      28:  </bean>
      29:   
      30:  <bean id="printActivitor" class="com.yingzi.springscheduledemo.task.PrintTast">
      31:   
      32:  </bean>
  2. 2. 简单测试用的实现类:
       1:  public class PrintTast {
       2:      
       3:      private int flag = 0;
       4:      
       5:      public void excutePrint(){
       6:          System.out.println("spring-schedule task print" + (flag++));
       7:      }
       8:      
       9:  }
      运行项目,输出如下:
         1:  spring-schedule task print1
         2:  spring-schedule task print1
         3:  spring-schedule task print2
         4:  spring-schedule task print2
         5:  spring-schedule task print3
         6:  spring-schedule task print3
         7:  spring-schedule task print4
         8:  spring-schedule task print4

      比较奇怪的是居然打印是每次打印两遍一样的内容。经过调试发现,原因是spring的配置文件被载入两遍导致的。所以使用quartz的时候需要注意载入容器的次数。当然也可以在代码里面加入标志防止多次执行。

      附上cronExpression 的表达式。转自网络,出处 http://gwh-08.iteye.com/blog/1601258

      1.秒(0–59)
      2.分钟(0–59)
      3.小时(0–23)
      4.月份中的日期(1–31)
      5.月份(1–12或JAN–DEC)
      6.星期中的日期(1–7或SUN–SAT)
      7.年份(1970–2099)
                秒 0-59 , - * / 
                分 0-59 , - * / 
                小时 0-23 , - * / 
                日期 1-31 , - * ? / L W C 
                月份 1-12 或者 JAN-DEC , - * / 
                星期 1-7 或者 SUN-SAT , - * ? / L C # 
                年(可选)留空, 1970-2099 , - * / 
                表达式意义 
                "0 0 12 * * ?" 每天中午12点触发 
      "0 15 10 ? * *" 每天上午10:15触发 
      "0 15 10 * * ?" 每天上午10:15触发 
      "0 15 10 * * ? *" 每天上午10:15触发 
      "0 15 10 * * ? 2005" 2005年的每天上午10:15触发 
      "0 * 14 * * ?" 在每天下午2点到下午2:59期间的每1分钟触发 
      "0 0/5 14 * * ?" 在每天下午2点到下午2:55期间的每5分钟触发 
      "0 0/5 14,18 * * ?" 在每天下午2点到2:55期间和下午6点到6:55期间的每5分钟触发 
      "0 0-5 14 * * ?" 在每天下午2点到下午2:05期间的每1分钟触发 
      "0 10,44 14 ? 3 WED" 每年三月的星期三的下午2:10和2:44触发 
      "0 15 10 ? * MON-FRI" 周一至周五的上午10:15触发 
      "0 15 10 15 * ?" 每月15日上午10:15触发 
      "0 15 10 L * ?" 每月最后一日的上午10:15触发 
      "0 15 10 ? * 6L" 每月的最后一个星期五上午10:15触发 
      "0 15 10 ? * 6L 2002-2005" 2002年至2005年的每月的最后一个星期五上午10:15触发 
      "0 15 10 ? * 6#3" 每月的第三个星期五上午10:15触发 
      每天早上6点 
      0 6 * * * 
      每两个小时 
      0 */2 * * * 
      晚上11点到早上7点之间每两个小时,早上八点 
      0 23-7/2,8 * * * 
      每个月的4号和每个礼拜的礼拜一到礼拜三的早上11点 
      0 11 4 * 1-3 
      1月1日早上4点 
      0 4 1 1 *
      
      原文地址:https://www.cnblogs.com/atio/p/3195880.html