简体   繁体   中英

@KafkaListener with multiples independent topics processing

Software used:

  • Java version: 8
  • SpringBoot version: 2.4.0
  • SpringKafka version: 2.7.2

I have this method in my spring:

    @KafkaListener(topics="#{consumerSpring.topics}", groupId="#{consumerSpring.consumerId}", concurrency="#{consumerSpring.recommendedConcurrency}")
    public void listenKafkbooiaTopic(@Header(KafkaHeaders.RECEIVED_TOPIC) String topicName, @Payload String message, Acknowledgment ack) throws Exception {
        ConsumerSpring consumer = this.consumerSpring();
        //
        //
        KafkaHandlerReturn handlerReturn = consumer.getKafkaProxy().handleRequest(
                topicName,
                consumer.getConsumerId(),
                message);
        
        if (handlerReturn.equals(KafkaHandlerReturn.SUCCESS) || handlerReturn.equals(KafkaHandlerReturn.FAIL_LOGIC)) {
            ack.acknowledge();
        } else {
            ack.nack(5 * 1000);
        }
    }

#{consumerSpring.topics} returns

{"topic1", "topic2", "topic3"}

#{consumerSpring.consumerId} returns:

myConsumer

#{consumerSpring.recommendedConcurrency} returns:

3

OK! This is working fine! But I need isolate these topics, for example:

TopicA is stuck in fatal error and it's calling:

ack.nack(5 * 1000);

But the topics: TopicB and TopicC aren't stuck. Then I need that these topics continue the execution normally.

Basically I need the same behavior as if I declared two separate structures, example:

    @KafkaListener(topics="topica", groupId="#{consumerSpring.consumerId}")
    public void listenerTopicB(@Header(KafkaHeaders.RECEIVED_TOPIC) String topicName, @Payload String message, Acknowledgment ack) throws Exception {
        ConsumerSpring consumer = this.consumerSpring();
        //
        //
        KafkaHandlerReturn handlerReturn = consumer.getKafkaProxy().handleRequest(
                topicName,
                consumer.getConsumerId(),
                message);
        
        if (handlerReturn.equals(KafkaHandlerReturn.SUCCESS) || handlerReturn.equals(KafkaHandlerReturn.FAIL_LOGIC)) {
            ack.acknowledge();
        } else {
            ack.nack(5 * 1000);
        }
    }
    
    @KafkaListener(topics="topicb", groupId="#{consumerSpring.consumerId}")
    public void listenerTopicA(@Header(KafkaHeaders.RECEIVED_TOPIC) String topicName, @Payload String message, Acknowledgment ack) throws Exception {
        ConsumerSpring consumer = this.consumerSpring();
        //
        //
        KafkaHandlerReturn handlerReturn = consumer.getKafkaProxy().handleRequest(
                topicName,
                consumer.getConsumerId(),
                message);
        
        if (handlerReturn.equals(KafkaHandlerReturn.SUCCESS) || handlerReturn.equals(KafkaHandlerReturn.FAIL_LOGIC)) {
            ack.acknowledge();
        } else {
            ack.nack(5 * 1000);
        }
    }

There is no need for multiple methods.

You can put multiple @KafkaListener annotations on a single method and each one will create a separate container.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM