Search in sources :

Example 1 with MessageListener

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();
    }
}
Also used : DirtiesContext(org.springframework.test.annotation.DirtiesContext) AopUtils(org.springframework.aop.support.AopUtils) ListenableFuture(org.springframework.util.concurrent.ListenableFuture) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Autowired(org.springframework.beans.factory.annotation.Autowired) HashMap(java.util.HashMap) EmbeddedKafkaBroker(org.springframework.kafka.test.EmbeddedKafkaBroker) AtomicReference(java.util.concurrent.atomic.AtomicReference) ArrayList(java.util.ArrayList) SpringJUnitConfig(org.springframework.test.context.junit.jupiter.SpringJUnitConfig) ContainerProperties(org.springframework.kafka.listener.ContainerProperties) StringDeserializer(org.apache.kafka.common.serialization.StringDeserializer) BDDMockito.given(org.mockito.BDDMockito.given) KafkaTransactionManager(org.springframework.kafka.transaction.KafkaTransactionManager) Map(java.util.Map) Metric(org.apache.kafka.common.Metric) MetricName(org.apache.kafka.common.MetricName) Deserializer(org.apache.kafka.common.serialization.Deserializer) Consumer(org.apache.kafka.clients.consumer.Consumer) KafkaTestUtils(org.springframework.kafka.test.utils.KafkaTestUtils) Properties(java.util.Properties) EmbeddedKafka(org.springframework.kafka.test.context.EmbeddedKafka) Listener(org.springframework.kafka.core.ConsumerFactory.Listener) ConsumerConfig(org.apache.kafka.clients.consumer.ConsumerConfig) Collectors(java.util.stream.Collectors) MessageListener(org.springframework.kafka.listener.MessageListener) Mockito.verify(org.mockito.Mockito.verify) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) Configuration(org.springframework.context.annotation.Configuration) CountDownLatch(java.util.concurrent.CountDownLatch) AbstractMap(java.util.AbstractMap) List(java.util.List) Stream(java.util.stream.Stream) SendResult(org.springframework.kafka.support.SendResult) ProxyFactory(org.springframework.aop.framework.ProxyFactory) Collections(java.util.Collections) KafkaMessageListenerContainer(org.springframework.kafka.listener.KafkaMessageListenerContainer) Mockito.mock(org.mockito.Mockito.mock) KafkaConsumer(org.apache.kafka.clients.consumer.KafkaConsumer) KafkaTransactionManager(org.springframework.kafka.transaction.KafkaTransactionManager) KafkaMessageListenerContainer(org.springframework.kafka.listener.KafkaMessageListenerContainer) CountDownLatch(java.util.concurrent.CountDownLatch) ContainerProperties(org.springframework.kafka.listener.ContainerProperties) SendResult(org.springframework.kafka.support.SendResult) Test(org.junit.jupiter.api.Test)

Example 2 with MessageListener

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");
}
Also used : GenericMessageListenerContainer(org.springframework.kafka.listener.GenericMessageListenerContainer) VariableContext(org.vividus.context.VariableContext) Collectors.groupingBy(java.util.stream.Collectors.groupingBy) LoggerFactory(org.slf4j.LoggerFactory) TimeoutException(java.util.concurrent.TimeoutException) HashMap(java.util.HashMap) Collectors.collectingAndThen(java.util.stream.Collectors.collectingAndThen) Function(java.util.function.Function) ArrayList(java.util.ArrayList) When(org.jbehave.core.annotations.When) DefaultKafkaProducerFactory(org.springframework.kafka.core.DefaultKafkaProducerFactory) ContainerProperties(org.springframework.kafka.listener.ContainerProperties) StringDeserializer(org.apache.kafka.common.serialization.StringDeserializer) KafkaTemplate(org.springframework.kafka.core.KafkaTemplate) DurationBasedWaiter(org.vividus.util.wait.DurationBasedWaiter) VariableScope(org.vividus.variable.VariableScope) Collectors.mapping(java.util.stream.Collectors.mapping) Duration(java.time.Duration) Map(java.util.Map) ComparisonRule(org.vividus.steps.ComparisonRule) StringSerializer(org.apache.kafka.common.serialization.StringSerializer) Map.entry(java.util.Map.entry) StringUtils.substringAfter(org.apache.commons.lang3.StringUtils.substringAfter) ProducerConfig(org.apache.kafka.clients.producer.ProducerConfig) IPropertyParser(org.vividus.util.property.IPropertyParser) Logger(org.slf4j.Logger) DefaultKafkaConsumerFactory(org.springframework.kafka.core.DefaultKafkaConsumerFactory) AfterStory(org.jbehave.core.annotations.AfterStory) Set(java.util.Set) BlockingQueue(java.util.concurrent.BlockingQueue) ConsumerConfig(org.apache.kafka.clients.consumer.ConsumerConfig) Collectors(java.util.stream.Collectors) MessageListener(org.springframework.kafka.listener.MessageListener) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) ConsumerRecord(org.apache.kafka.clients.consumer.ConsumerRecord) Matcher(org.hamcrest.Matcher) LinkedBlockingDeque(java.util.concurrent.LinkedBlockingDeque) SoftAssert(org.vividus.softassert.SoftAssert) StringUtils.substringBefore(org.apache.commons.lang3.StringUtils.substringBefore) TestContext(org.vividus.testcontext.TestContext) KafkaMessageListenerContainer(org.springframework.kafka.listener.KafkaMessageListenerContainer) LinkedBlockingDeque(java.util.concurrent.LinkedBlockingDeque) ContainerProperties(org.springframework.kafka.listener.ContainerProperties) KafkaMessageListenerContainer(org.springframework.kafka.listener.KafkaMessageListenerContainer) When(org.jbehave.core.annotations.When)

Example 3 with MessageListener

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();
}
Also used : ContainerProperties(org.springframework.kafka.listener.ContainerProperties) ConcurrentMessageListenerContainer(org.springframework.kafka.listener.ConcurrentMessageListenerContainer) MessageListener(org.springframework.kafka.listener.MessageListener)

Example 4 with MessageListener

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());
}
Also used : DirtiesContext(org.springframework.test.annotation.DirtiesContext) BeforeEach(org.junit.jupiter.api.BeforeEach) KafkaTestUtils(org.springframework.kafka.test.utils.KafkaTestUtils) Logger(org.slf4j.Logger) EmbeddedKafka(org.springframework.kafka.test.context.EmbeddedKafka) DefaultKafkaConsumerFactory(org.springframework.kafka.core.DefaultKafkaConsumerFactory) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) BlockingQueue(java.util.concurrent.BlockingQueue) EmbeddedKafkaBroker(org.springframework.kafka.test.EmbeddedKafkaBroker) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) MessageListener(org.springframework.kafka.listener.MessageListener) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) AfterEach(org.junit.jupiter.api.AfterEach) ContainerProperties(org.springframework.kafka.listener.ContainerProperties) SpringBootTest(org.springframework.boot.test.context.SpringBootTest) ConsumerRecord(org.apache.kafka.clients.consumer.ConsumerRecord) Map(java.util.Map) Assertions(org.assertj.core.api.Assertions) ContainerTestUtils(org.springframework.kafka.test.utils.ContainerTestUtils) KafkaMessageListenerContainer(org.springframework.kafka.listener.KafkaMessageListenerContainer) KafkaConditions(org.springframework.kafka.test.assertj.KafkaConditions) ContainerProperties(org.springframework.kafka.listener.ContainerProperties) DefaultKafkaConsumerFactory(org.springframework.kafka.core.DefaultKafkaConsumerFactory) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 5 with MessageListener

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;
}
Also used : MessageListener(org.springframework.kafka.listener.MessageListener) AbstractMessageListenerContainer(org.springframework.kafka.listener.AbstractMessageListenerContainer) MessageListenerContainer(org.springframework.kafka.listener.MessageListenerContainer) AbstractMessageListenerContainer(org.springframework.kafka.listener.AbstractMessageListenerContainer) Around(org.aspectj.lang.annotation.Around)

Aggregations

MessageListener (org.springframework.kafka.listener.MessageListener)7 ContainerProperties (org.springframework.kafka.listener.ContainerProperties)6 Map (java.util.Map)5 TimeUnit (java.util.concurrent.TimeUnit)5 KafkaMessageListenerContainer (org.springframework.kafka.listener.KafkaMessageListenerContainer)5 HashMap (java.util.HashMap)4 ConsumerConfig (org.apache.kafka.clients.consumer.ConsumerConfig)4 StringDeserializer (org.apache.kafka.common.serialization.StringDeserializer)4 Test (org.junit.jupiter.api.Test)4 EmbeddedKafka (org.springframework.kafka.test.context.EmbeddedKafka)4 ArrayList (java.util.ArrayList)3 List (java.util.List)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 Collectors (java.util.stream.Collectors)3 AbstractMap (java.util.AbstractMap)2 Collections (java.util.Collections)2 Properties (java.util.Properties)2 BlockingQueue (java.util.concurrent.BlockingQueue)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 Stream (java.util.stream.Stream)2