多次在Spring MVC中执行Quartz调度程序

时间:2014-02-27 05:36:14

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

我在Spring MVC中使用Quarts进行任务调度,我的问题是它每次执行多次(两次)。请在下面找到配置和Java文件。

File web.xml

<!--  Spring Configuration -->

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

<listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<servlet>
    <servlet-name>spring</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring-servlet.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>spring</servlet-name>
    <url-pattern>*.do</url-pattern>
</servlet-mapping>

文件spring-servlet.xml

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

    <context:component-scan base-package="e24online.corporate.springmvc.controllers" />
    <context:component-scan base-package="com.elitecore.dashboard.controller" />
    <context:component-scan base-package="e24online.corporate.zeroconfiguration.controllers" />

    <import resource="serviceConf.xml" />
    <import resource="daoConf.xml" />
    <import resource="beanConf.xml" />
    <import resource="spring-quartz.xml" />

    <bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass"
            value="org.springframework.web.servlet.view.JstlView" />
        <property name="prefix" value="/webpages/" />
        <property name="suffix" value=".jsp" />
    </bean>

    <bean id="messageSource" class="e24online.corporate.springmvc.E24onlineReloadableResourceBundleMessageSource">
        <property name="basename" value="classpath:resources/properties/menu" />
        <property name="basenames">
            <list>
                <value>file:/usr/local/cyberoam/properties/multilingual/menu</value>
                <value>file:/usr/local/cyberoam/properties/multilingual/label</value>
                <value>file:/usr/local/cyberoam/properties/multilingual/message</value>
                <value>file:/usr/local/cyberoam/properties/multilingual/misc</value>
                <value>file:/usr/local/cyberoam/properties/multilingual/javascript</value>
            </list>
        </property>
        <property name="cacheSeconds" value="3000" />
    </bean>

    <bean id="localeChangeInterceptor" class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
        <property name="paramName" value="lang" />
    </bean>

    <bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver">
        <property name="defaultLocale" value="en" />
    </bean>

    <!-- Configure the multipart resolver -->
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <!-- one of the properties available; the maximum file size in bytes -->
        <property name="maxUploadSize" value="524288000"/>
    </bean>

    <bean id="handlerMapping"
          class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
        <property name="interceptors">
        <list>
            <ref bean="localeChangeInterceptor" />
            <bean class="e24online.corporate.springmvc.intercepter.E24onlineInterceptor"/>
        </list>
        </property>
    </bean>
</beans>

文件spring-quartz.xml

<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">

    <bean id="myTask" class="e24online.corporate.cas.printer.testScheduler.FixedDelayWorker" />

    <!-- Specifing class and method that is going to be called on a specified
         time basis. -->
    <bean id="myJob"
        class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
        <property name="targetObject" ref="myTask" />
        <property name="targetMethod" value="work" />
    </bean>

    <!-- Simple trigger specify repeat interval and delay time -->
    <bean id="simpleTrigger"
        class="org.springframework.scheduling.quartz.SimpleTriggerFactoryBean">
        <property name="jobDetail" ref="myJob" />
        <property name="repeatInterval" value="15000" />
        <property name="startDelay" value="0" />
    </bean>

    <!-- Scheduler factory bean to bind,the executing code and time intervals
         together. -->
    <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
        <property name="jobDetails">
            <list>
                <ref bean="myJob" />
            </list>
        </property>
        <property name="triggers">
            <list>
                <ref bean="simpleTrigger" />
            </list>
        </property>
    </bean>
</beans>

File FixedDelayWorker.java

package e24online.corporate.cas.printer.testScheduler;

import e24online.log.E24onlineLogger;

public class FixedDelayWorker implements Worker {

    public void work() {
        String threadName = Thread.currentThread().getName();
        E24onlineLogger.sysLog.debug("   " + threadName + " has began working.");
        E24onlineLogger.sysLog.debug("working...");
        E24onlineLogger.sysLog.debug("   " + threadName + " has completed work.");
    }
}

输出/日志

2014-05-05 15:06:53,462  - DEBUG [ FixedDelayWorker - work() -  9 ] -    org.springframework.scheduling.quartz.SchedulerFactoryBean#0_Worker-10 has began working.
2014-05-05 15:06:53,462  - DEBUG [ FixedDelayWorker - work() - 10 ] - working...
2014-05-05 15:06:53,462  - DEBUG [ FixedDelayWorker - work() - 11 ] -    org.springframework.scheduling.quartz.SchedulerFactoryBean#0_Worker-10 has completed work.
2014-05-05 15:06:54,881  - DEBUG [ FixedDelayWorker - work() -  9 ] -    org.springframework.scheduling.quartz.SchedulerFactoryBean#0_Worker-10 has began working.
2014-05-05 15:06:54,882  - DEBUG [ FixedDelayWorker - work() - 10 ] - working...
2014-05-05 15:06:54,882  - DEBUG [ FixedDelayWorker - work() - 11 ] -    org.springframework.scheduling.quartz.SchedulerFactoryBean#0_Worker-10 has completed work.

2 个答案:

答案 0 :(得分:1)

我认为这可能是因为您在web.xml中包含以下两行

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

一次作为单独的,一次为Spring servlet定义。 这可能会创建两个不同的上下文。 我没有测试你的代码...

答案 1 :(得分:0)

您的代码:

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

<servlet>
    <servlet-name>spring</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring-servlet.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

你要两次调用 spring-servlet.xml

我的建议是:

   <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>

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

   <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>

或只是

<servlet>
   <servlet-name>dispatcher</servlet-name>
   <servlet-class>
     org.springframework.web.servlet.DispatcherServlet
   </servlet-class>
   <init-param>
     <param-name>contextConfigLocation</param-name>
     <param-value>/WEB-INF/spring/dispatcher-config.xml</param-value>
   </init-param>
   <load-on-startup>1</load-on-startup>
 </servlet>

 <servlet-mapping>
   <servlet-name>dispatcher</servlet-name>
   <url-pattern>/</url-pattern>
 </servlet-mapping>

希望有所帮助

如果你发现我没有提出任何解释,因为我很难解释它。我希望这些代码可以做到。