JavaWEB springmvc 使用定时任务

1.配置web.xml

  在web.xml配置使用springmvc框架,其他配置略。

 1     <display-name>xxx.com</display-name>
 2     
 3     <!-- 配置Spring MVC DispatcherServlet -->
 4     <servlet>
 5         <servlet-name>MVC DispatcherServlet</servlet-name>
 6         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
 7         <!-- 初始化参数 -->
 8         <init-param>
 9             <!-- 加载SpringMVC的xml到 spring的上下文容器中 -->
10             <param-name>contextConfigLocation</param-name>
11             <!-- 下面这个参数是指定springmvc的配置文件所在 -->
12             <param-value>classpath:com/jieli/config/springmvc-context.xml</param-value>
13         </init-param>
14         <load-on-startup>1</load-on-startup>
15     </servlet>
16 
17     <!-- 配置DispatcherServlet所需要拦截的 url -->
18     <servlet-mapping>
19         <servlet-name>MVC DispatcherServlet</servlet-name>
20         <!-- <url-pattern>/</url-pattern> 这样配置的话,所有页面都会进入拦截器 
21              这个在springmvc 的配置文件里还有对其进行再次配置-->
22         <url-pattern>/</url-pattern>
23     </servlet-mapping>

2.配置springmvc-context.xml

  根据web.xml里面的指定配置springmvc 核心代码在第18-19行和第85-92行进行配置。

 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   xmlns:p="http://www.springframework.org/schema/p" 
 5   xmlns:context="http://www.springframework.org/schema/context"
 6   xmlns:util="http://www.springframework.org/schema/util"
 7   xmlns:mvc="http://www.springframework.org/schema/mvc"
 8   xmlns:task="http://www.springframework.org/schema/task"
 9   xsi:schemaLocation="
10   http://www.springframework.org/schema/beans
11   http://www.springframework.org/schema/beans/spring-beans.xsd
12   http://www.springframework.org/schema/util
13   http://www.springframework.org/schema/util/spring-util.xsd
14   http://www.springframework.org/schema/context 
15   http://www.springframework.org/schema/context/spring-context.xsd
16   http://www.springframework.org/schema/mvc
17   http://www.springframework.org/schema/mvc/spring-mvc.xsd
18   http://www.springframework.org/schema/task
19   http://www.springframework.org/schema/task/spring-task-3.0.xsd" >
20 
21     <!-- ==============基础配置 开始============= -->
22     <!-- 开启controller注解支持 -->
23     <!-- use-default-filters="false" 只扫描指定的注解 -->
24     <context:component-scan base-package="com.jieli.controller" use-default-filters="false">
25         <context:include-filter type="annotation"
26             expression="org.springframework.stereotype.Controller" />
27     </context:component-scan>
28     
29     <!-- 视图解析器 -->
30     <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
31        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
32        <property name="contentType" value="text/html"/>        
33        <property name="prefix" value="/WEB-INF/views/"/>
34        <property name="suffix" value=".jsp"/>
35     </bean>
36     
37     <!-- 如果当前请求为"/"时,则转发到"index" -->
38     <mvc:view-controller path="/" view-name="forward:index"/> 
39     
40     <!-- 默认的注解映射的支持 -->  
41     <mvc:annotation-driven />
42 
43     <!-- 静态资源映射 不经过controller-->
44     <mvc:resources mapping="/js/**" location="/WEB-INF/js/" />
45     <mvc:resources mapping="/css/**" location="/WEB-INF/css/" />
46     <mvc:resources mapping="/fonts/**" location="/WEB-INF/fonts/" />
47     <mvc:resources mapping="/plugins/**" location="/WEB-INF/plugins/" />
48     <mvc:resources mapping="/images/**" location="/WEB-INF/images/" />
49     <mvc:resources mapping="/html/**" location="/WEB-INF/html/" />
50     <mvc:resources mapping="/jsp/**" location="/WEB-INF/jsp/" />
51     <!-- 当上面要访问的静态资源不包括在上面的配置中时,则根据此配置来访问 -->
52     <mvc:default-servlet-handler/>
53     
54     <!-- 自定义拦截器 准备用于权限管理 -->
55 <!--     <mvc:interceptors>    -->
56         <!-- 如果不定义 mvc:mapping path 将拦截所有的URL请求 -->
57 <!--         <bean class="com.demo.web.auth.AuthInterceptor"></bean> -->
58 <!--         <mvc:interceptor> -->
59 <!--             <mvc:mapping path="/secure/*"/> -->
60 <!--             <bean class="*****.***Interceptor"></bean> -->
61 <!--         </mvc:interceptor> -->
62 <!--     </mvc:interceptors> -->
63     
64     <!-- ==============基础配置 结束============= -->
65     
66     
67     <!-- =========下面的配置是一些插件化配置====== -->
68     <!-- 支持上传文件 -->  
69     <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
70         <!-- 设置上传文件的最大尺寸为1MB -->
71         <property name="maxUploadSize">
72             <value>2048576</value>
73         </property>
74         <property name="defaultEncoding">
75             <value>UTF-8</value>
76         </property>
77     </bean>
78     
79     <!-- 权限控制, 作用是让所有的请求动通过拦截器 -->
80     <mvc:interceptors>   
81         <!-- 如果不定义 mvc:mapping path 将拦截所有的URL请求 -->
82         <bean class="com.jieli.interceptor.AuthInterceptor"></bean>
83     </mvc:interceptors>
84 
85     <!-- 增加定时任务插件 -->
86     <context:annotation-config></context:annotation-config>
87     <!-- spring 扫描注解配置 -->
88     <context:component-scan base-package="com.jieli.plugins.timetask">
89     </context:component-scan>
90     <!-- 开启这个配置 spring才能识别@Scheduled注解 -->
91     <task:annotation-driven scheduler="qbScheduler" mode="proxy"/>
92     <task:scheduler id="qbScheduler" pool-size="10"/>
93 
94     <!-- 增加邮件 -->
95 </beans>

3.使用注解方式

 1 package com.jieli.plugins.timetask;
 2 
 3 
 4 import org.springframework.scheduling.annotation.Scheduled;
 5 import org.springframework.stereotype.Component;
 6 
 7 
 8 @Component("taskJob")
 9 public class TestEverySecond {
10     @Scheduled(cron = "1/5 * * * * ?")
11     public void testTask(){
12         System.out.println(System.currentTimeMillis());
13     }
14 }

  这样就可以了,就会每5秒进行一次任务。一般做一些定时通知,定时备份,定时清理等任务。

4.出现问题及解决

  这样配置完成后,发现启动服务后,这个testTask任务,都是执行两次的。查看tomcat打印的日志,发现定时任务的启动两次的,或者说整个web服务是启动两次的。以前没有这个定时任务,由于启动时日志太多,也没有报错,所以一直没有太注意。web容器启动了两次服务,就产生了两个定时器实例。经上网查询,是有两种可能,一个是spring配置出错,一个是tomcat配置出错。关于配置出错,请参看这篇文章: http://blog.csdn.net/chaijunkun/article/details/6925889 。

  我是tomcat配置出错。具体出现问题如下:

  默认tomcat配置文件conf/server.xml 里面Host段是这个样子的

1       <Host name="localhost"  appBase="webapps"
2             unpackWARs="true" autoDeploy="true"
3             xmlValidation="false" xmlNamespaceAware="false">
4       </Host>

  我们把在eclipse下的项目部署到webapps文件目录下后,启动服务后,在浏览器中访问的地址是 Http://127.0.0.1:8080/<项目名称>/  这样子进行访问。当我们不要项目名称作为访问路径,想通过ip、端口直接进行访问时,配置如下:

1       <Host name="localhost"  appBase="webapps"
2             unpackWARs="true" autoDeploy="true"
3             xmlValidation="false" xmlNamespaceAware="false">
4 
5         <Context path="" docBase="/JieLiERP" debug="0" reloadable="true" />
6 
7       </Host>

  增加context段,进行配置。就这样一直无事,直到最近增加定时任务功能,才发现原来这样配置会出现项目启动两次的情况。也是导致定时任务被调用两次的原因。我的解决办法是 配置如下:

1       <Host name="localhost"  appBase=""
2             unpackWARs="true" autoDeploy="true"
3             xmlValidation="false" xmlNamespaceAware="false">
4 
5         <Context path="" docBase="/webapps/JieLiERP" debug="0" reloadable="true" />
6 
7       </Host>

  就这样解决问题。 还有Context段,tomcat 6.0 和 tomcat 7.0及以上的配置有点区别

1 <!-- tomcat 6.0 -->
2 <Context path="" docBase="/webapps/JieLiERP" debug="0" reloadable="true" />
3 
4 <!-- tomcat 7.0及以上 -->
5 <Context path="/" docBase="webapps/JieLiERP" debug="0" reloadable="true" />

  本文地址: http://www.cnblogs.com/wunaozai/p/5026765.html

原文地址:https://www.cnblogs.com/wunaozai/p/5026765.html