Java 如何管理/停止spring 4 ConcurrentTaskScheduler
我正在使用Spring4。我使用它定期执行web套接字的任务:Java 如何管理/停止spring 4 ConcurrentTaskScheduler,java,spring,tomcat,Java,Spring,Tomcat,我正在使用Spring4。我使用它定期执行web套接字的任务: private TaskScheduler scheduler = new ConcurrentTaskScheduler(); 在我的班上: @Configuration @EnableWebSocketMessageBroker @EnableScheduling @Component public class WebSocketConfig implements WebSocketMessageBrokerConfigure
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) {
}
}
@配置
@EnableWebSocketMessageBroker
@使能调度
@组成部分
公共类WebSocketConfig实现WebSocketMessageBrokerConfiger{
@自动连线
私有SimpMessagingTemplate;
private TaskScheduler=新的ConcurrentTaskScheduler();
公共无效注册表TompendPoints(StompEndpointRegistry注册表){
registry.addEndpoint(“/simplemessages”).withSockJS();
}
public void配置MessageBroker(MessageBrokerRegistry配置){
config.enableSimpleBroker(“/topic/”,“/queue/”);
config.setApplicationDestinationPrefixes(“/app”);
}
@施工后
私有无效广播时间周期(){
scheduleAtFixedRate(新的Runnable(){
公开募捐{
字符串statStr=“服务器响应”+新日期();
System.out.println(“线程调度运行时:+Hello.printTime());
试一试{
template.convertAndSend(“/topic/simpleMessageResponse”,statStr);
}捕获(消息异常e){
System.err.println(“!!!!!!!websocket计时器错误:>”+e.toString());
}
}
}, 4000));
}
@发情前期
私人服务(){
scheduler=null;//如何销毁?
}
公共无效配置ClientInBoundChannel(通道注册){
}
公共无效配置ClientOutboundChannel(通道注册){
registration.taskExecutor().corePoolSize(4.maxPoolSize(10);
}
公共布尔配置MessageConverter(列表arg0){
//TODO自动生成的方法存根
返回true;
}
@凌驾
公共无效配置WebSocketTransport(WebSocketTransportRegistration arg0){
}
}
我想知道一些事情:
destroyServices()
需要销毁调度程序。这里的问题是,即使tomcat再次重启,以前运行的线程仍然在运行。因此,当tomcat停止运行时,该线程也应该终止。我需要知道如何在tomcat即将崩溃或任何系统崩溃时销毁它以下代码片段来自
@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
(实际上取决于您的配置)方法。谢谢。我用完整的课程更新了我的问题。我不清楚声明@Bean(destromethod=“shutdown”)
。