use of org.springframework.kafka.listener.ContainerProperties in project goodsKill by techa03.
the class KafkaTest method testAutoCommit.
@Test
public void testAutoCommit() throws Exception {
logger.info("Start auto");
ContainerProperties containerProps = new ContainerProperties("topic1", "topic2");
final CountDownLatch latch = new CountDownLatch(4);
// containerProps.setMessageListener(new MessageListener<Integer, String>() {
//
// @Override
// public void onMessage(ConsumerRecord<Integer, String> message) {
// logger.info("received: " + message);
// latch.countDown();
// }
//
// });
KafkaMessageListenerContainer<Integer, String> container = createContainer(containerProps);
container.setBeanName("testAuto");
container.start();
// wait a bit for the container to start
Thread.sleep(1000);
KafkaTemplate<Integer, String> template = createTemplate();
template.setDefaultTopic("goodskill-kafka");
template.sendDefault(0, "foo");
template.sendDefault(2, "bar");
template.sendDefault(0, "baz");
template.sendDefault(2, "qux");
template.flush();
assertTrue(latch.await(60, TimeUnit.SECONDS));
container.stop();
logger.info("Stop auto");
}
use of org.springframework.kafka.listener.ContainerProperties in project spring-kafka by spring-projects.
the class SerializationIntegrationTests method configurePreLoadedDelegates.
@Test
void configurePreLoadedDelegates() {
Map<String, Object> consumerProps = KafkaTestUtils.consumerProps(DBTD_TOPIC, "false", EmbeddedKafkaCondition.getBroker());
consumerProps.put(DelegatingByTopicDeserializer.VALUE_SERIALIZATION_TOPIC_CONFIG, DBTD_TOPIC + ":" + TestDeserializer.class.getName());
TestDeserializer testDeser = new TestDeserializer();
DelegatingByTopicDeserializer delegating = new DelegatingByTopicDeserializer(Map.of(Pattern.compile(DBTD_TOPIC), testDeser), new StringDeserializer());
DefaultKafkaConsumerFactory<String, Object> cFact = new DefaultKafkaConsumerFactory<String, Object>(consumerProps, new StringDeserializer(), delegating);
ContainerProperties props = new ContainerProperties(DBTD_TOPIC);
props.setCheckDeserExWhenKeyNull(true);
props.setCheckDeserExWhenValueNull(true);
props.setMessageListener(mock(MessageListener.class));
KafkaMessageListenerContainer<String, Object> container = new KafkaMessageListenerContainer<>(cFact, props);
container.start();
assertThat(KafkaTestUtils.getPropertyValue(container, "listenerConsumer.consumer.valueDeserializer")).isSameAs(delegating);
Map<?, ?> delegates = KafkaTestUtils.getPropertyValue(delegating, "delegates", Map.class);
assertThat(delegates).hasSize(1);
assertThat(delegates.values().iterator().next()).isSameAs(testDeser);
assertThat(testDeser.configured).isTrue();
assertThat(KafkaTestUtils.getPropertyValue(container, "listenerConsumer.checkNullKeyForExceptions", Boolean.class)).isTrue();
assertThat(KafkaTestUtils.getPropertyValue(container, "listenerConsumer.checkNullValueForExceptions", Boolean.class)).isTrue();
container.stop();
}
use of org.springframework.kafka.listener.ContainerProperties in project spring-kafka by spring-projects.
the class ReplyingKafkaTemplateTests method aggregatingTemplate.
public AggregatingReplyingKafkaTemplate<Integer, String, String> aggregatingTemplate(TopicPartitionOffset topic, int releaseSize, AtomicInteger releaseCount) {
ContainerProperties containerProperties = new ContainerProperties(topic);
containerProperties.setAckMode(AckMode.MANUAL_IMMEDIATE);
Map<String, Object> consumerProps = KafkaTestUtils.consumerProps(this.testName, "false", embeddedKafka);
DefaultKafkaConsumerFactory<Integer, Collection<ConsumerRecord<Integer, String>>> cf = new DefaultKafkaConsumerFactory<>(consumerProps);
KafkaMessageListenerContainer<Integer, Collection<ConsumerRecord<Integer, String>>> container = new KafkaMessageListenerContainer<>(cf, containerProperties);
container.setBeanName(this.testName);
AggregatingReplyingKafkaTemplate<Integer, String, String> template = new AggregatingReplyingKafkaTemplate<>(this.config.pf(), container, (list, timeout) -> {
releaseCount.incrementAndGet();
return list.size() == releaseSize;
});
template.setSharedReplyTopic(true);
template.start();
assertThat(template.getAssignedReplyTopicPartitions()).hasSize(1);
assertThat(template.getAssignedReplyTopicPartitions().iterator().next().topic()).isEqualTo(topic.getTopic());
return template;
}
use of org.springframework.kafka.listener.ContainerProperties in project spring-kafka by spring-projects.
the class ReplyingKafkaTemplateTests method nullDuration.
@SuppressWarnings({ "rawtypes", "unchecked" })
@Test
void nullDuration() throws Exception {
ProducerFactory pf = mock(ProducerFactory.class);
Producer producer = mock(Producer.class);
willAnswer(invocation -> {
Callback callback = invocation.getArgument(1);
SettableListenableFuture<Object> future = new SettableListenableFuture<>();
future.set("done");
callback.onCompletion(new RecordMetadata(new TopicPartition("foo", 0), 0L, 0, 0L, 0, 0), null);
return future;
}).given(producer).send(any(), any());
given(pf.createProducer()).willReturn(producer);
GenericMessageListenerContainer container = mock(GenericMessageListenerContainer.class);
ContainerProperties properties = new ContainerProperties("two");
given(container.getContainerProperties()).willReturn(properties);
ReplyingKafkaTemplate template = new ReplyingKafkaTemplate(pf, container);
template.start();
Message<?> msg = MessageBuilder.withPayload("foo".getBytes()).setHeader(KafkaHeaders.TOPIC, "foo").build();
// was NPE here
template.sendAndReceive(new ProducerRecord("foo", 0, "bar", "baz"), null).getSendFuture().get();
}
use of org.springframework.kafka.listener.ContainerProperties in project spring-kafka by spring-projects.
the class ReplyingKafkaTemplateTests method createTemplate.
public ReplyingKafkaTemplate<Integer, String, String> createTemplate(String topic, boolean badDeser) throws Exception {
ContainerProperties containerProperties = new ContainerProperties(topic);
final CountDownLatch latch = new CountDownLatch(1);
containerProperties.setConsumerRebalanceListener(new ConsumerRebalanceListener() {
@Override
public void onPartitionsRevoked(Collection<TopicPartition> partitions) {
// no op
}
@Override
public void onPartitionsAssigned(Collection<TopicPartition> partitions) {
latch.countDown();
}
});
Map<String, Object> consumerProps = KafkaTestUtils.consumerProps(this.testName, "false", embeddedKafka);
if (badDeser) {
consumerProps.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, ErrorHandlingDeserializer.class);
consumerProps.put(ErrorHandlingDeserializer.VALUE_DESERIALIZER_CLASS, BadDeser.class);
}
DefaultKafkaConsumerFactory<Integer, String> cf = new DefaultKafkaConsumerFactory<>(consumerProps);
KafkaMessageListenerContainer<Integer, String> container = new KafkaMessageListenerContainer<>(cf, containerProperties);
container.setBeanName(this.testName);
ReplyingKafkaTemplate<Integer, String, String> template = new ReplyingKafkaTemplate<>(this.config.pf(), container);
template.setSharedReplyTopic(true);
template.start();
assertThat(latch.await(30, TimeUnit.SECONDS)).isTrue();
assertThat(template.getAssignedReplyTopicPartitions()).hasSize(5);
assertThat(template.getAssignedReplyTopicPartitions().iterator().next().topic()).isEqualTo(topic);
return template;
}
Aggregations