Q:

Implementing shared logic for multiple KafkaListeners in spring-kafka

My Spring Boot application contains several @KafkaListeners, 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.
These steps are currently implemented in a base class, but because the topics passed to @KafkaListener 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.
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 KafkaListenerAnnotationBeanPostProcessor, it seems to be quite involved.
Does anyone have any recommendadtions?

A:

How about this:
public abstract class BaseKafkaProcessingLogic {

     @KafkaHandler
     public void handle(Object payload) {

     }

}

@KafkaListener(topics = "topic1")
public class Topic1Handler extends BaseKafkaProcessingLogic {

}

@KafkaListener(topics = "topic2")
public class Topic2Handler extends BaseKafkaProcessingLogic {

}

?

