Pattern confirmation consumer / producer without a

2019-08-17 14:35发布

问题:

to achieve to start a spring boot application without a needed running kafka server I defined the consumer with autostartup=false in this way:

 @KafkaListener(id = "myContainer", topics = {"myTopic"}, autoStartup = "false")
    public void receive(ConsumerRecord<String, Foo> consumerRecord) {
        log.info("consuming {}", consumerRecord);
    }

Now to start initially the consumer I tied it to the producer of this topic in the producer in this way:

public void sendFoo(Foo foo) {

        try {
            kafkaListenerEndpointRegistry.getListenerContainer("myContainer").start();
        }catch(Exception e){
            log.info(e.getMessage());
        }
        kafka.send(mytopic, foo);
    }

Is this the recommended pattern or does a better one exist ?