有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何使用@RabbitListener优雅地停止使用消息

是否有一种方法可以优雅地停止ListenerContainer及其关联的Consumers

我正在努力实现的目标

  1. 停止使用消息
  2. 优雅地停止ListenerContainer
  3. 等待长时间运行的消费者,并在完成时确认

我可以使用consumer.stop()停止ListenerContainers,但是活动的长时间运行的使用者将无法成功完成,并且处理的消息将不会被确认,因此在恢复ListenerContainer后将再次处理

输出

Waiting for workers to finish.
Workers not finished.
Closing channel for unresponsive consumer: Consumer@6d229b1c

消息已处理,但未确认

我可能能够使用setForceCloseChannel(false)实现优雅的关机,但是否可以验证取消的使用者是否已完成?SimpleMessageListenerContainer。doShutDown()有一个本地范围的列表“canceledConsumers”


共 (2) 个答案

  1. # 1 楼答案

    增加关机超时时间

    Message Listener Container Configuration

    shutdownTimeout

    When a container shuts down (for example, if its enclosing ApplicationContext is closed), it waits for in-flight messages to be processed up to this limit. Defaults to five seconds.

    /**
     * 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) {
    
  2. # 2 楼答案

    要完成@user634545的最后一条评论,我们必须处理两个属性:

    必须调整这些参数才能验证以下内容:

    prefetchCount < shutdownTimeout * consumerExecutionTimePerMessage
    

    这意味着在收到关闭命令后,使用者应该能够使用并确认每个预取的消息