Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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
...
like image 948
shani Avatar asked Jan 23 '26 17:01

shani


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);
}
like image 114
Gary Russell Avatar answered Jan 25 '26 11:01

Gary Russell



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!