处理当前消息时从容关闭 Spring 集成应用程序

Graceful shutdown Spring integration application when current messages processed

我有一个应用程序,它从 JMS 消息队列读取数据,并在读取消息消息后传递给 diff 异步通道(执行程序通道)。当我停止应用程序时,它会停止处理当前正在运行的消息并抛出错误

org.springframework.messaging.MessageDeliveryException: Dispatcher has no subscribers for channel
    at org.springframework.integration.channel.AbstractSubscribableChannel.doSend(AbstractSubscribableChannel.java:93)
    at org.springframework.integration.channel.AbstractMessageChannel.send(AbstractMessageChannel.java:425)
    at org.springframework.integration.channel.MessagePublishingErrorHandler.handleError(MessagePublishingErrorHandler.java:114)
    at org.springframework.integration.util.ErrorHandlingTaskExecutor.run(ErrorHandlingTaskExecutor.java:58)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at java.lang.Thread.run(Thread.java:748)
Caused by: org.springframework.integration.MessageDispatchingException: Dispatcher has no subscribers
    at org.springframework.integration.dispatcher.UnicastingDispatcher.doDispatch(UnicastingDispatcher.java:154)
    at org.springframework.integration.dispatcher.UnicastingDispatcher.dispatch(UnicastingDispatcher.java:121)
    at org.springframework.integration.channel.AbstractSubscribableChannel.doSend(AbstractSubscribableChannel.java:89).

有什么方法可以正常关闭应用程序吗?

我已经在应用上下文中使用 registerShutdownHook()

参见the documentation about Orderly Shutdown

As described in "MBean Exporter", the MBean exporter provides a JMX operation called stopActiveComponents, which is used to stop the application in an orderly manner. The operation has a single Long parameter. The parameter indicates how long (in milliseconds) the operation waits to allow in-flight messages to complete. The operation works as follows: ...

但是,异步切换和 JMS 会存在消息丢失的风险。

并发最好由侦听器容器管理。