本以为spring定时任务挺简单的,后来发现单纯的进行文件的配置有的时候定时任务并没有执行,这是什么原因呢?
通过看spring指导文档上的讲解,以及通过对文档的理解做了一个Demo看一下,再说明这个问题。
1、demo的目录结构
说明:图中显示的jar包都是需要的,亲测缺少后会报错
2、需要执行的任务类(这里是没有使用注解的,如果理解了下面的配置我觉得注解会更容易)
package com.schedule.task;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
*
* 需要执行的任务
*
* */
public class MyJob{
/**
* @param 空
* @return void
* 执行方法打印系统当前时间
* */
public void doIt(){
SimpleDateFormat myFmt=new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");
Date now=new Date();
System.out.println(myFmt.format(now));
}
}
3、web.xml中的配置
<?xml version="1.0" encoding="UTF-8"?>
<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/javaee/web-app_3_0.xsd">
<display-name></display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>timer</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:timer-servlet.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>timer</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
4、spring配置文件 applicationContext.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:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd"
default-lazy-init="false">
<bean id="myJob" class="com.schedule.task.MyJob"></bean>
<bean id="jobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject" ref="myJob"/>
<property name="targetMethod" value="doIt"/>
<property name="concurrent" value="false"/>
</bean>
<bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail" ref="jobDetail"/>
<property name="cronExpression" value="*/2 * * * * ?"/>
</bean>
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="cronTrigger"/>
</list>
</property>
</bean>
</beans>
timer-servlet.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:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-4.1.xsd"
default-lazy-init="false">
</beans>
5、现在将demo部署到tomcat,并启动tomcat,查看结果
6、在做的过程中遇到的问题:启动之后,定时任务不执行
(1)猜测是有关的类没有找到,当ctrl+鼠标左键点击下面这段代码的
org.springframework.scheduling.quartz.CronTriggerBean时,并没有定位到源码,后来发现spring-4.1.6中已经没有这个类了,spring指导文档已经将其换成
org.springframework.scheduling.quartz.CronTriggerFactoryBean
<bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail" ref="jobDetail"/>
<property name="cronExpression" value="*/2 * * * * ?"/>
</bean>
(2)原来定时任务的配置是在 timer-servlet.xml文件中的,发现还是没有执行,后来将定时任务配置改到applicationContext.xml 中定时任务执行了,感觉这个定时任务需要有一个类似于监听器的类,即web.xml中配置的
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
看来还是应该注重细节。
写的不好,主要让自己认识到自己的错误,以此为鉴,也希望给大家提供一些参考。