Search in sources :

Example 16 with ContainerProperties

use of org.springframework.kafka.listener.ContainerProperties in project core-services by digit-egov.

the class PersisterConsumerConfig method container.

@Bean
public KafkaMessageListenerContainer<String, String> container() throws Exception {
    ContainerProperties properties = new ContainerProperties(this.topics.toArray(new String[topics.size()]));
    // set more properties
    // properties.setPauseEnabled(true);
    // properties.setPauseAfter(0);
    // properties.setGenericErrorHandler(kafkaConsumerErrorHandler);
    properties.setMessageListener(indexerMessageListener);
    log.info("Custom KafkaListenerContainer built...");
    return new KafkaMessageListenerContainer<>(consumerFactory(), properties);
}
Also used : ContainerProperties(org.springframework.kafka.listener.ContainerProperties) KafkaMessageListenerContainer(org.springframework.kafka.listener.KafkaMessageListenerContainer) Bean(org.springframework.context.annotation.Bean)

Example 17 with ContainerProperties

use of org.springframework.kafka.listener.ContainerProperties in project chutney by chutney-testing.

the class KafkaBasicConsumeTask method createMessageListenerContainer.

private ConcurrentMessageListenerContainer<String, String> createMessageListenerContainer(MessageListener<String, String> messageListener) {
    ContainerProperties containerProperties = new ContainerProperties(topic);
    containerProperties.setMessageListener(messageListener);
    containerProperties.setAckMode(ContainerProperties.AckMode.valueOf(this.ackMode));
    ofNullable(properties.get(ConsumerConfig.AUTO_COMMIT_INTERVAL_MS_CONFIG)).ifPresent(acims -> containerProperties.setAckTime(Long.parseLong(acims)));
    ofNullable(properties.get(AUTO_COMMIT_COUNT_CONFIG)).ifPresent(acc -> containerProperties.setAckCount(Integer.parseInt(acc)));
    return new ConcurrentMessageListenerContainer<>(kafkaConsumerFactoryFactory.create(target, group, properties), containerProperties);
}
Also used : ContainerProperties(org.springframework.kafka.listener.ContainerProperties) ConcurrentMessageListenerContainer(org.springframework.kafka.listener.ConcurrentMessageListenerContainer)

Example 18 with ContainerProperties

use of org.springframework.kafka.listener.ContainerProperties 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 19 with ContainerProperties

use of org.springframework.kafka.listener.ContainerProperties in project spring-boot by spring-projects.

the class KafkaAutoConfigurationTests method listenerProperties.

@SuppressWarnings("unchecked")
@Test
void listenerProperties() {
    this.contextRunner.withPropertyValues("spring.kafka.template.default-topic=testTopic", "spring.kafka.template.transaction-id-prefix=txOverride", "spring.kafka.listener.ack-mode=MANUAL", "spring.kafka.listener.client-id=client", "spring.kafka.listener.ack-count=123", "spring.kafka.listener.ack-time=456", "spring.kafka.listener.concurrency=3", "spring.kafka.listener.poll-timeout=2000", "spring.kafka.listener.no-poll-threshold=2.5", "spring.kafka.listener.type=batch", "spring.kafka.listener.idle-between-polls=1s", "spring.kafka.listener.idle-event-interval=1s", "spring.kafka.listener.idle-partition-event-interval=1s", "spring.kafka.listener.monitor-interval=45", "spring.kafka.listener.log-container-config=true", "spring.kafka.listener.missing-topics-fatal=true", "spring.kafka.jaas.enabled=true", "spring.kafka.listener.immediate-stop=true", "spring.kafka.producer.transaction-id-prefix=foo", "spring.kafka.jaas.login-module=foo", "spring.kafka.jaas.control-flag=REQUISITE", "spring.kafka.jaas.options.useKeyTab=true", "spring.kafka.listener.async-acks=true").run((context) -> {
        DefaultKafkaProducerFactory<?, ?> producerFactory = context.getBean(DefaultKafkaProducerFactory.class);
        DefaultKafkaConsumerFactory<?, ?> consumerFactory = context.getBean(DefaultKafkaConsumerFactory.class);
        KafkaTemplate<?, ?> kafkaTemplate = context.getBean(KafkaTemplate.class);
        AbstractKafkaListenerContainerFactory<?, ?, ?> kafkaListenerContainerFactory = (AbstractKafkaListenerContainerFactory<?, ?, ?>) context.getBean(KafkaListenerContainerFactory.class);
        assertThat(kafkaTemplate.getMessageConverter()).isInstanceOf(MessagingMessageConverter.class);
        assertThat(kafkaTemplate).hasFieldOrPropertyWithValue("producerFactory", producerFactory);
        assertThat(kafkaTemplate.getDefaultTopic()).isEqualTo("testTopic");
        assertThat(kafkaTemplate).hasFieldOrPropertyWithValue("transactionIdPrefix", "txOverride");
        assertThat(kafkaListenerContainerFactory.getConsumerFactory()).isEqualTo(consumerFactory);
        ContainerProperties containerProperties = kafkaListenerContainerFactory.getContainerProperties();
        assertThat(containerProperties.getAckMode()).isEqualTo(AckMode.MANUAL);
        assertThat(containerProperties.isAsyncAcks()).isEqualTo(true);
        assertThat(containerProperties.getClientId()).isEqualTo("client");
        assertThat(containerProperties.getAckCount()).isEqualTo(123);
        assertThat(containerProperties.getAckTime()).isEqualTo(456L);
        assertThat(containerProperties.getPollTimeout()).isEqualTo(2000L);
        assertThat(containerProperties.getNoPollThreshold()).isEqualTo(2.5f);
        assertThat(containerProperties.getIdleBetweenPolls()).isEqualTo(1000L);
        assertThat(containerProperties.getIdleEventInterval()).isEqualTo(1000L);
        assertThat(containerProperties.getIdlePartitionEventInterval()).isEqualTo(1000L);
        assertThat(containerProperties.getMonitorInterval()).isEqualTo(45);
        assertThat(containerProperties.isLogContainerConfig()).isTrue();
        assertThat(containerProperties.isMissingTopicsFatal()).isTrue();
        assertThat(containerProperties.isStopImmediate()).isTrue();
        assertThat(kafkaListenerContainerFactory).extracting("concurrency").isEqualTo(3);
        assertThat(kafkaListenerContainerFactory.isBatchListener()).isTrue();
        assertThat(context.getBeansOfType(KafkaJaasLoginModuleInitializer.class)).hasSize(1);
        KafkaJaasLoginModuleInitializer jaas = context.getBean(KafkaJaasLoginModuleInitializer.class);
        assertThat(jaas).hasFieldOrPropertyWithValue("loginModule", "foo");
        assertThat(jaas).hasFieldOrPropertyWithValue("controlFlag", AppConfigurationEntry.LoginModuleControlFlag.REQUISITE);
        assertThat(context.getBeansOfType(KafkaTransactionManager.class)).hasSize(1);
        assertThat(((Map<String, String>) ReflectionTestUtils.getField(jaas, "options"))).containsExactly(entry("useKeyTab", "true"));
    });
}
Also used : KafkaJaasLoginModuleInitializer(org.springframework.kafka.security.jaas.KafkaJaasLoginModuleInitializer) ContainerProperties(org.springframework.kafka.listener.ContainerProperties) ConcurrentKafkaListenerContainerFactory(org.springframework.kafka.config.ConcurrentKafkaListenerContainerFactory) AbstractKafkaListenerContainerFactory(org.springframework.kafka.config.AbstractKafkaListenerContainerFactory) KafkaListenerContainerFactory(org.springframework.kafka.config.KafkaListenerContainerFactory) AbstractKafkaListenerContainerFactory(org.springframework.kafka.config.AbstractKafkaListenerContainerFactory) Test(org.junit.jupiter.api.Test)

Example 20 with ContainerProperties

use of org.springframework.kafka.listener.ContainerProperties in project spring-boot by spring-projects.

the class KafkaPropertiesTests method listenerDefaultValuesAreConsistent.

@Test
void listenerDefaultValuesAreConsistent() {
    ContainerProperties container = new ContainerProperties("test");
    Listener listenerProperties = new KafkaProperties().getListener();
    assertThat(listenerProperties.isMissingTopicsFatal()).isEqualTo(container.isMissingTopicsFatal());
}
Also used : Listener(org.springframework.boot.autoconfigure.kafka.KafkaProperties.Listener) ContainerProperties(org.springframework.kafka.listener.ContainerProperties) Test(org.junit.jupiter.api.Test)

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