简体   繁体   English

通用Spring Kafka监听器

[英]Generic Spring Kafka Listener

I have made a kafka producer and consumer in one maven project. 我在一个Maven项目中让kafka生产者和消费者成为了一个。

I want to use it in another maven project, so I added the dependency of the above kafka project. 我想在另一个Maven项目中使用它,因此我添加了上述kafka项目的依赖项。 Now the problem is producer is fine but how to make listener generic that can be override by all other project which add this project. 现在的问题是生产者很好,但是如何使侦听器通用,而添加该项目的所有其他项目都可以覆盖它。

Currently I have Listener in one project 目前我在一个项目中有监听器

public class Listener {
    public CountDownLatch countDownLatch0 = new CountDownLatch(3);
    public CountDownLatch countDownLatch1 = new CountDownLatch(3);
    public CountDownLatch countDownLatch2 = new CountDownLatch(3);

    @KafkaListener(id = "id0", topicPartitions = { @TopicPartition(topic = "SpringKafkaTopic1", partitions = { "0" }) })
    public void listenPartition0(ConsumerRecord<?, ?> record) {
        System.out.println("Listener Id0, Thread ID: " + Thread.currentThread().getId());
        System.out.println("Received: " + record);
        countDownLatch0.countDown();
    }

    @KafkaListener(id = "id1", topicPartitions = { @TopicPartition(topic = "SpringKafkaTopic1", partitions = { "1" }) })
    public void listenPartition1(ConsumerRecord<?, ?> record) {
        System.out.println("Listener Id1, Thread ID: " + Thread.currentThread().getId());
        System.out.println("Received: " + record);
        countDownLatch1.countDown();
    }

    @KafkaListener(id = "id2", topicPartitions = { @TopicPartition(topic = "SpringKafkaTopic1", partitions = { "2" }) })
    public void listenPartition2(ConsumerRecord<?, ?> record) {
        System.out.println("Listener Id2, Thread ID: " + Thread.currentThread().getId());
        System.out.println("Received: " + record);
        countDownLatch2.countDown();
    }

How do I make generic listener which can be overriden by all other project who all add this project as dependency and can listen to their respective topics 我如何制作可以被所有其他项目覆盖的通用侦听器,其他所有项目都将该项目添加为依赖项并可以收听其各自的主题

If i'm understanding your question properly, I think you'd need to do something like this: 如果我正确理解了您的问题,我认为您需要执行以下操作:

In the shared .jar ... 在共享的.jar中...

public abstract class Listener {
  public CountDownLatch countDownLatch0 = new CountDownLatch(3);
  public CountDownLatch countDownLatch1 = new CountDownLatch(3);
  public CountDownLatch countDownLatch2 = new CountDownLatch(3);


  abstract void handlePartition0(record);
  abstract void handlePartition1(record);
  abstract void handlePartition2(record);

  @KafkaListener(id = "id0", topicPartitions = { @TopicPartition(topic = "SpringKafkaTopic1", partitions = { "0" }) })
  public void listenPartition0(ConsumerRecord<?, ?> record) {
    handlePartition0(record);
    countDownLatch0.countDown();
  }

  @KafkaListener(id = "id1", topicPartitions = { @TopicPartition(topic = "SpringKafkaTopic1", partitions = { "1" }) })
  public void listenPartition1(ConsumerRecord<?, ?> record) {
    handlePartition1(record);
    countDownLatch1.countDown();
  }

  @KafkaListener(id = "id2", topicPartitions = { @TopicPartition(topic = "SpringKafkaTopic1", partitions = { "2" }) })
  public void listenPartition2(ConsumerRecord<?, ?> record) {
    handlePartition2(record);
    countDownLatch2.countDown();
  }
}

Then in the child projects, import the shared .jar and import the handleXXX methods. 然后在子项目中,导入共享的.jar并导入handleXXX方法。

public class MyChildListener extends Listener {
  public void handlePartition0(Record<?,?> r) { 
    // do something useful
  }
  ...
}

This might work for you. 这可能对您有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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