1

我正在编写一个基于 kafka 请求/回复语义的微服务应用程序,因此我将 ReplyingKafkaTemplate 配置为消息生产者,将带有 @SendTo 注释方法的 @KafkaListener 配置为服务请求侦听器。我需要根据主题名称动态地创建一个带有@KafkaListeners 和@SendTo 注释标记的方法的类。它应该是这样的:

@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();
     }

}

无论如何我可以做到这一点吗?

4

0 回答 0