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(TopicPartitionOffset topic) {
ContainerProperties containerProperties = new ContainerProperties(topic);
Map<String, Object> consumerProps = KafkaTestUtils.consumerProps(this.testName, "false", embeddedKafka);
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(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 testAggregateOrphansNotStored.
@SuppressWarnings({ "rawtypes", "unchecked" })
@Test
public void testAggregateOrphansNotStored() throws Exception {
GenericMessageListenerContainer container = mock(GenericMessageListenerContainer.class);
ContainerProperties properties = new ContainerProperties("two");
properties.setAckMode(AckMode.MANUAL);
given(container.getContainerProperties()).willReturn(properties);
ProducerFactory pf = mock(ProducerFactory.class);
Producer producer = mock(Producer.class);
given(pf.createProducer()).willReturn(producer);
AtomicReference<byte[]> correlation = new AtomicReference<>();
willAnswer(invocation -> {
ProducerRecord rec = invocation.getArgument(0);
correlation.set(rec.headers().lastHeader(KafkaHeaders.CORRELATION_ID).value());
return new SettableListenableFuture<>();
}).given(producer).send(any(), any());
AggregatingReplyingKafkaTemplate template = new AggregatingReplyingKafkaTemplate(pf, container, (list, timeout) -> true);
template.setDefaultReplyTimeout(Duration.ofSeconds(30));
template.start();
List<ConsumerRecord> records = new ArrayList<>();
ConsumerRecord record = new ConsumerRecord("two", 0, 0L, null, "test1");
RequestReplyFuture future = template.sendAndReceive(new ProducerRecord("one", null, "test"));
record.headers().add(new RecordHeader(KafkaHeaders.CORRELATION_ID, correlation.get()));
records.add(record);
Consumer consumer = mock(Consumer.class);
template.onMessage(records, consumer);
assertThat(future.get(10, TimeUnit.SECONDS)).isNotNull();
assertThat(KafkaTestUtils.getPropertyValue(template, "pending", Map.class)).hasSize(0);
Map<TopicPartition, OffsetAndMetadata> offsets = new HashMap<>();
offsets.put(new TopicPartition("two", 0), new OffsetAndMetadata(1));
verify(consumer).commitSync(offsets, Duration.ofSeconds(30));
// simulate redelivery after completion
template.onMessage(records, consumer);
assertThat(KafkaTestUtils.getPropertyValue(template, "pending", Map.class)).hasSize(0);
template.stop();
template.destroy();
}
use of org.springframework.kafka.listener.ContainerProperties in project spring-kafka by spring-projects.
the class DefaultKafkaConsumerFactoryTests method testContainerTxProducerIsNotCached.
@SuppressWarnings("unchecked")
@Test
public void testContainerTxProducerIsNotCached() throws Exception {
Map<String, Object> producerProps = KafkaTestUtils.producerProps(this.embeddedKafka);
DefaultKafkaProducerFactory<Integer, String> pf = new DefaultKafkaProducerFactory<>(producerProps);
KafkaTemplate<Integer, String> template = new KafkaTemplate<>(pf);
DefaultKafkaProducerFactory<Integer, String> pfTx = new DefaultKafkaProducerFactory<>(producerProps);
pfTx.setTransactionIdPrefix("fooTx.");
KafkaTemplate<Integer, String> templateTx = new KafkaTemplate<>(pfTx);
Map<String, Object> consumerProps = KafkaTestUtils.consumerProps("txCache2Group", "false", this.embeddedKafka);
DefaultKafkaConsumerFactory<Integer, String> cf = new DefaultKafkaConsumerFactory<>(consumerProps);
ContainerProperties containerProps = new ContainerProperties("txCache2");
CountDownLatch latch = new CountDownLatch(1);
containerProps.setMessageListener((MessageListener<Integer, String>) r -> {
templateTx.send("txCacheSendFromListener", "bar");
templateTx.send("txCacheSendFromListener", "baz");
latch.countDown();
});
KafkaTransactionManager<Integer, String> tm = new KafkaTransactionManager<>(pfTx);
containerProps.setTransactionManager(tm);
KafkaMessageListenerContainer<Integer, String> container = new KafkaMessageListenerContainer<>(cf, containerProps);
container.start();
try {
ListenableFuture<SendResult<Integer, String>> future = template.send("txCache2", "foo");
future.get(10, TimeUnit.SECONDS);
assertThat(latch.await(30, TimeUnit.SECONDS)).isTrue();
assertThat(KafkaTestUtils.getPropertyValue(pfTx, "cache", Map.class)).hasSize(0);
} finally {
container.stop();
pf.destroy();
pfTx.destroy();
}
}
use of org.springframework.kafka.listener.ContainerProperties in project spring-cloud-contract by spring-cloud.
the class StubRunnerKafkaConfiguration method registerContainers.
private void registerContainers(ConfigurableListableBeanFactory beanFactory, List<Contract> matchingContracts, String flowName, StubRunnerKafkaRouter listener) {
// listener's container
ConsumerFactory consumerFactory = beanFactory.getBean(ConsumerFactory.class);
for (Contract matchingContract : matchingContracts) {
if (matchingContract.getInput() == null) {
continue;
}
String destination = MapConverter.getStubSideValuesForNonBody(matchingContract.getInput().getMessageFrom()).toString();
ContainerProperties containerProperties = new ContainerProperties(destination);
KafkaMessageListenerContainer container = listenerContainer(consumerFactory, containerProperties, listener);
String containerName = flowName + ".container";
Object initializedContainer = beanFactory.initializeBean(container, containerName);
beanFactory.registerSingleton(containerName, initializedContainer);
if (log.isDebugEnabled()) {
log.debug("Initialized kafka message container with name [" + containerName + "] listening to destination [" + destination + "]");
}
}
}
use of org.springframework.kafka.listener.ContainerProperties in project vividus by vividus-framework.
the class KafkaSteps method startKafkaListener.
/**
* Starts the Kafka consumer with the provided configuration to listen the specified topics. The consumer must be
* stopped when it's not needed.
*
* @param consumerKey The key of the producer configuration
* @param topics The comma-separated set of topics to listen
*/
@When("I start consuming messages from `$consumerKey` Kafka topics `$topics`")
public void startKafkaListener(String consumerKey, Set<String> topics) {
stopListener(getListeners().remove(consumerKey), false);
BlockingQueue<String> messageQueue = new LinkedBlockingDeque<>();
testContext.get(MESSAGES_KEY, HashMap::new).put(consumerKey, messageQueue);
ContainerProperties containerProperties = new ContainerProperties(topics.toArray(new String[0]));
containerProperties.setMessageListener((MessageListener<String, String>) data -> messageQueue.add(data.value()));
GenericMessageListenerContainer<String, String> container = new KafkaMessageListenerContainer<>(consumerFactories.get(consumerKey), containerProperties);
container.start();
getListeners().put(consumerKey, container);
LOGGER.info("Kafka message listener is started");
}
Aggregations