0
我使用Spring 4,我用這爲網絡插座定期執行任務:如何管理/停止彈簧4 ConcurrentTaskScheduler
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) {
}
}
我想知道的東西:
我發現調度程序在4000毫秒內運行兩次。它是如何發生的,我怎樣才能阻止它?
我在tomcat中運行這個應用程序。正如你所看到的,方法
destroyServices()
需要破壞schedular。這裏的問題是,即使tomcat重新啓動,以前運行的線程仍在運行。所以當tomcat正在關閉時,該線程也應該被終止。我需要知道如何在tomcat上銷燬它或者系統崩潰?
謝謝。我用完整的課程更新了我的問題。我不清楚聲明'@Bean(destroyMethod =「shutdown」)'。 – 2014-09-13 16:20:04