zoukankan      html  css  js  c++  java
  • spring 框架自带的定时器功能

    1...pom.xml 文件配置

     1  <dependencies>
     2     <dependency>
     3       <groupId>junit</groupId>
     4       <artifactId>junit</artifactId>
     5       <version>4.11</version>
     6       <scope>test</scope>
     7     </dependency>
     8       <dependency>
     9           <groupId>org.quartz-scheduler</groupId>
    10           <artifactId>quartz</artifactId>
    11           <version>1.8.5</version>
    12       </dependency>
    13       <dependency>
    14           <groupId>org.springframework</groupId>
    15           <artifactId>spring-context-support</artifactId>
    16           <version>3.2.9.RELEASE</version>
    17       </dependency>
    18       <dependency>
    19           <groupId>org.springframework</groupId>
    20           <artifactId>spring-core</artifactId>
    21           <version>3.2.9.RELEASE</version>
    22       </dependency>
    23       <dependency>
    24           <groupId>org.springframework</groupId>
    25           <artifactId>spring-webmvc</artifactId>
    26           <version>3.2.9.RELEASE</version>
    27       </dependency>
    28 <!--      事务管理 必须引入-->
    29       <dependency>
    30           <groupId>org.springframework</groupId>
    31           <artifactId>spring-tx</artifactId>
    32           <version>3.2.9.RELEASE</version>
    33       </dependency>
    34   </dependencies>

    2...applicationContext.xml

     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        xsi:schemaLocation="http://www.springframework.org/schema/beans
     5        http://www.springframework.org/schema/beans/spring-beans.xsd">
     6 <!--创建的java 类  方法 -->
     7     <bean id="testTime" class="com.wsc.core.time.TestTime"></bean>
     8     <bean id="jobDetailFactoryBean" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
     9 <!--      目标 java类名-->
    10         <property name="targetObject" ref="testTime"></property>
    11 <!--     目标 java方法名-->
    12         <property name="targetMethod" value="testTime"></property>
    13     </bean>
    14     <bean id="triggerFactoryBean" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
    15         <property name="jobDetail" ref="jobDetailFactoryBean"></property>
    16 <!--      cron 表达式-->
    17         <property name="cronExpression" value="0/5 * * * * ?"></property>
    18     </bean>
    19     <bean id="schedulerFactoryBean" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
    20         <property name="triggers" >
    21             <list>
    22                 <ref bean="triggerFactoryBean"></ref>
    23             </list>
    24         </property>
    25     </bean>
    26 </beans>

    3...web.xml 

     1 <!DOCTYPE web-app PUBLIC
     2  "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
     3  "http://java.sun.com/dtd/web-app_2_3.dtd" >
     4 
     5 <web-app>
     6   <display-name>Archetype Created Web Application</display-name>
     7 <!--    引入 spring 配置文件-->
     8     <context-param>
     9         <param-name>contextConfigLocation</param-name>
    10         <param-value>classpath:applicationContext.xml</param-value>
    11     </context-param>
    12 <!--    监听器-->
    13     <listener>
    14         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    15     </listener>
    16 
    17 </web-app>

    4...java 代码

     1 package com.wsc.core.time;
     2 
     3 import java.util.Date;
     4 
     5 /**
     6  * @version 1.0
     7  * @ClassName TestTime
     8  * @Description TODO
     9  * @Author WSC
    10  * @Date 2019/8/18 11:24
    11  **/
    12 public class TestTime {
    13 //    java 简单的方法  定义定时功能
    14     public void testTime(){
    15         System.out.println(new Date());
    16     }
    17 }

    5...启动tomcat 测试 运行结果

     1 Sun Aug 18 12:06:30 CST 2019
     2 Sun Aug 18 12:06:35 CST 2019
     3 Sun Aug 18 12:06:40 CST 2019
     4 Sun Aug 18 12:06:45 CST 2019
     5 Sun Aug 18 12:06:50 CST 2019
     6 Sun Aug 18 12:06:55 CST 2019
     7 Sun Aug 18 12:07:00 CST 2019
     8 Sun Aug 18 12:07:05 CST 2019
     9 Sun Aug 18 12:07:10 CST 2019
    10 Sun Aug 18 12:07:15 CST 2019

    6...cron 表达式

    */5 * * * * ? 每隔5秒执行一次
    0 */1 * * * ? 每隔1分钟执行一次
    0 0 5-15 * * ? 每天5-15点整点触发
    0 0/3 * * * ? 每三分钟触发一次
    0 0-5 14 * * ? 在每天下午2点到下午2:05期间的每1分钟触发 
    0 0/5 14 * * ? 在每天下午2点到下午2:55期间的每5分钟触发
    0 0/5 14,18 * * ? 在每天下午2点到2:55期间和下午6点到6:55期间的每5分钟触发
    0 0/30 9-17 * * ? 朝九晚五工作时间内每半小时
    0 0 10,14,16 * * ? 每天上午10点,下午2点,4点 
    
    0 0 12 ? * WED 表示每个星期三中午12点
    0 0 17 ? * TUES,THUR,SAT 每周二、四、六下午五点
    0 10,44 14 ? 3 WED 每年三月的星期三的下午2:10和2:44触发 
    0 15 10 ? * MON-FRI 周一至周五的上午10:15触发
    0 0 23 L * ? 每月最后一天23点执行一次
    0 15 10 L * ? 每月最后一日的上午10:15触发 
    0 15 10 ? * 6L 每月的最后一个星期五上午10:15触发 
    0 15 10 * * ? 2005 2005年的每天上午10:15触发 
    0 15 10 ? * 6L 2002-2005 2002年至2005年的每月的最后一个星期五上午10:15触发 
    0 15 10 ? * 6#3 每月的第三个星期五上午10:15触发
    
    
    "30 * * * * ?" 每半分钟触发任务
    "30 10 * * * ?" 每小时的10分30秒触发任务
    "30 10 1 * * ?" 每天1点10分30秒触发任务
    "30 10 1 20 * ?" 每月20号1点10分30秒触发任务
    "30 10 1 20 10 ? *" 每年10月20号1点10分30秒触发任务
    "30 10 1 20 10 ? 2011" 2011年10月20号1点10分30秒触发任务
    "30 10 1 ? 10 * 2011" 2011年10月每天1点10分30秒触发任务
    "30 10 1 ? 10 SUN 2011" 2011年10月每周日1点10分30秒触发任务
    "15,30,45 * * * * ?" 每15秒,30秒,45秒时触发任务
    "15-45 * * * * ?" 15到45秒内,每秒都触发任务
    "15/5 * * * * ?" 每分钟的每15秒开始触发,每隔5秒触发一次
    "15-30/5 * * * * ?" 每分钟的15秒到30秒之间开始触发,每隔5秒触发一次
    "0 0/3 * * * ?" 每小时的第0分0秒开始,每三分钟触发一次
    "0 15 10 ? * MON-FRI" 星期一到星期五的10点15分0秒触发任务
    "0 15 10 L * ?" 每个月最后一天的10点15分0秒触发任务
    "0 15 10 LW * ?" 每个月最后一个工作日的10点15分0秒触发任务
    "0 15 10 ? * 5L" 每个月最后一个星期四的10点15分0秒触发任务
    "0 15 10 ? * 5#3" 每个月第三周的星期四的10点15分0秒触发任务

    注:

    执行任务的类不用继承其他超级类。这种方式可以实现低版本的spring(4.1.0)以前版本和高版本quartz(2.X)的结合使用。
    如果公司使用spring4.1.0以前版本,又不想使用quartz1.X版本, 可以通过这种方式。

    (如果使用高版本spring和低版本quartz:报错。原因是 JobDetailFactoryBean的创建需要JobDetailImpl, 而低版本quartz1.X包下没有该实现类。)

  • 相关阅读:
    attr 修改IMG src
    64转2
    :eq
    DOM0和D0M2级事件
    c's's透明度
    span标签 宽度无效解决方案
    CSS 点击事件
    input点击后的 默认边框去除
    js
    CSS 的 ID 和 Class 有什么区别,如何正确使用它们。
  • 原文地址:https://www.cnblogs.com/wangshichang/p/11372041.html
Copyright © 2011-2022 走看看