Spring @Scheduled task запускается дважды
Я создаю@Scheduled
задание запускать каждые 5 секунд. Как и в других вопросах, моя задача выполняется дважды!
Я посмотрел на другие вопросы и прочитал соответствующую документациюВот, но я не смог разобраться в проблеме.
я знаю этодва отдельных экземпляры моего@Scheduled
класс получают, когда я запускаю свой сервер Tomcat. Я также выяснил, когда они создаются в отношении моего файла журнала.
Один связан с этой строкой журнала:
ИНФОРМАЦИЯ: Инициализация корневого элемента Spring WebApplicationContext
и другой с этой строкой журнала:
ИНФОРМАЦИЯ: Инициализация Spring FrameworkServlet 'сервлет'
Вот весенний конфигурационный файл.
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:security="http://www.springframework.org/schema/security"
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.1.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd">
<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 />
И мой простой Java-класс:
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();
}
}
}
Также я использую Spring 4.
РЕДАКТИРОВАТЬ:Adding web.xml
<web-app 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_3_0.xsd"
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>