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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM