Quartz + Spring 启动时双重执行 [英] Quartz + Spring double execution on startup

查看:86
本文介绍了Quartz + Spring 启动时双重执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 Quartz 2.2.1 和 Spring 3.2.2.Eclipse Juno 上的应用程序

I have Quartz 2.2.1 and Spring 3.2.2. app on Eclipse Juno

这是我的 bean 配置:

This is my bean configuration:

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

<!-- Spring Quartz -->

<bean id="checkAndRouteDocumentsTask" class="net.tce.task.support.CheckAndRouteDocumentsTask" />

<bean name="checkAndRouteDocumentsJob" class="org.springframework.scheduling.quartz.JobDetailFactoryBean">

    <property name="jobClass" value="net.tce.task.support.CheckAndRouteDocumentsJob" />
    <property name="jobDataAsMap">
        <map>
            <entry key="checkAndRouteDocumentsTask" value-ref="checkAndRouteDocumentsTask" />
        </map>
    </property>
    <property name="durability" value="true" />

</bean>

<!-- Simple Trigger, run every 30 seconds -->
<bean id="checkAndRouteDocumentsTaskTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerFactoryBean">

    <property name="jobDetail" ref="checkAndRouteDocumentsJob" />
    <property name="repeatInterval" value="30000" />
    <property name="startDelay" value="15000" />

</bean>

<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
    <property name="jobDetails">
        <list>
            <ref bean="checkAndRouteDocumentsJob" />
        </list>
    </property>

    <property name="triggers">
        <list>
            <ref bean="checkAndRouteDocumentsTaskTrigger" />
        </list>
    </property>
</bean>

我的 mvc spring servlet 配置:

My mvc spring servlet config:

    <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans     
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">  
    <bean id="propertyConfigurer" 
     class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    </bean>
    <mvc:annotation-driven />
    <context:annotation-config />
    <context:component-scan base-package="net.tce" />
    <import resource="spring-quartz.xml"/>  
</beans>

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_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>OperationalTCE</display-name>
  <servlet>
    <servlet-name>mvc-dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>0</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>mvc-dispatcher</servlet-name>
    <url-pattern>/rest/*</url-pattern>
  </servlet-mapping>
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value>
  </context-param>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
</web-app>

我的问题是总是在启动我的应用程序时,Quartz 同时创建两个作业.我的工作必须每 30 秒执行一次:

My problem is that always when startup my application, Quartz creates two jobs at the same time. My job must be execute every 30 seconds:

INFO: Starting TASK on Mon Nov 04 15:36:46 CST 2013...
INFO: Starting TASK on Mon Nov 04 15:36:46 CST 2013...
INFO: Starting TASK on Mon Nov 04 15:37:16 CST 2013...
INFO: Starting TASK on Mon Nov 04 15:37:16 CST 2013...
INFO: Starting TASK on Mon Nov 04 15:37:46 CST 2013...
INFO: Starting TASK on Mon Nov 04 15:37:46 CST 2013...

感谢您的帮助.

推荐答案

你的 ContextLoaderListenerDispatcherServlet 都在加载 mvc-dispatcher-servlet.xml.基本上复制所有 bean 导致 2 次执行,因为它也被复制.

Your ContextLoaderListener and DispatcherServlet are both loading the mvc-dispatcher-servlet.xml. Basically duplicating all your beans resulting in 2 executions as that is also duplicated.

将您的配置拆分为由 ContextLoaderListener(包含您的服务、dao、计时器等)加载的配置和由 DispatcherServlet 加载的配置(仅包含 web相关的 bean 控制器、视图解析器等).

Split your configuration into one that is being loaded by the ContextLoaderListener (containing your services, dao, timers etc.) and one loaded by the DispatcherServlet (containing only web related beans controllers, view resovlers etc.).

或者完全放弃ContextLoaderListener,只使用DispatcherServlet 加载所有内容.

Or ditch the ContextLoaderListener altogether and only use the DispatcherServlet to load everything.

这篇关于Quartz + Spring 启动时双重执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆