use of org.springframework.kafka.listener.MessageListener 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.MessageListener 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");
}
use of org.springframework.kafka.listener.MessageListener in project chutney by chutney-testing.
the class KafkaBasicConsumeTaskTest method overrideTaskMessageListenerContainer.
private MessageListener<String, String> overrideTaskMessageListenerContainer(Task task) {
ConsumerFactory<String, String> cf = mock(ConsumerFactory.class, RETURNS_DEEP_STUBS);
Consumer<String, String> consumer = mock(Consumer.class);
given(cf.createConsumer(any(), any(), any(), any())).willReturn(consumer);
when(cf.getConfigurationProperties().get(eq(ConsumerConfig.GROUP_ID_CONFIG))).thenReturn(GROUP);
KafkaConsumerFactoryFactory kafkaConsumerFactoryFactory = mock(KafkaConsumerFactoryFactory.class);
when(kafkaConsumerFactoryFactory.create(any(), any(), any())).thenReturn(cf);
ReflectionTestUtils.setField(task, "kafkaConsumerFactoryFactory", kafkaConsumerFactoryFactory);
ContainerProperties containerProperties = new ContainerProperties(TOPIC);
containerProperties.setGroupId(GROUP);
containerProperties.setMessageListener(ReflectionTestUtils.invokeMethod(task, "createMessageListener"));
ConcurrentMessageListenerContainer<String, String> messageListenerContainer = new ConcurrentMessageListenerContainer<>(cf, containerProperties);
return (MessageListener<String, String>) messageListenerContainer.getContainerProperties().getMessageListener();
}
use of org.springframework.kafka.listener.MessageListener in project kafka-fundamentals by katyagorshkova.
the class KafkaProducerServiceTest method setUp.
@BeforeEach
public void setUp() {
consumerRecords = new LinkedBlockingQueue<>();
ContainerProperties containerProperties = new ContainerProperties("topic1");
Map<String, Object> consumerProperties = KafkaTestUtils.consumerProps("group1", "false", embeddedKafka);
DefaultKafkaConsumerFactory<String, String> consumer = new DefaultKafkaConsumerFactory<>(consumerProperties);
listener = new KafkaMessageListenerContainer<>(consumer, containerProperties);
listener.setupMessageListener((MessageListener<String, String>) record -> {
log.debug("Listened message='{}'", record);
consumerRecords.add(record);
});
listener.start();
ContainerTestUtils.waitForAssignment(listener, embeddedKafka.getPartitionsPerTopic());
}
use of org.springframework.kafka.listener.MessageListener in project spring-cloud-sleuth by spring-cloud.
the class MessageListenerMethodInterceptor method wrapListenerContainerCreation.
@Around("anyCreateListenerContainer()")
public Object wrapListenerContainerCreation(ProceedingJoinPoint pjp) throws Throwable {
MessageListenerContainer listener = (MessageListenerContainer) pjp.proceed();
if (listener instanceof AbstractMessageListenerContainer) {
AbstractMessageListenerContainer container = (AbstractMessageListenerContainer) listener;
Object someMessageListener = container.getContainerProperties().getMessageListener();
if (someMessageListener == null) {
if (log.isDebugEnabled()) {
log.debug("No message listener to wrap. Proceeding");
}
} else if (someMessageListener instanceof MessageListener) {
container.setupMessageListener(createProxy(someMessageListener));
} else {
if (log.isDebugEnabled()) {
log.debug("ATM we don't support Batch message listeners");
}
}
} else {
if (log.isDebugEnabled()) {
log.debug("Can't wrap this listener. Proceeding");
}
}
return listener;
}
Aggregations