Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在Tomcat7上停止Spring@计划任务?_Java_Spring_Tomcat - Fatal编程技术网

Java 如何在Tomcat7上停止Spring@计划任务?

Java 如何在Tomcat7上停止Spring@计划任务?,java,spring,tomcat,Java,Spring,Tomcat,我正在Servlet3.0、Spring3和其他一些东西上构建JavaWeb项目。 我正在尝试使用Spring的@scheduled任务。 它工作正常,但当我从Tomcat中取消部署war时,计划的任务继续运行。如何在Spring容器上停止它 更新 我已经找到了问题的根源——我实例化了两次Spring上下文。第一次在web.xml中作为 <context-param> <param-name>contextConfigLocation</param-name>

我正在Servlet3.0、Spring3和其他一些东西上构建JavaWeb项目。 我正在尝试使用Spring的@scheduled任务。
它工作正常,但当我从Tomcat中取消部署war时,计划的任务继续运行。如何在Spring容器上停止它

更新 我已经找到了问题的根源——我实例化了两次Spring上下文。第一次在web.xml中作为

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
    /WEB-INF/spring/servlet-context.xml
</param-value>
</context-param>
因此,创建了两个ScheduledProcessor实例,其中一个被销毁。所以春天摧毁了它意识到的一个,第二个继续运行,让我觉得什么都没有被摧毁。 删除ClassPathXmlApplicationContext解决了我的问题

Controller.java

package a.b.c;
imports...

@Controller
@RequestMapping("/")
public class ReceiverController {

    @RequestMapping(method = RequestMethod.GET)
    public String welcome(ModelMap model) {
        new ClassPathXmlApplicationContext("config.xml", ReceiverController.class);
        return "index";
    }


}

ScheduledProcessor.java

package a.b.c;

imports..

@Service
public class ScheduledProcessor   {

    private Logger logger = LoggerFactory.getLogger(ScheduledProcessor.class);

    @Scheduled(fixedDelay = 30000)
    public void process() {
        logger.info("Processing task");

    }

}

spring config.xml

<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: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/task http://www.springframework.org/schema/task/spring-task-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="a.b.c"/>
    <task:annotation-driven/>

</beans>

日志文件输出:

2013年7月3日11:32:34436[INFO](ScheduledProcessor.process:17)->处理任务
11:33:04439[信息](ScheduledProcessor.process:17)->处理任务
2013年7月11:33:06913[信息](XmlWebApplicationContext.doClose:1042)->关闭 命名空间“测试servlet”的WebApplicationContext:启动日期[Wed] 2013年7月3日11:32:21];上下文层次结构的根目录2013年7月3日 11:33:06914[信息]
(DefaultListableBeanFactory.DestroysSingleton:444)->正在销毁 单身人士 org.springframework.beans.factory.support。DefaultListableBeanFactory@1a56058: 定义bean [scheduledProcessor、receiverController、org.springframework.context.annotation.internalConfigurationAnnotationProcessor等。。; 工厂层次结构的根 **
2013年7月3日11:33:34440信息->处理任务
2013年7月3日12:34:0440信息->处理任务


可能
registerShutdownHook()
会对您有所帮助。我已将我的应用程序上下文更改为AbstractApplicationContext context=new ClassPathXmlApplicationContext(“config.xml”,ReceiverController.class);context.registerShutdownHook();但仍然相同。
<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: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/task http://www.springframework.org/schema/task/spring-task-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="a.b.c"/>
    <task:annotation-driven/>

</beans>