title: Implementing shared logic for multiple KafkaListeners in spring-kafka
body: <p>My Spring Boot application contains several <a href="https://docs.spring.io/spring-kafka/docs/2.1.6.RELEASE/reference/html/_reference.html#kafka-listener-annotation" rel="nofollow noreferrer"><code>@KafkaListener</code>s</a>, and each listener performs the same steps before and after actually processing the payload: Validate the payload, check whether the event has been processed already, check whether it's a tombstone (null) message, decide whether processing should be retried in case of failure, emit metrics, etc.</p>
  
  <p>These steps are currently implemented in a base class, but because the topics passed to <code>@KafkaListener</code> must be constant at runtime, the method annotated with @KafkaListener is defined in the subclass, and does nothing but pass its parameters to a method in the base class.</p>
  
  <p>This works just fine, but I wonder if there's a more elegant solution. I assume my base class would have to create a listener container programmatically, but after a quick look at <a href="https://github.com/spring-projects/spring-kafka/blob/b25e1441bdaa8d161415771649c2339af8da9e86/spring-kafka/src/main/java/org/springframework/kafka/annotation/KafkaListenerAnnotationBeanPostProcessor.java" rel="nofollow noreferrer"><code>KafkaListenerAnnotationBeanPostProcessor</code></a>, it seems to be quite involved.</p>
  
  <p>Does anyone have any recommendadtions?</p>