title: How to perform a graceful application shutdown when using Spring Cloud Stream with Kafka?
body: <p>I have a spring boot (v.1.57) application which uses Spring Cloud Stream (v1.3.0) and Kafka (v1.1.6). I want to be able to gracefully shut it down, i.e., when shutting down, all stream listeners (i.e., annotated with @StreamListener) should:</p>
  
  <ol>
  <li>Stop polling new messages</li>
  <li>Finish their work</li>
  <li>Commit the offset to Kafka</li>
  </ol>
  
  <p>I noticed that there's a property called 'shutdownTimeout' in ContainerProperties (which is set to a default of 10000ms) so I've tried to modify it to 30000 by extending ConcurrentKafkaListenerContainerFactoryConfigurer class (Since it has a @ConditionalOnMissingBean annotation) via reflection like so:</p>
  
  <pre class="lang-java prettyprint-override"><code>@Slf4j
  @Component
  public class BehalfConcurrentKafkaListenerContainerFactoryConfigurer extends ConcurrentKafkaListenerContainerFactoryConfigurer {
  
      @Autowired
      private KafkaProperties kproperties;
  
      @Override
      public void configure(ConcurrentKafkaListenerContainerFactory&lt;Object, Object&gt; listenerContainerFactory,
                            ConsumerFactory&lt;Object, Object&gt; consumerFactory) {
          PropertyAccessor myAccessor = PropertyAccessorFactory.forDirectFieldAccess(this);
          myAccessor.setPropertyValue("properties", kproperties);
  
          ContainerProperties containerProperties = listenerContainerFactory
                  .getContainerProperties();
          super.configure(listenerContainerFactory, consumerFactory);
          containerProperties.setShutdownTimeout(30000);
      }
  }
  </code></pre>
  
  <p>But it wasn't successful. Also tried putting it (shutdownTimeout: 30000) in application.yml under the spring cloud stream binder settings, but again it didn't help.</p>
  
  <p>Is there any way to control the shutdown process and achieve my goals?</p>