Quartz + Spring双执行

时间:2015-04-20 14:28:37

标签: java xml spring spring-mvc quartz-scheduler

我有Quartz 2.2.1和Spring 3.2.8 app。我的问题是,在启动过程中,Quartz在2秒后创建了两个以下的作业。我的工作必须每10秒执行一次,但它会执行两次:

Info:   INFO: Starting TASK on Mon Apr 20 16:22:21 CEST 2015
Info:   INFO: Starting TASK on Mon Apr 20 16:22:23 CEST 2015
Info:   INFO: Starting TASK on Mon Apr 20 16:22:31 CEST 2015
Info:   INFO: Starting TASK on Mon Apr 20 16:22:33 CEST 2015
Info:   INFO: Starting TASK on Mon Apr 20 16:22:41 CEST 2015
Info:   INFO: Starting TASK on Mon Apr 20 16:22:43 CEST 2015

我尝试按照Quartz + Spring double execution on startup上的建议。提供的答案(在web.xml中删除包含“ContextLoaderListener”的标签)没有帮助,因为我遇到了WebContext丢失的错误。谢谢你的帮助。

这是我的web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 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_2_5.xsd">
    <servlet>
        <servlet-name>spring</servlet-name>
        <servlet-class>
            org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>spring</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
    <jsp-config>
        <jsp-property-group>
            <description>header and footer settings</description>
            <url-pattern>/WEB-INF/*</url-pattern>
            <include-prelude>/WEB-INF/header.jspf</include-prelude>
        </jsp-property-group>
    </jsp-config>

    <!-- Spring Security -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <listener>
        <listener-class>utils.SessionCounterListener</listener-class>
    </listener>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            /WEB-INF/spring-servlet.xml
        </param-value>
    </context-param>

    <filter>
        <filter-name>springSecurityFilterChain</filter-name>
        <filter-class>org.springframework.web.filter.DelegatingFilterProxy
        </filter-class>
    </filter>

    <filter-mapping>
        <filter-name>springSecurityFilterChain</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
</web-app>

我的spring-servlet.xml看起来像这样:

<?xml version="1.0" encoding="windows-1252"?> <beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       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
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
       http://www.springframework.org/schema/tx
       http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">

    <!-- Use @Component annotations for bean definitions -->

    <context:component-scan base-package="cinema"/>

    <!-- Use @Controller annotations for MVC controller definitions -->

    <mvc:resources mapping="/resources/**" location="/resources/" />

    <mvc:annotation-driven />


    <bean id="exampleBusinessObject" class="cinema.controller.ServerMonitorController"/>


    <bean id="jobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
        <property name="targetObject" ref="exampleBusinessObject"/>
        <property name="targetMethod" value="sendIt"/>
        <property name="concurrent" value="false"/>
    </bean>

    <bean id="simpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerFactoryBean">
        <property name="jobDetail" ref="jobDetail"/>
        <!-- czas do odczekania 
        <property name="startDelay" value="10000"/>-->
        <!-- czas pomiedzy odpaleniami (50 sek = 50000) -->
        <property name="repeatInterval" value="10000"/>
    </bean>

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


    <bean id="emf" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="loadTimeWeaver">
            <bean class="org.springframework.instrument.classloading.glassfish.GlassFishLoadTimeWeaver"/>
        </property>
    </bean>


    <tx:jta-transaction-manager />

    <!-- Use @Transaction annotations for managing transactions -->
    <tx:annotation-driven />

    <!-- View resolver -->
    <bean class=
     "org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/"/>
    </bean>

    <import resource="/spring-security.xml" /> </beans>

ServerMonitorController.java:

@Controller
public class ServerMonitorController {

    public static void sendIt() throws FileNotFoundException, IOException {

        System.out.println("INFO: Starting TASK on "+ new Date());

    }
}

0 个答案:

没有答案