Search in sources :

Example 1 with ContainerProperties

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");
}
Also used : ContainerProperties(org.springframework.kafka.listener.ContainerProperties) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 2 with ContainerProperties

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();
}
Also used : StringDeserializer(org.apache.kafka.common.serialization.StringDeserializer) MessageListener(org.springframework.kafka.listener.MessageListener) KafkaMessageListenerContainer(org.springframework.kafka.listener.KafkaMessageListenerContainer) DefaultKafkaConsumerFactory(org.springframework.kafka.core.DefaultKafkaConsumerFactory) ContainerProperties(org.springframework.kafka.listener.ContainerProperties) Test(org.junit.jupiter.api.Test)

Example 3 with ContainerProperties

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;
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ContainerProperties(org.springframework.kafka.listener.ContainerProperties) Collection(java.util.Collection) KafkaMessageListenerContainer(org.springframework.kafka.listener.KafkaMessageListenerContainer) DefaultKafkaConsumerFactory(org.springframework.kafka.core.DefaultKafkaConsumerFactory)

Example 4 with ContainerProperties

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();
}
Also used : SettableListenableFuture(org.springframework.util.concurrent.SettableListenableFuture) DefaultKafkaProducerFactory(org.springframework.kafka.core.DefaultKafkaProducerFactory) ProducerFactory(org.springframework.kafka.core.ProducerFactory) RecordMetadata(org.apache.kafka.clients.producer.RecordMetadata) Callback(org.apache.kafka.clients.producer.Callback) Producer(org.apache.kafka.clients.producer.Producer) TopicPartition(org.apache.kafka.common.TopicPartition) ContainerProperties(org.springframework.kafka.listener.ContainerProperties) ProducerRecord(org.apache.kafka.clients.producer.ProducerRecord) GenericMessageListenerContainer(org.springframework.kafka.listener.GenericMessageListenerContainer) Test(org.junit.jupiter.api.Test)

Example 5 with ContainerProperties

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;
}
Also used : KafkaMessageListenerContainer(org.springframework.kafka.listener.KafkaMessageListenerContainer) DefaultKafkaConsumerFactory(org.springframework.kafka.core.DefaultKafkaConsumerFactory) CountDownLatch(java.util.concurrent.CountDownLatch) ConsumerRebalanceListener(org.apache.kafka.clients.consumer.ConsumerRebalanceListener) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TopicPartition(org.apache.kafka.common.TopicPartition) ContainerProperties(org.springframework.kafka.listener.ContainerProperties)

Aggregations

ContainerProperties (org.springframework.kafka.listener.ContainerProperties)73 KafkaMessageListenerContainer (org.springframework.kafka.listener.KafkaMessageListenerContainer)46 DefaultKafkaConsumerFactory (org.springframework.kafka.core.DefaultKafkaConsumerFactory)41 Test (org.junit.jupiter.api.Test)38 DefaultKafkaProducerFactory (org.springframework.kafka.core.DefaultKafkaProducerFactory)30 KafkaTemplate (org.springframework.kafka.core.KafkaTemplate)26 QueueChannel (org.springframework.integration.channel.QueueChannel)21 ErrorMessage (org.springframework.messaging.support.ErrorMessage)19 Message (org.springframework.messaging.Message)18 MessageHeaders (org.springframework.messaging.MessageHeaders)18 CountDownLatch (java.util.concurrent.CountDownLatch)17 DirectChannel (org.springframework.integration.channel.DirectChannel)17 MessageChannel (org.springframework.messaging.MessageChannel)14 Acknowledgment (org.springframework.kafka.support.Acknowledgment)11 Type (java.lang.reflect.Type)10 HashMap (java.util.HashMap)9 StringDeserializer (org.apache.kafka.common.serialization.StringDeserializer)9 BeanFactory (org.springframework.beans.factory.BeanFactory)9 Bean (org.springframework.context.annotation.Bean)9 ConcurrentMessageListenerContainer (org.springframework.kafka.listener.ConcurrentMessageListenerContainer)9