Spring整合定时框架Quartz

1.

创建chuangjiaapplicationContext-quartz.xml,把下面的代码复制进去(注:改几个小地方)

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

<!-- 配置MyJob类 -->

  <bean id="myJob" class="cn.itcast.jk.action.quarte.QuarteAction"></bean>(注:这个路径就是你的定时类的路径)


<!-- 配置JobDetail,目的:让job执行的 -->
<bean id="jobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<!-- 注入目标对象 -->
<property name="targetObject" ref="myJob"/>(注:这个是上面的ID,引入你的定时类)
<!-- 注入目标方法 -->
<property name="targetMethod" value="test1"/>(注:这个就是你定时类里面的方法名)
</bean>


<!-- 配置Trigger,配置时间表达式 -->
<bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail" ref="jobDetail"/>
<!-- 时间表达式  秒 分 时 日 月 星期 年 
表达式的格式:秒  分  时  日  月  星期  年 
日和星期可以用?来表示(不能同时用)
年是可以省略不写,省略不写表示每一年都执行
0/10      每过10秒发一次
2,5,9   表示在2,5,9时都会触发定时器trigger
2-8    表示在2到8之间都会触发
-->
<property name="cronExpression" value="0 0 3 * * ? *"/>

</bean>


      <!-- 配置时间表达式,间隔时间执行 -->
      <bean id="simpleTrigger"
          class="org.springframework.scheduling.quartz.SimpleTriggerFactoryBean">
        <property name="jobDetail" ref="jobDetail" />
        <!-- 服务启动多久后,启动时间,单位ms  -->
        <property name="startDelay" value="0" />
        <!-- 重复间隔时间,单位ms  -->
        <property name="repeatInterval" value="5000" />
      </bean>


<!-- 以上的可以配置多个 -->
<!-- 配置的是和谐的调度容器 -->
<bean id="scheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>

<ref bean="cronTrigger"/>

                                <ref bean="simpleTrigger"/>

                                <!-- 直接在这引入多个就可以了 -->

</list>
</property>
</bean>

</beans>


2.

applicationContext.xml 中引入applicationContext-quartz.xml加下面一段话

    <import resource="applicationContext-quartz.xml"/> 


3.

pom.xml中加入的jar包

            <!-- 整合定时 -->
            <dependency>
                  <groupId>org.springframework</groupId>
                  <artifactId>spring-context-support</artifactId>
                  <version>${spring.version}</version>
            </dependency>
            <dependency>
                  <groupId>org.quartz-scheduler</groupId>
                  <artifactId>quartz</artifactId>
                  <version>2.2.1</version> 
            </dependency>
            <dependency>
                  <groupId>org.quartz-scheduler</groupId>
                  <artifactId>quartz-jobs</artifactId>
                  <version>2.2.1</version> 
            </dependenc



cron表达式的基本使用: 

  
字段 允许值 允许的特殊字符 
秒 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点到早上8点之间每两个小时,早上八点 
0 23-7/2,8 * * * 
每个月的4号和每个礼拜的礼拜一到礼拜三的早上11点 
0 11 4 * 1-3 
1月1日早上4点 
0 4 1 1 *

猜你喜欢

转载自blog.csdn.net/potatoandpotato/article/details/80266367