SpringBoot kafka request/reply with dynmaic topic name for @KafkaListener

156 views Asked by At

i am writing a microservices application based on kafka request/reply semantic, so i got configured ReplyingKafkaTemplate as message producer and @KafkaListener with @SendTo annotations method as the service request listener. I need to create a class with method marked with @KafkaListeners and @SendTo annotations dynamically based on topic name. It should be something like this:

@Component
class KafkaReceiver {

    // LISTENER FOR OTHER MICROSERVICE REQUESTS
    @KafkaListener("#{topicProvider.getTopic()})
    @SendTo
    public Response listen(Request request) {
       ... some logic
       return response;
    }
}

@Component
class KafkaRecevierFactory {
    
     public KafkaReceiver createListener(String topic) {
         ... 
         return kafkaReceiver;
     }

     //OR SOMETHING LIKE THIS:
     public void runListenerContainer(String topic, ReqeustProcessor processor) {
         Container container = ContainerFactory.create(topic)
         container.setListener( request -> { 
                                  Response resp = processor.process(request);
                                  return resp;
                               });
          container.start();
     }

}

Is there anyway i can do this?

0

There are 0 answers