samedi 21 février 2015

Spring @Scheduled task runs twice

I am creating an @Scheduled task to run every 5 seconds. As has been a problem in other questions, my task is running twice!


I have looked at other questions, and read the applicable documentation here, but I have not been able to figure out the problem.


I know that two seperate instances of my @Scheduled class are getting instantiated when I start my tomcat server. I have also figured out when they are instantiated in reference to my log file.


One associated with this log line :



INFO: Initializing Spring root WebApplicationContext



and another with this log line:



INFO: Initializing Spring FrameworkServlet 'servlet'



Here is the spring config file.



<beans xmlns="http://ift.tt/GArMu6"
xmlns:context="http://ift.tt/GArMu7"
xmlns:xsi="http://ift.tt/ra1lAU" xmlns:mvc="http://ift.tt/1bHqwjR"
xmlns:security="http://ift.tt/1c8inpe"
xmlns:task="http://ift.tt/LFBt0P"
xsi:schemaLocation="http://ift.tt/GArMu6 http://ift.tt/1cMYE2s
http://ift.tt/1bHqwjR http://ift.tt/1fmimld
http://ift.tt/1c8inpe http://ift.tt/1epvZ6L
http://ift.tt/GArMu7 http://ift.tt/QEDs1k http://ift.tt/LFBt0P http://ift.tt/1je45tD">

<context:component-scan base-package="web.controllers"/>
<context:component-scan base-package="services"/>
<context:component-scan base-package="dao"/>
<context:component-scan base-package="scheduled"/>
<context:property-placeholder location="/WEB-INF/application.properties"/>

<mvc:annotation-driven/>
<mvc:default-servlet-handler/>

<task:annotation-driven />


And my simple java class:



package scheduled;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

@Service
public class Notifier {

@Scheduled(fixedDelay = 5000)
public void notifyUsersOfBidItems() {
try {
System.out.println(this);

} catch (Exception e) {
e.printStackTrace();
}

}
}


Also, I am using Spring 4.


Thank you so much if you can help me!!!


EDIT: Adding web.xml



<web-app xmlns="http://ift.tt/nSRXKP"
xmlns:xsi="http://ift.tt/ra1lAU"
xsi:schemaLocation="http://ift.tt/nSRXKP http://ift.tt/1eWqHMP"
version="3.0">
<display-name>Archetype Created Web Application</display-name>

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

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

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

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

<error-page>
<error-code>404</error-code>
<location>/error/notFound</location>
</error-page>

<error-page>
<error-code>403</error-code>
<location>/error/notFound</location>
</error-page>


<error-page>
<location>/error/internal</location>
</error-page>

<!-- Spring Security -->
<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>

Aucun commentaire:

Enregistrer un commentaire