spring注解配置quartz应用

Posted 心和梦的方向

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了spring注解配置quartz应用相关的知识,希望对你有一定的参考价值。

        项目中会经常用到定时器,因此,其quartz的使用,我们必须要掌握。下面就以例子来讲解如何在spring中整合quartz,

使用注解配置的方式来实现定时执行任务。

一、引入jar包

    项目中需引入quartz的jar包,由于整合到spring中,肯定也引入了spring的相关jar包。

    例子中引用的是quartz-2.1.1版本,使用maven管理项目,pom文件需引入quartz依赖

  

二、spring配置文件中配置 (applicationContext.xml)

  1)      xmlns和 xsi:schemaLocation配置

        xmlns:task="http://www.springframework.org/schema/task"

        xsi:schemaLocation="http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd "

    

 2)task任务扫描注解   

  <!-- 加载定时任务 -->
  <task:annotation-driven/>

 3)扫描的位置

       <!-- 自动扫描 -->

      <context:component-scan base-package="com.hik.quartz"/>

4)写任务实现类及配置任务定时执行方式

    

 

使用spring注解配置quartz,非常方便实用。

下面给出配置spring配置文件配置

 1 <?xml version="1.0" encoding="UTF-8"?>    
 2 <beans xmlns="http://www.springframework.org/schema/beans"    
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
 4     xmlns:p="http://www.springframework.org/schema/p"  
 5     xmlns:aop="http://www.springframework.org/schema/aop"   
 6     xmlns:context="http://www.springframework.org/schema/context"  
 7     xmlns:jee="http://www.springframework.org/schema/jee"  
 8     xmlns:tx="http://www.springframework.org/schema/tx"  
 9     xmlns:task="http://www.springframework.org/schema/task"
10     xsi:schemaLocation="    
11         http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd  
12         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd  
13         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd  
14         http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd  
15         http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
16         http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd ">    
17         
18     <!-- 自动扫描 -->
19     <context:component-scan base-package="com.hik.service" />
20     <context:component-scan base-package="com.hik.quartz"/>
21     
22     <!-- 加载定时任务 -->
23     <task:annotation-driven/>
24     
25     <!-- 配置数据源 -->
26     <bean id="dataSource"
27         class="org.springframework.jdbc.datasource.DriverManagerDataSource">
28         <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
29         <property name="url" value="jdbc:mysql://localhost:3306/db_crm"/>
30         <property name="username" value="root"/>
31         <property name="password" value="passwd"/>
32     </bean>
33 
34     <!-- 配置mybatis的sqlSessionFactory -->
35     <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
36         <property name="dataSource" ref="dataSource" />
37         <!-- 自动扫描mappers.xml文件 -->
38         <property name="mapperLocations" value="classpath:com/hik/mappers/*.xml"></property>
39         <!-- mybatis配置文件 -->
40         <property name="configLocation" value="classpath:mybatis-config.xml"></property>
41     </bean>
42 
43     <!-- DAO接口所在包名,Spring会自动查找其下的类 -->
44     <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
45         <property name="basePackage" value="com.hik.dao" />
46         <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
47     </bean>
48 
49     <!-- (事务管理)transaction manager, use JtaTransactionManager for global tx -->
50     <bean id="transactionManager"
51         class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
52         <property name="dataSource" ref="dataSource" />
53     </bean>
54     
55     <!-- 配置事务通知属性 -->  
56     <tx:advice id="txAdvice" transaction-manager="transactionManager">  
57         <!-- 定义事务传播属性 -->  
58         <tx:attributes>  
59             <tx:method name="insert*" propagation="REQUIRED" />  
60             <tx:method name="update*" propagation="REQUIRED" />  
61             <tx:method name="edit*" propagation="REQUIRED" />  
62             <tx:method name="save*" propagation="REQUIRED" />  
63             <tx:method name="add*" propagation="REQUIRED" />  
64             <tx:method name="new*" propagation="REQUIRED" />  
65             <tx:method name="set*" propagation="REQUIRED" />  
66             <tx:method name="remove*" propagation="REQUIRED" />  
67             <tx:method name="delete*" propagation="REQUIRED" />  
68             <tx:method name="change*" propagation="REQUIRED" />  
69             <tx:method name="check*" propagation="REQUIRED" />  
70             <tx:method name="get*" propagation="REQUIRED" read-only="true" />  
71             <tx:method name="find*" propagation="REQUIRED" read-only="true" />  
72             <tx:method name="load*" propagation="REQUIRED" read-only="true" />  
73             <tx:method name="*" propagation="REQUIRED" read-only="true" />  
74         </tx:attributes>  
75     </tx:advice>  
76   
77     <!-- 配置事务切面 -->  
78     <aop:config>  
79         <aop:pointcut id="serviceOperation"  
80             expression="execution(* com.hik.service.*.*(..))" />  
81         <aop:advisor advice-ref="txAdvice" pointcut-ref="serviceOperation" />  
82     </aop:config>  
83     
84    
85     
86 </beans>
View Code

同时给出quartz的CronExpression类的源码,可以知道并理解怎样去配置自己想要的定时执行任务方式

   1 /*
   2  * All content copyright Terracotta, Inc., unless otherwise indicated. All rights reserved.
   3  * 
   4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not 
   5  * use this file except in compliance with the License. You may obtain a copy 
   6  * of the License at 
   7  * 
   8  *   http://www.apache.org/licenses/LICENSE-2.0 
   9  *   
  10  * Unless required by applicable law or agreed to in writing, software 
  11  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 
  12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 
  13  * License for the specific language governing permissions and limitations 
  14  * under the License.
  15  * 
  16  */
  17 
  18 package org.quartz;
  19 
  20 import java.io.Serializable;
  21 import java.text.ParseException;
  22 import java.util.Calendar;
  23 import java.util.Date;
  24 import java.util.HashMap;
  25 import java.util.Iterator;
  26 import java.util.Locale;
  27 import java.util.Map;
  28 import java.util.SortedSet;
  29 import java.util.StringTokenizer;
  30 import java.util.TimeZone;
  31 import java.util.TreeSet;
  32 
  33 /**
  34  * Provides a parser and evaluator for unix-like cron expressions. Cron 
  35  * expressions provide the ability to specify complex time combinations such as
  36  * &quot;At 8:00am every Monday through Friday&quot; or &quot;At 1:30am every 
  37  * last Friday of the month&quot;. 
  38  * <P>
  39  * Cron expressions are comprised of 6 required fields and one optional field
  40  * separated by white space. The fields respectively are described as follows:
  41  * 
  42  * <table cellspacing="8">
  43  * <tr>
  44  * <th align="left">Field Name</th>
  45  * <th align="left">&nbsp;</th>
  46  * <th align="left">Allowed Values</th>
  47  * <th align="left">&nbsp;</th>
  48  * <th align="left">Allowed Special Characters</th>
  49  * </tr>
  50  * <tr>
  51  * <td align="left"><code>Seconds</code></td>
  52  * <td align="left">&nbsp;</th>
  53  * <td align="left"><code>0-59</code></td>
  54  * <td align="left">&nbsp;</th>
  55  * <td align="left"><code>, - * /</code></td>
  56  * </tr>
  57  * <tr>
  58  * <td align="left"><code>Minutes</code></td>
  59  * <td align="left">&nbsp;</th>
  60  * <td align="left"><code>0-59</code></td>
  61  * <td align="left">&nbsp;</th>
  62  * <td align="left"><code>, - * /</code></td>
  63  * </tr>
  64  * <tr>
  65  * <td align="left"><code>Hours</code></td>
  66  * <td align="left">&nbsp;</th>
  67  * <td align="left"><code>0-23</code></td>
  68  * <td align="left">&nbsp;</th>
  69  * <td align="left"><code>, - * /</code></td>
  70  * </tr>
  71  * <tr>
  72  * <td align="left"><code>Day-of-month</code></td>
  73  * <td align="left">&nbsp;</th>
  74  * <td align="left"><code>1-31</code></td>
  75  * <td align="left">&nbsp;</th>
  76  * <td align="left"><code>, - * ? / L W</code></td>
  77  * </tr>
  78  * <tr>
  79  * <td align="left"><code>Month</code></td>
  80  * <td align="left">&nbsp;</th>
  81  * <td align="left"><code>1-12 or JAN-DEC</code></td>
  82  * <td align="left">&nbsp;</th>
  83  * <td align="left"><code>, - * /</code></td>
  84  * </tr>
  85  * <tr>
  86  * <td align="left"><code>Day-of-Week</code></td>
  87  * <td align="left">&nbsp;</th>
  88  * <td align="left"><code>1-7 or SUN-SAT</code></td>
  89  * <td align="left">&nbsp;</th>
  90  * <td align="left"><code>, - * ? / L #</code></td>
  91  * </tr>
  92  * <tr>
  93  * <td align="left"><code>Year (Optional)</code></td>
  94  * <td align="left">&nbsp;</th>
  95  * <td align="left"><code>empty, 1970-2199</code></td>
  96  * <td align="left">&nbsp;</th>
  97  * <td align="left"><code>, - * /</code></td>
  98  * </tr>
  99  * </table>
 100  * <P>
 101  * The \'*\' character is used to specify all values. For example, &quot;*&quot; 
 102  * in the minute field means &quot;every minute&quot;.
 103  * <P>
 104  * The \'?\' character is allowed for the day-of-month and day-of-week fields. It
 105  * is used to specify \'no specific value\'. This is useful when you need to
 106  * specify something in one of the two fields, but not the other.
 107  * <P>
 108  * The \'-\' character is used to specify ranges For example &quot;10-12&quot; in
 109  * the hour field means &quot;the hours 10, 11 and 12&quot;.
 110  * <P>
 111  * The \',\' character is used to specify additional values. For example
 112  * &quot;MON,WED,FRI&quot; in the day-of-week field means &quot;the days Monday,
 113  * Wednesday, and Friday&quot;.
 114  * <P>
 115  * The \'/\' character is used to specify increments. For example &quot;0/15&quot;
 116  * in the seconds field means &quot;the seconds 0, 15, 30, and 45&quot;. And 
 117  * &quot;5/15&quot; in the seconds field means &quot;the seconds 5, 20, 35, and
 118  * 50&quot;.  Specifying \'*\' before the  \'/\' is equivalent to specifying 0 is
 119  * the value to start with. Essentially, for each field in the expression, there
 120  * is a set of numbers that can be turned on or off. For seconds and minutes, 
 121  * the numbers range from 0 to 59. For hours 0 to 23, for days of the month 0 to
 122  * 31, and for months 1 to 12. The &quot;/&quot; character simply helps you turn
 123  * on every &quot;nth&quot; value in the given set. Thus &quot;7/6&quot; in the
 124  * month field only turns on month &quot;7&quot;, it does NOT mean every 6th 
 125  * month, please note that subtlety.  
 126  * <P>
 127  * The \'L\' character is allowed for the day-of-month and day-of-week fields.
 128  * This character is short-hand for &quot;last&quot;, but it has different 
 129  * meaning in each of the two fields. For example, the value &quot;L&quot; in 
 130  * the day-of-month field means &quot;the last day of the month&quot; - day 31 
 131  * for January, day 28 for February on non-leap years. If used in the 
 132  * day-of-week field by itself, it simply means &quot;7&quot; or 
 133  * &quot;SAT&quot;. But if used in the day-of-week field after another value, it
 134  * means &quot;the last xxx day of the month&quot; - for example &quot;6L&quot;
 135  * means &quot;the last friday of the month&quot;. You can also specify an offset 
 136  * from the last day of the month, such as "L-3" which would mean the third-to-last 
 137  * day of the calendar month. <i>When using the \'L\' option, it is important not to 
 138  * specify lists, or ranges of values, as you\'ll get confusing/unexpected results.</i>
 139  * <P>
 140  * The \'W\' character is allowed for the day-of-month field.  This character 
 141  * is used to specify the weekday (Monday-Friday) nearest the given day.  As an 
 142  * example, if you were to specify &quot;15W&quot; as the value for the 
 143  * day-of-month field, the meaning is: &quot;the nearest weekday to the 15th of
 144  * the month&quot;. So if the 15th is a Saturday, the trigger will fire on 
 145  * Friday the 14th. If the 15th is a Sunday, the trigger will fire on Monday the
 146  * 16th. If the 15th is a Tuesday, then it will fire on Tuesday the 15th. 
 147  * However if you specify &quot;1W&quot; as the value for day-of-month, and the
 148  * 1st is a Saturday, the trigger will fire on Monday the 3rd, as it will not 
 149  * \'jump\' over the boundary of a month\'s days.  The \'W\' character can only be 
 150  * specified when the day-of-month is a single day, not a range or list of days.
 151  * <P>
 152  * The \'L\' and \'W\' characters can also be combined for the day-of-month 
 153  * expression to yield \'LW\', which translates to &quot;last weekday of the 
 154  * month&quot;.
 155  * <P>
 156  * The \'#\' character is allowed for the day-of-week field. This character is
 157  * used to specify &quot;the nth&quot; XXX day of the month. For example, the 
 158  * value of &quot;6#3&quot; in the day-of-week field means the third Friday of 
 159  * the month (day 6 = Friday and &quot;#3&quot; = the 3rd one in the month). 
 160  * Other examples: &quot;2#1&quot; = the first Monday of the month and 
 161  * &quot;4#5&quot; = the fifth Wednesday of the month. Note that if you specify
 162  * &quot;#5&quot; and there is not 5 of the given day-of-week in the month, then
 163  * no firing will occur that month.  If the \'#\' character is used, there can
 164  * only be one expression in the day-of-week field (&quot;3#1,6#3&quot; is 
 165  * not valid, since there are two expressions).
 166  * <P>
 167  * <!--The \'C\' character is allowed for the day-of-month and day-of-week fields.
 168  * This character is short-hand for "calendar". This means values are
 169  * calculated against the associated calendar, if any. If no calendar is
 170  * associated, then it is equivalent to having an all-inclusive calendar. A
 171  * value of "5C" in the day-of-month field means "the first day included by the
 172  * calendar on or after the 5th". A value of "1C" in the day-of-week field
 173  * means "the first day included by the calendar on or after Sunday".-->
 174  * <P>
 175  * The legal characters and the names of months and days of the week are not
 176  * case sensitive.
 177  * 
 178  * <p>
 179  * <b>NOTES:</b>
 180  * <ul>
 181  * <li>Support for specifying both a day-of-week and a day-of-month value is
 182  * not complete (you\'ll need to use the \'?\' character in one of these fields).
 183  * </li>
 184  * <li>Overflowing ranges is supported - that is, having a larger number on 
 185  * the left hand side than the right. You might do 22-2 to catch 10 o\'clock 
 186  * at night until 2 o\'clock in the morning, or you might have NOV-FEB. It is 
 187  * very important to note that overuse of overflowing ranges creates ranges 
 188  * that don\'t make sense and no effort has been made to determine which 
 189  * interpretation CronExpression chooses. An example would be 
 190  * "0 0 14-6 ? * FRI-MON". </li>
 191  * </ul>
 192  * </p>
 193  * 
 194  * 
 195  * @author Sharada Jambula, James House
 196  * @author Contributions from Mads Henderson
 197  * @author Refactoring from CronTrigger to CronExpression by Aaron Craven
 198  */
 199 public final class CronExpression implements Serializable, Cloneable {
 200 
 201     private static final long serialVersionUID = 12423409423L;
 202     
 203     protected static final int SECOND = 0;
 204     protected static final int MINUTE = 1;
 205     protected static final int HOUR = 2;
 206     protected static final int DAY_OF_MONTH = 3;
 207     protected static final int MONTH = 4;
 208     protected static final int DAY_OF_WEEK = 5;
 209     protected static final int YEAR = 6;
 210     protected static final int ALL_SPEC_INT = 99; // \'*\'
 211     protected static final int NO_SPEC_INT = 98; // \'?\'
 212     protected static final Integer ALL_SPEC = ALL_SPEC_INT;
 213     protected static final Integer NO_SPEC = NO_SPEC_INT;
 214     
 215     protected static final Map<String, Integer> monthMap = new HashMap<String, Integer>(20);
 216     protected static final Map<String, Integer> dayMap = new HashMap<String, Integer>(60);
 217     static {
 218         monthMap.put("JAN", 0);
 219         monthMap.put("FEB", 1);
以上是关于spring注解配置quartz应用的主要内容,如果未能解决你的问题,请参考以下文章

使用spring quartz实现任务调度(注解方式)

使用轻量级Spring @Scheduled注解执行定时任务

Java Spring-Spring与Quartz整合

quartz定时任务实例

spring @Scheduled注解执行定时任务

使用Quartz框架集成Spring,动态配置定时任务(个人思考)