简体   繁体   English

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

[英]Execution of Quarts scheduler in Spring MVC multiple times

I am using Quarts for task scheduling in Spring MVC, and my problem is that it executes multiple times (twice) every task. 我在Spring MVC中使用Quarts进行任务调度,但我的问题是每个任务执行多次(两次)。 Please find the configuration and Java files below. 请在下面找到配置和Java文件。

File web.xml 文件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>

File spring-servlet.xml 文件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>

File spring-quartz.xml 文件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 文件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.");
    }
}

Output/Logs 输出/日志

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.

I think it might be because you are including the following line twice in your web.xml 我认为这可能是因为您在web.xml中两次包含了以下行

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

once as a separate and once for the Spring servlet definition. 一次作为单独的,一次用于Spring servlet的定义。 This might be creating two different contexts. 这可能会创建两个不同的上下文。 I have not tested with your code... 我尚未测试您的代码...

Your Code: 您的代码:

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

And

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

you are calling the spring-servlet.xml twice. 您将两次调用spring-servlet.xml

My Suggestion is : 我的建议是:

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

or simply 或简单地

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

hope it helps 希望能帮助到你

if you notice i didnt put any explanation because im having a hard time to explain it. 如果您注意到我没有做任何解释,因为即时通讯很难解释它。 i hope this codes will do. 我希望这个代码能做到。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM