Is it possible to use Micronaut Kafka consumer dynamically without pre-defining the @Topic("TopicName")

801 views Asked by At

I Have the following code, i want to provide the "topicName" as paramater or read it dynamically from a property, is the possible

@KafkaListener(offsetReset = OffsetReset.EARLIEST)
public class KafkaConsumer {

    private final String topicName;

    public KafkaConsumer(String topicName) {
        this.topicName = topicName;
    }

    @Topic("topicName")
    public void receive(@KafkaKey String day, String message) {
        System.out.println("Got Message for the  - " + day + " and Message is  " + message);
    }

}
2

There are 2 answers

1
Denis On

You can do:

@Topic("${myTopicFromProperties}")
2
Dmitry Khamitov On

It's a bit confusing because if the question to be combined with the OP's comment "i need to use a different topic name in every method call, such as getting the topic name from the user and then creating a listener" one might think of the following examples/scenarios based on the official doc:

@Topic({"topic1", "topic2", "topic3}) //multiple topics, see "Specifying Topics" sub-section on the linked page
public void receive(
    @KafkaKey String key,
    String message,
    long offset, 
    int partition, 
    String topic, // topic as a parameter
    long timestamp
) { 
    System.out.println("Got message: " + message + " from topic: " + topic);
}

You can also use ConsumerRecord and get all the necessary information from there:

// "Receiving a ConsumerRecord" sub-section on the linked page
@Topic({"topic1", "topic2", "topic3})
public void receive(ConsumerRecord<String, String> record) { 
    System.out.println("Got message: " + record.value() + " from topic: " + record.topic());
}

You should also be able to specify the topics through the property placeholders as found in another answer like @Topic({"${topic1}", "${topic2}", "${topic3}"}).

P.S. The above examples assume that for each specified topic both the message key and the message body are deserialized to strings.