功能描述:在开发程序中很多时候会出现希望在某个时间点、一段时间后执行某个动作,此时即需要实现定时功能,也就是希望程序能够自己进行监督,从而达到在希望的时间触发相应的事件。
一、在maven中加入Quartz的依赖
<dependencies>
<!--定时调度-->
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>2.2.2</version>
</dependency>
</dependencies>
二、在web.xml文件中集成quartz的配置文件
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/j2ee/web-app_3_0.xsd">
<display-name>TestTime Web Application</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/spring-quartz.xml
</param-value>
</context-param>
<!-- Servlet Filters ================================================ -->
<!-- 编码过滤器 -->
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!--
- Declare Spring context listener which sets up the Spring Application Context
- containing all Cocoon components (and user defined beans as well).
-->
<!-- Spring监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 防止Spring内存溢出监听器
<listener>
<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
</listener>-->
<!--
- Declare Spring request listener which sets up the required RequestAttributes
- to support Springs and Cocoon custom bean scopes like the request scope or the
- session scope.
-->
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
<!-- Servlet Configuration ========================================== -->
<servlet>
<display-name>TestTimeDispatcherServlet</display-name>
<servlet-name>testTime-job-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- URL space mappings ============================================= -->
<servlet-mapping>
<servlet-name>testTime-job-dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- 默认首页 -->
<welcome-file-list>
<welcome-file>/index.jsp</welcome-file>
</welcome-file-list>
</web-app>
三、配置spring-quartz.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<description>Quartz Job Setting</description>
<!-- 新建每个需要的配置-->
<import resource="quartz/testPrint.xml"></import>
<import resource="quartz/print.xml"></import>
<!-- D.Quartz的调度工厂,调度工厂只能有一个,多个调度任务在list中添加 -->
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<!-- 所有的调度列表-->
<ref bean="testTrigger"/>
<ref bean="printTrigger"/>
</list>
</property>
</bean>
</beans>
四、配置print.xml文件
concurrent表示:对于相同的JobDetail,当指定多个Trigger时, 很可能第一个job完成之前,第二个job就开始了。当concurrent设为false,多个job不会并发运行,第二个job将不会在第一个job完成之前开始。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- A.配置调度的任务对应bean的id和自定义class-->
<bean id="print" class="com.testTime.job.schedule.test.MyPrintJob"/>
<!-- B.配置调度任务对应的bean的id和执行的方法,作业不并发调度-->
<bean id="printDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject" ref="print"/>
<property name="targetMethod" value="print"/>
<property name="concurrent" value="false"/>
</bean>
<!-- C.配置调度任务执行的触发的时间-->
<bean id="printTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail" ref="printDetail"/>
<property name="cronExpression">
<!-- 秒 分 时 月的某天 月 星期的某天 年 除了年都是必须值 -->
<!-- 每3秒执行任务调度 -->
<value>0/3 * * * * ?</value>
</property>
</bean>
</beans>
下图是cronExpression的具体意义。
/**
* ----------------------------
* 【位置说明】:
* 1 - 秒(0–59)
* 2 - 分(0–59)
* 3 - 时(0–23)
* 4 - 日(1–31)
* 5 - 月(1–12)
* 6 - 星期(SUN–SAT or 1–7)
* 7 - 年(可选, 1970–2099)
* --------------------------------
* 【符号说明】
* 0 0 12 * * ?---------------在每天中午12:00触发
* 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:00至2:59之间每分钟触发一次
* 0 0/5 14 * * ?---------------每天在下午2:00至2:59之间每5分钟触发一次
* 0 0/5 14,18 * * ?---------------每天在下午2:00至2:59和6:00至6:59之间的每5分钟触发一次
* 0 0-5 14 * * ?---------------每天在下午2:00至2:05之间每分钟触发一次
* 0 10,44 14 ? 3 WED---------------每三月份的星期三在下午2:00和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, 2003, 2004 and 2005年的每个月的最后一个星期五的上午10:15触发
* 0 15 10 ? * 6#3---------------在每个月的第三个星期五的上午10:15触发
* 0 0 12 1/5 * ?---------------从每月的第一天起每过5天的中午12:00时触发
* 0 11 11 11 11 ?---------------在每个11月11日的上午11:11时触发
* @return
*/
四、具体任务类和任务方法的实现
package com.testTime.job.schedule.test;
import com.testTime.job.utils.DateFormatUtils;
import com.testTime.job.utils.LogUtils;
import com.testTime.job.utils.date.DateUtils;
public class MyPrintJob {
public void print(){
Integer current = DateUtils.getCurrentTimestamp();
LogUtils.trace(DateFormatUtils.format(current,"yyyy-MM-dd"));
}
}