在Kafka中使用两个Kafka生产商工厂嵌入式单元测试

时间:2018-08-17 12:15:52

标签: unit-testing apache-kafka spring-kafka

我使用KafkaEmbedded进行消费者单元测试,其中我有两个创建两个具有两个不同序列化器和两个不同主题的不同生产者:

let $doc := <acts>
<Activities datatype="array">
    <Activity>
      <ActivityCrewSize>10</ActivityCrewSize>
      <ActivitySeqNo>1</ActivitySeqNo>
      <ActivityDesc/>
    </Activity>
</Activities>
<Activities datatype="array">
    <Activity>
      <ActivityCrewSize>23</ActivityCrewSize>
      <ActivitySeqNo>2</ActivitySeqNo>
      <ActivityDesc/>
    </Activity>
</Activities>
<Activities datatype="array">
    <Activity>
      <ActivityCrewSize>50</ActivityCrewSize>
      <ActivitySeqNo>3</ActivitySeqNo>
      <ActivityDesc/>
    </Activity>
</Activities>
</acts>
return 
<Activities datatype="array">
{
for $each in $doc/Activities/Activity
return $each
}

然后,如here所述,我将分区分配给生产者:

// first producer factory
Map<String, Object> firstProducerProperties = KafkaTestUtils.producerProps(kafkaEmbedded);
firstProducerProperties.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
firstProducerProperties.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, KafkaAvroSerializer.class);
firstProducerProperties.put(KafkaAvroSerializerConfig.SCHEMA_REGISTRY_URL_CONFIG, properties.getKafka().getSchemaRegistryUrl());

ProducerFactory<String, TicketChange> firstProducerFactory =
    new DefaultKafkaProducerFactory<>(firstProducerProperties);

firstTemplate = new KafkaTemplate<>(firstProducerFactory);
firstTemplate.setDefaultTopic("topic-one");

...

 // second producer factory
Map<String, Object> secondProducerProperties = KafkaTestUtils.producerProps(kafkaEmbedded);
secondProducerProperties.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
secondProducerProperties.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class);

ProducerFactory<String, TicketChange> secondProducerFactory =
    new DefaultKafkaProducerFactory<>(secondProducerProperties);

secondTemplate = new KafkaTemplate<>(secondProducerFactory);
secondTemplate.setDefaultTopic("topic-two")

最后一行分区数乘以主题数。如果运行测试,则会出现以下错误:

for (MessageListenerContainer messageListenerContainer : kafkaListenerEndpointRegistry
    .getListenerContainers()) {

  ContainerTestUtils.waitForAssignment(messageListenerContainer,
      kafkaEmbedded.getPartitionsPerTopic() * 2);
}

就像我必须以某种方式添加其他侦听器一样?每个生产者的消息处理程序?

0 个答案:

没有答案