spring frame comes with a timer function

1 ... pom.xml configuration file

 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 Code

. 1  Package com.wsc.core.time;
 2  
. 3  Import java.util.Date;
 . 4  
. 5  / ** 
. 6  * @version 1.0
 . 7  * @ClassName TestTime
 . 8  * @Description the TODO
 . 9  * @author of WSC
 10  * @date 2019 / 11:24 8/18
 . 11  * * / 
12 is  public  class testTime {
 13 is  //     Java simple way to define the timing function 
14      public  void testTime () {
 15          System.out.println ( new new a Date ());
 16      }
17 }

5 ... tomcat start test run results

 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 expression

* / 5 * * * *? Every 5 seconds to perform a
 0 * / 1 * * *? 1 minutes is performed once every
 005-15 * *? 5- daily 15:00 Hourly trigger
 0 0/3 * * *? triggered every three minutes
 00-514 * *? triggered during every minute of every day at 2:00 pm to 2:05 pm 
 0 0/5 14 * *? during the day at 2:00 pm to 2:55 pm the trigger every 5 minutes
 0 0/5 * 14, 18 *? during 2:00 to 2:55 every afternoon and trigger 6:00 pm to 6:55 during every 5 minutes
 0 0/30 * 9-17 *? North Korea every half hour in the nine to five working hours
 00 10,14,16 * *? 10 am every day, 14:00, 4:00 

 0 0 12? * WED represent each point noon on Wednesday 12
 0 0 17? * TUES, THUR, SAT every Tuesday, Thursday and Saturday afternoon Wu Dian
 0 10, 44 14? 3 WED March of each year Wednesday afternoon 2:10 and 2:44 trigger 
 0 15 10? * MON- FRI 10 am Monday to Friday : 15 trigger
 0 0 23 L *? end of every month 23:00 executed once
 0 15 10 L *?10:15 am on the last day of the month triggered 
 0 15 10? * 6L of each month at 10:15 am last Friday triggered 
 01510 * *? 2005 2005 10:15 am every day trigger 
 0 15 10? * 6L 2002-2005 from 2002 to 2005, the last Friday of each month at 10:15 am triggering 
 01510? * 6 # 3 on every third Friday at 10:15 am triggering


 "30 * * * *?" every half minute when the task
 "3010 * * *?" hour 10 minutes and 30 seconds to trigger the task
 "30101 * *?" 1:10 every day 30 seconds to trigger the task
 "3010120 *?" monthly No. 201 point 10 minutes and 30 seconds to trigger the task
 "301012010? *" each year on October 20, 1:10:30 triggered task
 "301012010? 2011" October 2011 20 1:10:30 triggered task
 "30101? 10 * 2011" at 1:10 on the October 2011 day 30 seconds to trigger the task
 "30 10 1? 10 SUN 2011 "October 2011 Sunday 1:10:30 triggered task
 "15,30,45 * * * *?"Every 15 seconds, the task is triggered when 30 seconds 45 seconds
 "15-45 * * * *?" Within 15-45 seconds, triggers per second mission
 "15/5 * * * *?" Start every 15 seconds per minute trigger every 5 seconds to trigger a
 "15-30 / 5 * * * *?" start trigger from 15 seconds to 30 seconds every minute, every 5 seconds to trigger a
 "0 0/3 * * *?" every hours 0 minutes, 0 seconds start, triggered every three minutes
 "0 15 10? * MON- FRI" Monday to Friday 10:15:00 trigger tasks
 "0 15 10 L *?" on the last day of each month the 10:15:00 trigger tasks
 "0 15 10 LW *?" the last working day of each month 10:15:00 trigger tasks
 "0 15 10? * 5L" on the last Thursday of every month 10:00 15 minutes, 0 seconds triggered task
 "01510? * 5 # 3" is the third week of every month 10:15:00 Thursday triggered task

Note:

Class to perform tasks not inherit other super class. This way you can achieve a low version of the spring (4.1.0) combined with the previous version and the version of the high quartz (2.X) use.
If your company uses spring4.1.0 previous version, you do not want to use quartz1.X version, can in this way.

(If you use the high and low version version of spring quartz: error reason is the need to create JobDetailFactoryBean of JobDetailImpl, but not the implementation class under the low version quartz1.X package.)

Guess you like

Origin www.cnblogs.com/wangshichang/p/11372041.html