Can I apply graceful shutdown when using Spring Cloud Stream Kafka 3.0.3.RELEASE?

Viewed 1068

I developed the kafka consumer application based on Spring Cloud Stream 3.0.3.RELEASE. (SpringBoot 2.3.4.RELEASE)

When I stop this application, I want the consumer to gracefully shut down. Similar Questions

  1. Stop polling new messages
  2. Finish their work and Commit the offset to Kafka
  3. Gracefully shut down application

Does the spring cloud stream default this work? Then is there a related document?


For your information, I am using spring cloud stream kafka as below.

#Message handler
@Component
public class MessageHandler {

    @Bean
    public Consumer<MyEvent> handleMessage() {
        return message -> {...}
    }
...
}


#application.yml
spring:
  cloud:
    stream:
      bindings:
        handleMessage-in-0:
          destination: myevent
          group: test-group
          consumer:
            maxAttempts: 2
            concurrency: 10
      function:
        definition: handleMessage
...
1 Answers

As mentioned in the answer you referenced, you can increase the shutdownTimeout container property (default 10 seconds) with a ListenerContainerCustomizer bean.

@Bean
public ListenerContainerCustomizer<AbstractMessageListenerContainer<?, ?>> customizer() {
    return (container, dest, group) -> container.getContainerProperties()
            .setShutdownTimeout(30_000L);
}
Related