Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/370.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 通用弹簧卡夫卡侦听器_Java_Spring_Maven_Spring Boot - Fatal编程技术网

Java 通用弹簧卡夫卡侦听器

Java 通用弹簧卡夫卡侦听器,java,spring,maven,spring-boot,Java,Spring,Maven,Spring Boot,我在一个maven项目中制作了卡夫卡制作人和消费者 我想在另一个maven项目中使用它,所以我添加了上述kafka项目的依赖项。现在的问题是生产者是好的,但如何使监听器通用,可以覆盖所有其他项目添加此项目 目前我在一个项目中有一个监听器 public class Listener { public CountDownLatch countDownLatch0 = new CountDownLatch(3); public CountDownLatch countDownLatch

我在一个maven项目中制作了卡夫卡制作人和消费者

我想在另一个maven项目中使用它,所以我添加了上述kafka项目的依赖项。现在的问题是生产者是好的,但如何使监听器通用,可以覆盖所有其他项目添加此项目

目前我在一个项目中有一个监听器

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();
    }
公共类侦听器{
公共倒计时闩锁倒计时闩锁0=新倒计时闩锁(3);
公共倒计时闩锁倒计时闩锁1=新倒计时闩锁(3);
公共倒计时闩锁倒计时闩锁2=新倒计时闩锁(3);
@卡夫卡列斯汀(id=“id0”,topicPartitions={@TopicPartition(topic=“SpringKafkaTopic1”,partitions={“0”})
公共无效列表nPartition0(消费者记录记录){
System.out.println(“侦听器Id0,线程ID:+Thread.currentThread().getId());
系统输出打印项次(“接收:+记录);
countDownLatch0.countDown();
}
@卡夫卡列斯汀(id=“id1”,topicPartitions={@TopicPartition(topic=“SpringKafkaTopic1”,partitions={“1”})
公共作废列表分区1(消费者记录记录){
System.out.println(“侦听器Id1,线程ID:+Thread.currentThread().getId());
系统输出打印项次(“接收:+记录);
倒计时锁1.倒计时();
}
@卡夫卡列斯汀(id=“id2”,topicPartitions={@TopicPartition(topic=“SpringKafkaTopic1”,partitions={“2”})
公共作废列表分区2(消费者记录记录){
System.out.println(“侦听器Id2,线程ID:+Thread.currentThread().getId());
系统输出打印项次(“接收:+记录);
倒计时锁2.倒计时();
}

我如何制作通用侦听器,它可以被所有其他项目覆盖,所有其他项目都将此项目添加为依赖项,并且可以侦听各自的主题

如果我正确理解了您的问题,我认为您需要这样做:

在shared.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();
  }
}
公共抽象类侦听器{
公共倒计时闩锁倒计时闩锁0=新倒计时闩锁(3);
公共倒计时闩锁倒计时闩锁1=新倒计时闩锁(3);
公共倒计时闩锁倒计时闩锁2=新倒计时闩锁(3);
摘要无效手册第0部分(记录);
摘要无效手册第1部分(记录);
摘要无效手册第2部分(记录);
@卡夫卡列斯汀(id=“id0”,topicPartitions={@TopicPartition(topic=“SpringKafkaTopic1”,partitions={“0”})
公共无效列表nPartition0(消费者记录记录){
handlePartition0(记录);
countDownLatch0.countDown();
}
@卡夫卡列斯汀(id=“id1”,topicPartitions={@TopicPartition(topic=“SpringKafkaTopic1”,partitions={“1”})
公共作废列表分区1(消费者记录记录){
handlePartition1(记录);
倒计时锁1.倒计时();
}
@卡夫卡列斯汀(id=“id2”,topicPartitions={@TopicPartition(topic=“SpringKafkaTopic1”,partitions={“2”})
公共作废列表分区2(消费者记录记录){
手册第2部分(记录);
倒计时锁2.倒计时();
}
}
然后在子项目中,导入shared.jar和handleXXX方法

public class MyChildListener extends Listener {
  public void handlePartition0(Record<?,?> r) { 
    // do something useful
  }
  ...
}
公共类MyChildListener扩展了Listener{
公共无效句柄0(记录r){
//做些有用的事
}
...
}
这可能对你有用