how to set Shutdown parameters for rabbit mq listener



we are trying to shutdown all process which are all running in micro service

  1. Any Consumer currently doing some work when trying to ack their current message
  2. Any other action on the channel. I’d like to let the consumers finish whatever message they’re processing and then close everything down.

  3. if we stop the running process and allow the transfer of data then wait until the specified time period mentioned in shutdown hook timeout and then it gets terminated.

we have created bean like below

 @Bean
  public SimpleMessageListenerContainer SimpleMessageListenerContainer() {

      SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(rabbitConnectionFactory);
      container.setIdleEventInterval(60000L);
      container.setShutdownTimeout(900000); // tried 
      return container;
  }

with above bean tried to achieve but it did’t work. we are using spring-rabbit 1.7.3.RELEASE.

log:

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).

have tried with setting shutdown properties to container but still it gets shutdown by default sec or before that ;and not with override set properties.

  • Is it a better way to set for each and every inbound-channel-adapter the shutdown properties, if that’s the case we have to configure for more than 100 channel
  • we have tested by terminate button in STS and by ctr+c in console for above 1 and 2 point to achieve but it’s not waiting for mentioned shutdown time.kindly help on this.

  • is there any way to determine the duration of shutdown time run.

Answer

It works exactly as you have it…

@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");
    }

}

Result:

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.


Source: stackoverflow