Spring @Scheduled任务运行两次
问题内容:
我正在创建一个@Scheduled
任务,每5秒运行一次。就像其他问题一样,我的任务运行了两次!
我查看了其他问题,并在此处阅读了适用的文档,但是我仍然无法解决问题。
我知道启动tomcat服务器时,将实例化我的类的 两个单独 实例@Scheduled
。我还想出了何时参考我的日志文件实例化它们。
与该日志行关联的一个:
INFO:初始化Spring根WebApplicationContext
另一个与此日志行:
INFO:初始化Spring FrameworkServlet’servlet’
这是spring配置文件。
<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>
问题答案:
我相信这是由于在您的web.xml中两次加载相同的配置文件引起的
<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> <!-- FIRST -->
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring_config.xml</param-value> <!-- SECOND -->
</context-param>
编辑以解决此问题:
创建另一个文件servlet-
servlet.xml(默认情况下,它将由ServletDispatcher配置拾取,因为它通过Servlet名称与该文件匹配)。该文件将包含以下内容:
<beans>
<context:component-scan base-package="web.controllers"/>
<mvc:annotation-driven/>
<mvc:default-servlet-handler/>
</beans>
修改原始文件(spring-config.xml):
<beans>
<task:annotation-driven />
<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"/>
</beans>
将您的Web xml Servlet配置修改为以下内容:
<servlet>
<servlet-name>servlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>