如何为rabbit mq listener设置Shutdown参数

时间:2018-02-12 13:24:20

标签: spring-boot spring-rabbitmq

我们正在尝试关闭所有在微服务中运行的进程

  1. 任何消费者在尝试确认时都会做一些工作 他们当前的消息
  2. 频道上的任何其他操作。我想让消费者 完成他们正在处理的任何消息,然后关闭所有内容 下来。

  3. 如果我们停止正在运行的进程并允许传输数据,那么请等到shutdown hook timeout中提到的指定时间段然后终止。

  4. 我们已经创建了如下的bean

     @Bean
      public SimpleMessageListenerContainer SimpleMessageListenerContainer() {
    
          SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(rabbitConnectionFactory);
          container.setIdleEventInterval(60000L);
          container.setShutdownTimeout(900000); // tried 
          return container;
      }
    
    用上面的bean试图实现,但它没有用。我们正在使用spring-rabbit 1.7.3.RELEASE。

    日志:

    2018-02-13 13:07:11.137  INFO 13412 --- [on(9)-127.0.0.1] o.s.a.r.l.SimpleMessageListenerContainer : Waiting for workers to finish.
    2018-02-13 13:07:11.137  INFO 13412 --- [on(9)-127.0.0.1] o.s.a.r.l.SimpleMessageListenerContainer : Successfully waited for workers to finish.
    2018-02-13 13:07:11.137  INFO 13412 --- [on(9)-127.0.0.1] o.s.c.s.DefaultLifecycleProcessor        : Stopping beans in phase 2147482647
    2018-02-13 13:07:11.137  INFO 13412 --- [on(9)-127.0.0.1] o.s.a.r.l.SimpleMessageListenerContainer : Waiting for workers to finish.
    2018-02-13 13:07:11.137  INFO 13412 --- [on(9)-127.0.0.1] o.s.a.r.l.SimpleMessageListenerContainer : Successfully waited for workers to finish.
    2018-02-13 13:07:11.137  INFO 13412 --- [on(9)-127.0.0.1] o.s.i.a.i.AmqpInboundChannelAdapter      : stopped inbound.x
    2018-02-13 13:07:11.138  INFO 13412 --- [on(9)-127.0.0.1] o.s.c.s.DefaultLifecycleProcessor        : Stopping beans in phase 1073741823
    2018-02-13 13:07:11.139  INFO 13412 --- [on(9)-127.0.0.1] o.s.a.r.l.SimpleMessageListenerContainer : Waiting for workers to finish.
    2018-02-13 13:07:11.139  INFO 13412 --- [on(9)-127.0.0.1] o.s.a.r.l.SimpleMessageListenerContainer : Successfully waited for workers to finish.
    2018-02-13 13:07:11.139  INFO 13412 --- [on(9)-127.0.0.1] o.s.i.a.i.AmqpInboundChannelAdapter      : stopped a
    2018-02-13 13:07:11.140  INFO 13412 --- [on(9)-127.0.0.1] o.s.a.r.l.SimpleMessageListenerContainer : Waiting for workers to finish.
    2018-02-13 13:07:11.140  INFO 13412 --- [on(9)-127.0.0.1] o.s.a.r.l.SimpleMessageListenerContainer : Successfully waited for workers to finish.
    2018-02-13 13:07:11.140  INFO 13412 --- [on(9)-127.0.0.1] o.s.i.a.i.AmqpInboundChannelAdapter      : stopped b
    2018-02-13 13:07:11.140  INFO 13412 --- [on(9)-127.0.0.1] o.s.a.r.l.SimpleMessageListenerContainer : Waiting for workers to finish.
    2018-02-13 13:07:11.140  INFO 13412 --- [on(9)-127.0.0.1] o.s.a.r.l.SimpleMessageListenerContainer : Successfully waited for workers to finish.
    2018-02-13 13:07:11.140  INFO 13412 --- [on(9)-127.0.0.1] o.s.i.a.i.AmqpInboundChannelAdapter      : stopped c
    2018-02-13 13:07:11.140  INFO 13412 --- [on(9)-127.0.0.1] o.s.a.r.l.SimpleMessageListenerContainer : Waiting for workers to finish.
    2018-02-13 13:07:11.140  INFO 13412 --- [on(9)-127.0.0.1] o.s.a.r.l.SimpleMessageListenerContainer : Successfully waited for workers to finish.
    2018-02-13 13:07:11.140  INFO 13412 --- [on(9)-127.0.0.1] o.s.i.a.i.AmqpInboundChannelAdapter      : stopped d
    2018-02-13 13:07:11.140  INFO 13412 --- [on(9)-127.0.0.1] o.s.a.r.l.SimpleMessageListenerContainer : Waiting for workers to finish.
    2018-02-13 13:07:11.140  INFO 13412 --- [on(9)-127.0.0.1] o.s.a.r.l.SimpleMessageListenerContainer : Successfully waited for workers to finish.
    2018-02-13 13:07:11.140  INFO 13412 --- [on(9)-127.0.0.1] o.s.i.a.i.AmqpInboundChannelAdapter      : stopped e
    2018-02-13 13:07:11.140  INFO 13412 --- [on(9)-127.0.0.1] o.s.c.s.DefaultLifecycleProcessor        : Stopping beans in phase 0
    2018-02-13 13:07:11.140  INFO 13412 --- [on(9)-127.0.0.1] o.s.i.e.EventDrivenConsumer              : Removing {json-to-object-transformer:convertEventJSONToObject} as a subscriber to the 'eventJSONToObjectChannel' channel
    2018-02-13 13:07:11.141  INFO 13412 --- [on(9)-127.0.0.1] o.s.i.c.DirectChannel                    : Channel 'xxx:local:8006.eventJSONToObjectChannel' has 0 subscriber(s).
    2018-02-13 13:07:11.143  INFO 13412 --- [on(9)-127.0.0.1] o.s.i.e.EventDrivenConsumer              : stopped convertEventJSONToObject
    2018-02-13 13:07:11.144  INFO 13412 --- [on(9)-127.0.0.1] o.s.i.e.EventDrivenConsumer              : Removing {bridge} as a subscriber to the 'asyncEventTypeRouterChannel' channel
    2018-02-13 13:07:11.144  INFO 13412 --- [on(9)-127.0.0.1] o.s.i.c.ExecutorChannel                  : Channel 'xxx:local:1234.asyncEventTypeRouterChannel' has 0 subscriber(s).
    2018-02-13 13:07:11.144  INFO 13412 --- [on(9)-127.0.0.1] o.s.i.e.EventDrivenConsumer              : stopped org.springframework.integration.config.ConsumerEndpointFactoryBean#0
    2018-02-13 13:07:11.144  INFO 13412 --- [on(9)-127.0.0.1] o.s.i.e.EventDrivenConsumer              : Removing {router:springIntEventRouter} as a subscriber to the 'eventTypeRouterChannel' channel
    2018-02-13 13:07:11.145  INFO 13412 --- [on(9)-127.0.0.1] o.s.i.c.DirectChannel                    : Channel 'xxx:local:1234.eventTypeRouterChannel' has 0 subscriber(s).
    

    已尝试将关闭属性设置为容器,但默认情况下仍然会在此之前或之前关闭;而不是使用覆盖设置属性。

    • 这是为每个入站通道适配器设置关闭属性的更好方法,如果是这种情况,我们必须配置超过100个通道
    • 我们已经通过STS中的终止按钮和控制台中的ctr + c测试高于1和2点来实现,但它并没有等待上面提到的关机时间。请大家帮忙解决这个问题。

    • 有没有办法确定关机时间的运行时间。

1 个答案:

答案 0 :(得分:1)

它的工作原理与你一样......

@SpringBootApplication
public class So48747564Application {

    public static void main(String[] args) {
        SpringApplication.run(So48747564Application.class, args);
    }

    @Bean
    public ApplicationRunner runner(RabbitTemplate template, SimpleMessageListenerContainer container) {
        return args -> {
            Scanner scanner = new Scanner(System.in);
            String line = null;
            while (!"quit".equals(line)) {
                line = scanner.nextLine();
                template.convertAndSend("so48747564", line);
            }
            scanner.close();
            container.stop();
        };
    }

    @Bean
    public SimpleMessageListenerContainer container(ConnectionFactory cf) {
        SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(cf);
        container.setQueueNames("so48747564");
        container.setMessageListener(new MessageListenerAdapter(new Object() {

            @SuppressWarnings("unused")
            public void handleMessage(String in) throws Exception {
                System.out.println("Received: " + in);
                Thread.sleep(5_000);
                System.out.println("Listener exiting");
            }

        }));
        container.setShutdownTimeout(10_000);
        return container;
    }

    @Bean
    public Queue queue() {
        return new Queue("so48747564");
    }

}

结果:

foo
Received: foo
Listener exiting
quit
Received: quit
2018-02-12 13:31:23.585  INFO 14916 --- [           main] o.s.a.r.l.SimpleMessageListenerContainer 
: Waiting for workers to finish.
Listener exiting
2018-02-12 13:31:28.589  INFO 14916 --- [           main] o.s.a.r.l.SimpleMessageListenerContainer 
: Successfully waited for workers to finish.