为了记录一个完整的定时任务的例子,从零配置到代码。
1.首先,web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>kongfu test</display-name>
<!-- 编码配置 -->
<filter>
<filter-name>spring character encoding filter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>spring character encoding filter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:/spring/application-context.xml</param-value>
</context-param>
<servlet>
<servlet-name>spring web</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:/spring/web-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring web</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
2.加入jar包:pom.xml
<!-- spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<!-- quartz -->
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>2.2.1</version>
</dependency>
3.spring的配置:
(1)web-context.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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx"
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/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"
default-lazy-init="true">
<mvc:annotation-driven/>
<context:component-scan base-package="com.kongfu.business"/>
<!-- 加载properties配置文件 -->
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations" value="classpath:properties/*.properties" />
<property name="fileEncoding" value="UTF-8" />
</bean>
</beans>
(2)application-context.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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx"
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/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<context:component-scan base-package="com.kongfu.business"/>
<!-- 加载properties配置文件 -->
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations" value="classpath:properties/*.properties" />
<property name="fileEncoding" value="UTF-8" />
</bean>
<bean id="messageSource"
class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basenames">
<list>
<value>i18n/error</value>
<value>i18n/message</value>
</list>
</property>
</bean>
<import resource="classpath:spring/application-*-context.xml" />
</beans>
(3)application-quartz-context.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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx"
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/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd" default-lazy-init="false">
<!-- 要调用的工作类 -->
<bean id="abstractJobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean" abstract="true">
<property name="concurrent" value="false" />
<property name="targetMethod" value="handle" />
</bean>
<!-- 定义调用对象和调用对象的方法 -->
<bean id="abstractCronTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean" abstract="true" />
<bean id="returnInvestTrigger" parent="abstractCronTrigger">
<property name="jobDetail">
<bean parent="abstractJobDetail">
<property name="targetObject" ref="overdueScheduleHandler" /><!-- 调用类中的方法 -->
</bean>
</property>
<property name="cronExpression" value="0 0/2 * * * ?" /><!-- 定义触发时间,每两分钟执行一次 -->
</bean>
<!-- 总管理类 (如果将属性 lazy-init='false' 那么容器启动就会执行调度程序) -->
<bean id="scheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="returnInvestTrigger"/>
</list>
</property>
</bean>
</beans>
4.定时任务执行类
AbstractScheduleHandler
public abstract class AbstractScheduleHandler {
protected final Logger logger = LoggerFactory.getLogger(AbstractScheduleHandler.class);
public void handle() throws IllegalAccessException, Exception {
if (logger.isDebugEnabled()) {
logger.debug("start schedule[" + getClass() + "].");
}
doHandle();
if (logger.isDebugEnabled()) {
logger.debug("end schedule[" + getClass() + "].");
}
}
protected abstract void doHandle() throws IllegalAccessException, Exception;
}
OverdueScheduleHandler
/**
* 定时任务
*
* @author CNkongfu
* @date 2017年2月13日
*/
@Component
public class OverdueScheduleHandler extends AbstractScheduleHandler {
@Override
protected void doHandle() throws IllegalAccessException, Exception {
quartz1();
}
private void quartz1() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:SS");
System.out.println("this is a quartz " + sdf.format(new Date()));
}
}
执行结果