java - 如何优雅地停止使用@RabbitListener 消费消息
问题描述
有没有办法优雅地停止 aListenerContainer
及其关联的Consumers
.
我正在努力实现的目标。
- 停止消费消息。
- 优雅地停下
ListenerContainer
。 - 等待长时间运行的消费者,并在完成时确认。
我可以停止ListenerContainers
using consumer.stop()
,但活动的长时间运行的消费者将无法成功完成,并且一旦 ListenerContainer 恢复,处理的消息将不会被确认,因此将再次处理。
输出
Waiting for workers to finish.
Workers not finished.
Closing channel for unresponsive consumer: Consumer@6d229b1c
消息已处理,但未确认。
我也许可以使用 来实现正常关闭setForceCloseChannel(false)
,但是否可以验证取消的消费者是否已完成?SimpleMessageListenerContainer.doShutDown() 有一个本地范围的列表“canceledConsumers”。
解决方案
增加关机超时。
请参阅消息侦听器容器配置。
关机超时
当容器关闭时(例如,如果其封闭的 ApplicationContext 已关闭),它会等待处理中的消息达到此限制。默认为五秒。
/**
* The time to wait for workers in milliseconds after the container is stopped. If any
* workers are active when the shutdown signal comes they will be allowed to finish
* processing as long as they can finish within this timeout. Defaults
* to 5 seconds.
* @param shutdownTimeout the shutdown timeout to set
*/
public void setShutdownTimeout(long shutdownTimeout) {
推荐阅读
- php - 用于提供图像的临时 S3 存储桶 URL
- mongodb - Mongoose:MongoError:>1 字段,同时尝试投影 $elemMatch
- c# - JIT SignalR Hub 发送和接收
- java - 如何将 HQL 放入实体中?
- image - rectifyStereoImages(图像校正)输出看起来失真
- ruby-on-rails - 由于 nil 值,Rails RSpec 功能测试失败,模型测试通过
- javascript - 通过拖放在输入中附加文件的问题
- android - React native Activity Indicator 仅在设备连接到笔记本电脑时有效
- c# - 添加脚本到游戏对象,找不到脚本
- ios - 通过 SKConstraint 使用 LERP 移动 SKCameraNode