1

I read both of this but they do not seem to work

Java Spring @Scheduled tasks executing twice

Spring @Scheduled is executing task twice when using annotations

I do not have @Configurable class, and I do not use applicationConetxt.xml.

My ScheduleTasks

@EnableScheduling
@Component
public class ScheduledTasks {
    @Autowired
    private ApplicationContext ctx;

    @Scheduled(fixedRate=5000)
    public void monitorRoom(){
        System.out.println("--------------------");
    }
}

I can see that when I run the app in tomcat, the task prints twice evrey 5 seconds.

Somebody says to remove

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

it works , but I feel this is not a solution. since this should be needed.(at least from the example from google)

Here is my web.xml

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
    <display-name>wodinow</display-name>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/dispatcher-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>
        <load-on-startup>1</load-on-startup>
    </servlet>

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

</web-app>

dispatcher-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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-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/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <context:component-scan base-package="wodinow.weixin.jaskey" />

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

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

    <mvc:annotation-driven/>

</beans>
Community
  • 1
  • 1
JaskeyLam
  • 15,405
  • 21
  • 114
  • 149
  • Actuall you are using an xml file for configuration, the name doesn't matter. Both the `ContextLoaderListener` and `DispatcherServlet` xml load the same file, meaning you get every bean twice, including your scheduled one. The easy solution remove the `ContexLoaderListener` from your configuration. Another thing make a choice either use xml or java config but don't mix. You have a `@EnableScheduling` which is java based configuration, you are using xml so instead of that annotation add `` to your xml configuration. – M. Deinum Nov 06 '14 at 12:34
  • @M.Deinum , Thank you! So for any case of loading dispatcher-servlet.xml, we do not need to use contextloaderlistener? Why all the guide gives me the same config? Would you please post your answer in the answer? Thank you! – JaskeyLam Nov 06 '14 at 12:39

1 Answers1

1

Remove the context loader listener. You only need that if you have to initialize a parent context. The contexts from the dispatcher servlet are standalone, but could access such parent contexts if needed, i.e. to retreive a service or dao.

Stefan
  • 12,108
  • 5
  • 47
  • 66