quartz.xml的配置java
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" xmlns:mvc="http://www.springframework.org/schema/mvc" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" 6 xmlns:task="http://www.springframework.org/schema/task" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" 7 xsi:schemaLocation="http://www.springframework.org/schema/beans 8 http://www.springframework.org/schema/beans/spring-beans.xsd 9 http://www.springframework.org/schema/mvc 10 http://www.springframework.org/schema/mvc/spring-mvc.xsd 11 http://www.springframework.org/schema/context 12 http://www.springframework.org/schema/context/spring-context.xsd 13 http://www.springframework.org/schema/aop 14 http://www.springframework.org/schema/aop/spring-aop.xsd 15 http://www.springframework.org/schema/tx 16 http://www.springframework.org/schema/tx/spring-tx.xsd 17 http://www.springframework.org/schema/task 18 http://www.springframework.org/schema/task/spring-task.xsd 19 http://code.alibabatech.com/schema/dubbo 20 http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> 21 22 <!-- <task:scheduler id="scheduler" pool-size="4" /> 23 <task:executor id="executor" pool-size="4-8" 24 queue-capacity="10" rejection-policy="DISCARD_OLDEST" keep-alive="300" /> 25 <task:annotation-driven executor="executor" 26 scheduler="scheduler" /> --> 27 28 <!-- 1 定義一個任務類 :job--> 29 <bean id="personInfoTask" class="com.ideal.task.PersonInfoTask"></bean> 30 <bean id="accessRecordTask" class="com.ideal.task.AccessRecordTask"></bean> 31 <!-- 2 任務類描述 --> 32 <bean id="personInfoJobDetails" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> 33 <!-- 定義目標對象 --> 34 <property name="targetObject" ref="personInfoTask"></property> 35 <!-- 定義目標方法 --> 36 <property name="targetMethod" value="personInfo"></property> 37 <property name="concurrent" value="false"/> 38 </bean> 39 <bean id="accessRecordJobDetails" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> 40 <!-- 定義目標對象 --> 41 <property name="targetObject" ref="accessRecordTask"></property> 42 <!-- 定義目標方法 --> 43 <property name="targetMethod" value="accessRecord"></property> 44 <property name="concurrent" value="false"/> 45 </bean> 46 <!-- 3 觸發器 --> 47 <bean id="personInfoJobTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean"> 48 <!-- 執行那個任務 --> 49 <property name="jobDetail" ref="personInfoJobDetails"></property> 50 <!-- 定義合適執行execute方法。表達式,每一天執行一次 --> 51 <property name="cronExpression" value="0 * * */1 * ?"></property> 52 </bean> 53 <bean id="accessRecordJobTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean"> 54 <!-- 執行那個任務 --> 55 <property name="jobDetail" ref="accessRecordJobDetails"></property> 56 <!-- 定義合適執行execute方法。表達式,每5分鐘執行一次 --> 57 <property name="cronExpression" value="0 */5 * * * ?"></property> 58 </bean> 59 <!-- 4 總管理容器:入口 --> 60 <bean id="startQuertz" class="org.springframework.scheduling.quartz.SchedulerFactoryBean" destroy-method="destroy"> 61 <!--必須的,QuartzScheduler延時啓動,應用啓動完後QuartzScheduler再啓動--> 62 <property name="startupDelay" value="5"/> 63 <!--設置自動啓動--> 64 <property name="autoStartup" value="true"/> 65 <property name="triggers"> 66 <list> 67 <ref bean="personInfoJobTrigger"/> 68 <ref bean="accessRecordJobTrigger"/> 69 <!-- 配置多個觸發器 --> 70 </list> 71 </property> 72 </bean> 73 74 </beans>
檢查了quartz的配置沒什麼問題,可是仍是執行了兩次,很奇怪web
查了不少資料,大多數spring加載quartz的時候加載了兩次spring
寫了下面監聽類也沒有起做用spring-mvc
1 package com.ideal.listener; 2 3 import javax.servlet.ServletContextEvent; 4 import javax.servlet.ServletContextListener; 5 6 import org.quartz.impl.StdScheduler; 7 import org.springframework.web.context.WebApplicationContext; 8 9 public class QuartzContextListener implements ServletContextListener { 10 11 /* 12 * 測試代碼寫得隨便 13 * 14 * @seejavax.servlet.ServletContextListener#contextDestroyed(javax.servlet. 15 * ServletContextEvent) 16 */ 17 @Override 18 public void contextDestroyed(ServletContextEvent arg0) { 19 WebApplicationContext webApplicationContext = (WebApplicationContext) arg0 20 .getServletContext() 21 .getAttribute( 22 WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE); 23 StdScheduler personInfoTask = (StdScheduler) webApplicationContext 24 .getBean("personInfoTask"); 25 StdScheduler accessRecordTask = (StdScheduler) webApplicationContext 26 .getBean("accessRecordTask"); 27 if(personInfoTask != null) { 28 personInfoTask.shutdown(); 29 } 30 if(accessRecordTask != null) { 31 accessRecordTask.shutdown(); 32 } 33 try { 34 Thread.sleep(1000); 35 } catch (InterruptedException e) { 36 e.printStackTrace(); 37 } 38 } 39 }
最後在tomcat的server.xml進行配置tomcat
<Host appBase="webapps" name="localhost" unpackWARs="true" autoDeploy="false" deployOnStartup="false">mvc
autoDeploy="false" deployOnStartup="false" 這兩個屬性改成false 暫時解決了,可是總認爲這種解決方案不是長久的,由於更改了tomcat的配置app
但願在項目裏面進行配置去探索解決方案。。。。。。。webapp