0

I am Trying to publish messages two different topics from single producer.

here i created two topics:

   @Bean
    public NewTopic multi1() {
        return TopicBuilder.name("multi1").partitions(1).build();
    }
    @Bean
    public NewTopic multi2() {
        return TopicBuilder.name("multi2").partitions(1).build();
    }

this is how iam sending the messages to two topics:

public void sendingtomultitopic()
{
    IntStream.range(0, 100).forEach(i->this.template.send("multi1", "mutli1 data value->"+i));      
    IntStream.range(0, 100).forEach(i->this.template.send("multi2", "multi2 data value->"+i));
    logger.info("sending finished");
}

is above method is correct way to send to multiple topics?

here below iam trying to consume messages

@KafkaListener(topics = {"multi1,multi2"}, groupId = "diffgroupid3")
    public void consumingfrommultitopics(String data) {
        logger.info(String.format("consumingfromtwotopics -> %s", data));
    }

exception iam receiving:

org.apache.kafka.common.errors.InvalidTopicException: Invalid topics: [multi1,multi2]

iam able to the published data in those two topics.

but this consumer is not retrieving any messages, please help me here?

Bharath
  • 81
  • 1
  • 11

2 Answers2

2

You can use from config (application.yml)

test:
  kafka:
    sender:
      serverAddress: xxxxx:port
    topic:
      consumer: topic1,topic2

@KafkaListener(topics = "#{'${test.kafka.topic}'.split(',')}"
  public void consumingfrommultitopics(String data) {
        logger.info(String.format("consumingfromtwotopics -> %s", data));
    }
Nagihan Sevgi
  • 53
  • 1
  • 6
2

You have a typo in 'topics' form, should be

@KafkaListener(topics = {"multi1","multi2"}...

(check out Can a single Spring's KafkaConsumer listener listens to multiple topic?)

You can also have two separate @KafkaListener, each for every topic. What works best for your use-case.

H. Opler
  • 351
  • 3
  • 8