[英]Clean shutdown of Spring WebSockets STOMP client
A Spring WebSocket STOMP client sends a long
to Spring WebSocket STOMP server that immediately returns the same value. A Spring WebSocket STOMP 客户端向long
Z45EDC1B96407D9D2103DD11STOMP596立即返回相同的值。 When the client completes sending, it exits its main thread and the client terminates as expected.当客户端完成发送时,它退出其主线程,客户端按预期终止。
If I enable STOMP heartbeats :如果我启用 STOMP 心跳:
webSocketStompClient.setTaskScheduler(
new DefaultManagedTaskScheduler()
);
webSocketStompClient.setDefaultHeartbeat(
new long[] {10_000, 10_000}
);
the client no longer exits the JVM when the client's main thread finishes because the DefaultManagedTaskScheduler
task-scheduler started a non-daemon thread ("pool-2-thread-1").当客户端的主线程完成时,客户端不再退出 JVM,因为DefaultManagedTaskScheduler
任务调度程序启动了一个非守护线程(“pool-2-thread-1”)。
I don't want to exit via System.exit
, so how can the keep-alive task-scheduler be shutdown so the client terminates when the main thread finishes?我不想通过System.exit
退出,那么如何关闭 keep-alive 任务调度程序,以便客户端在主线程完成时终止?
TL;DR TL;博士
Build and keep the JDKs executor and shut down the executor when finished.构建并保留 JDK 执行程序,完成后关闭执行程序。
Details :详情:
public class MyTaskScheduler {
private final ScheduledExecutorService executor;
private final ConcurrentTaskScheduler scheduler;
public MyTaskScheduler() {
executor = Executors.newScheduledThreadPool(1);
scheduler = new ConcurrentTaskScheduler(executor);
}
public TaskScheduler taskScheduler() {
return scheduler;
}
public void shutdown() {
executor.shutdown();
}
}
In the appropriate context, construct and keep the new task-scheduler:在适当的上下文中,构造并保留新的任务调度器:
MyTaskScheduler myTaskScheduler = new MyTaskScheduler();
And use the new task-scheduler instance for heartbeats:并将新的任务调度程序实例用于心跳:
webSocketStompClient.setTaskScheduler(
myTaskScheduler
);
webSocketStompClient.setDefaultHeartbeat(
new long[] {10_000, 10_000}
);
When application is finished shut down the executor:应用程序完成后关闭执行程序:
myTaskScheduler.shutdown();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.