如何管理/停止 spring 4 ConcurrentTaskScheduler [英] How to manage/stop spring 4 ConcurrentTaskScheduler

查看:36
本文介绍了如何管理/停止 spring 4 ConcurrentTaskScheduler的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是 Spring 4.我用它来定期为 Web 套接字执行任务:

I am using Spring 4. I use this for execute a task periodically for web sockets:

private TaskScheduler scheduler = new ConcurrentTaskScheduler();

在我的课堂上:

@Configuration
@EnableWebSocketMessageBroker
@EnableScheduling
@Component
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {

    @Autowired
    private SimpMessagingTemplate template;


    private TaskScheduler scheduler = new ConcurrentTaskScheduler();
    public void registerStompEndpoints(StompEndpointRegistry registry) {
        registry.addEndpoint("/simplemessages").withSockJS();
    }

    public void configureMessageBroker(MessageBrokerRegistry config) {

        config.enableSimpleBroker("/topic/", "/queue/");

        config.setApplicationDestinationPrefixes("/app");
    }


    @PostConstruct
    private void broadcastTimePeriodically() {


        scheduler.scheduleAtFixedRate(new Runnable() {
            public void run() {
                String statStr = "Server Response" + new Date();
                System.out.println("thread schedular run time :" + Hello.printTime());
                try {
                    template.convertAndSend("/topic/simplemessagesresponse", statStr);
                } catch (MessagingException e) {
                    System.err.println("!!!!!! websocket timer error :>" + e.toString());
                }

            }
        }, 4000));


}

@PreDestroy
private void destroyServices() {
    scheduler = null; // how to destroy ? 

}

public void configureClientInboundChannel(ChannelRegistration registration) {

}

public void configureClientOutboundChannel(ChannelRegistration registration) {
    registration.taskExecutor().corePoolSize(4).maxPoolSize(10);
}

public boolean configureMessageConverters(List < MessageConverter > arg0) {
    // TODO Auto-generated method stub
    return true;
}
@Override
public void configureWebSocketTransport(WebSocketTransportRegistration arg0) {

}
}

我想知道的事情:

  1. 我发现调度程序在 4000 毫秒内运行了两次.它是如何发生的,我该如何阻止它?

  1. I found that the scheduler is running twice within 4000 milliseconds. How is it happening and how can I stop it?

我在 tomcat 中运行这个应用程序.如您所见,destroyServices() 方法需要销毁调度程序.这里的问题是,即使再次重启tomcat,之前运行的线程仍在运行.因此,当 tomcat 即将关闭时,该线程也应该终止.我需要知道如何在 tomcat 即将关闭或任何系统崩溃时销毁它?

I run this application in tomcat. As you can see, the method destroyServices() needs to destroy the schedular. Here the problem is, even the tomcat is restarted again, previously running thread is still running. So when the tomcat is going to down, that thread also should be terminated. I need to know How I can destroy it on tomcat is going to down or any system crash?

推荐答案

以下代码片段来自 @EnableScheduling 的文档:

The following code snippet is from documentation of @EnableScheduling:

   @Configuration
   @EnableScheduling
   public class AppConfig implements SchedulingConfigurer {
       @Override
       public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
           taskRegistrar.setScheduler(taskExecutor());
       }

       @Bean(destroyMethod="shutdown")
       public Executor taskExecutor() {
           return Executors.newScheduledThreadPool(100);
       }
   }

我认为您应该获取名为 taskExecutor 的 bean(在本例中)并调用它的 shutdown(实际上取决于您的配置)方法.

I think you should get the bean named taskExecutor (in this case) and call shutdown (in fact depending on your configuration) method of it.

这篇关于如何管理/停止 spring 4 ConcurrentTaskScheduler的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆