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.