Search in sources :

Example 1 with ContainerProperties

use of org.springframework.kafka.listener.config.ContainerProperties in project loc-framework by lord-of-code.

the class LocKafkaAutoConfiguration method kafkaListenerContainerFactory.

@Bean(name = "kafkaListenerContainerFactory")
public ConcurrentKafkaListenerContainerFactory<?, ?> kafkaListenerContainerFactory(ConcurrentKafkaListenerContainerFactoryConfigurer configurer, ConsumerFactory<Object, Object> kafkaConsumerFactory) {
    ConcurrentKafkaListenerContainerFactory<Object, Object> factory = new ConcurrentKafkaListenerContainerFactory<>();
    ContainerProperties containerProperties = factory.getContainerProperties();
    containerProperties.setErrorHandler(new LocKafkaConsumerErrorHandler());
    factory.setRecordFilterStrategy(locMessageFilterStrategy());
    factory.setMessageConverter(recordMessageConverter());
    configurer.configure(factory, kafkaConsumerFactory);
    return factory;
}
Also used : ContainerProperties(org.springframework.kafka.listener.config.ContainerProperties) ConcurrentKafkaListenerContainerFactory(org.springframework.kafka.config.ConcurrentKafkaListenerContainerFactory) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) Bean(org.springframework.context.annotation.Bean)

Example 2 with ContainerProperties

use of org.springframework.kafka.listener.config.ContainerProperties in project loc-framework by lord-of-code.

the class LocKafkaTest method testConsumer.

@Test
@SuppressWarnings("unchecked")
public void testConsumer() throws Exception {
    assertThat(concurrentKafkaListenerContainerFactory).isNotNull();
    ContainerProperties containerProperties = concurrentKafkaListenerContainerFactory.getContainerProperties();
    assertThat(containerProperties).isNotNull();
    assertThat(containerProperties.getGenericErrorHandler()).isNotNull();
    assertThat(containerProperties.getAckMode()).isEqualTo(AckMode.RECORD);
    ConsumerFactory consumerFactory = concurrentKafkaListenerContainerFactory.getConsumerFactory();
    assertThat(consumerFactory).isNotNull();
    assertThat(consumerFactory.getConfigurationProperties()).isNotNull();
    assertThat(consumerFactory.getConfigurationProperties()).isNotNull();
    assertThat(consumerFactory.getConfigurationProperties().get("group.id")).isEqualTo("loc-kafka-unittest");
    assertThat(consumerFactory.getConfigurationProperties().get("bootstrap.servers")).isEqualTo(Lists.newArrayList("127.0.0.1:9092"));
    assertThat(consumerFactory.getConfigurationProperties().get("enable.auto.commit")).isEqualTo(false);
    assertThat(consumerFactory.getConfigurationProperties().get("key.deserializer")).isEqualTo(org.apache.kafka.common.serialization.ByteArrayDeserializer.class);
    assertThat(consumerFactory.getConfigurationProperties().get("value.deserializer")).isEqualTo(org.apache.kafka.common.serialization.ByteArrayDeserializer.class);
}
Also used : ConsumerFactory(org.springframework.kafka.core.ConsumerFactory) ContainerProperties(org.springframework.kafka.listener.config.ContainerProperties) Test(org.junit.Test) WebMvcTest(org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest)

Example 3 with ContainerProperties

use of org.springframework.kafka.listener.config.ContainerProperties in project xm-ms-entity by xm-online.

the class ApplicationStartup method createSystemConsumer.

private void createSystemConsumer(String name, MessageListener<String, String> consumeEvent) {
    log.info("Creating kafka consumer for topic {}", name);
    ContainerProperties containerProps = new ContainerProperties(name);
    Map<String, Object> props = kafkaProperties.buildConsumerProperties();
    if (name.equals(applicationProperties.getKafkaSystemTopic())) {
        props.put(ConsumerConfig.GROUP_ID_CONFIG, UUID.randomUUID().toString());
    }
    ConsumerFactory<String, String> factory = new DefaultKafkaConsumerFactory<>(props);
    ConcurrentMessageListenerContainer<String, String> container = new ConcurrentMessageListenerContainer<>(factory, containerProps);
    container.setupMessageListener(consumeEvent);
    container.start();
    log.info("Successfully created kafka consumer for topic {}", name);
}
Also used : ContainerProperties(org.springframework.kafka.listener.config.ContainerProperties) ConcurrentMessageListenerContainer(org.springframework.kafka.listener.ConcurrentMessageListenerContainer) DefaultKafkaConsumerFactory(org.springframework.kafka.core.DefaultKafkaConsumerFactory)

Example 4 with ContainerProperties

use of org.springframework.kafka.listener.config.ContainerProperties in project eventapis by kloiasoft.

the class EventListenConfiguration method startOperations.

private void startOperations() {
    Map<String, Object> consumerProperties = eventApisConfiguration.getEventBus().buildConsumerProperties();
    DefaultKafkaConsumerFactory<String, Operation> operationConsumerFactory = new DefaultKafkaConsumerFactory<>(consumerProperties, new StringDeserializer(), new JsonDeserializer<>(Operation.class));
    ContainerProperties operationContainerProperties = new ContainerProperties(Operation.OPERATION_EVENTS);
    operationContainerProperties.setMessageListener(new MultipleEventMessageListener(eventMessageListeners));
    operationListenerContainer = new ConcurrentMessageListenerContainer<>(operationConsumerFactory, operationContainerProperties);
    operationListenerContainer.setBeanName("emon-operations");
    operationListenerContainer.start();
}
Also used : StringDeserializer(org.apache.kafka.common.serialization.StringDeserializer) MultipleEventMessageListener(com.kloia.eventapis.api.emon.service.MultipleEventMessageListener) ContainerProperties(org.springframework.kafka.listener.config.ContainerProperties) Operation(com.kloia.eventapis.pojos.Operation) DefaultKafkaConsumerFactory(org.springframework.kafka.core.DefaultKafkaConsumerFactory)

Example 5 with ContainerProperties

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

the class ConcurrentKafkaListenerContainerFactoryConfigurer method configure.

/**
	 * Configure the specified Kafka listener container factory. The factory can be
	 * further tuned and default settings can be overridden.
	 * @param listenerContainerFactory the {@link ConcurrentKafkaListenerContainerFactory}
	 * instance to configure
	 * @param consumerFactory the {@link ConsumerFactory} to use
	 */
public void configure(ConcurrentKafkaListenerContainerFactory<Object, Object> listenerContainerFactory, ConsumerFactory<Object, Object> consumerFactory) {
    listenerContainerFactory.setConsumerFactory(consumerFactory);
    Listener container = this.properties.getListener();
    ContainerProperties containerProperties = listenerContainerFactory.getContainerProperties();
    if (container.getAckMode() != null) {
        containerProperties.setAckMode(container.getAckMode());
    }
    if (container.getAckCount() != null) {
        containerProperties.setAckCount(container.getAckCount());
    }
    if (container.getAckTime() != null) {
        containerProperties.setAckTime(container.getAckTime());
    }
    if (container.getPollTimeout() != null) {
        containerProperties.setPollTimeout(container.getPollTimeout());
    }
    if (container.getConcurrency() != null) {
        listenerContainerFactory.setConcurrency(container.getConcurrency());
    }
}
Also used : Listener(org.springframework.boot.autoconfigure.kafka.KafkaProperties.Listener) ContainerProperties(org.springframework.kafka.listener.config.ContainerProperties)

Aggregations

ContainerProperties (org.springframework.kafka.listener.config.ContainerProperties)6 DefaultKafkaConsumerFactory (org.springframework.kafka.core.DefaultKafkaConsumerFactory)3 MultipleEventMessageListener (com.kloia.eventapis.api.emon.service.MultipleEventMessageListener)2 StringDeserializer (org.apache.kafka.common.serialization.StringDeserializer)2 PublishedEventWrapper (com.kloia.eventapis.kafka.PublishedEventWrapper)1 Operation (com.kloia.eventapis.pojos.Operation)1 Test (org.junit.Test)1 ConditionalOnMissingBean (org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean)1 Listener (org.springframework.boot.autoconfigure.kafka.KafkaProperties.Listener)1 WebMvcTest (org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest)1 Bean (org.springframework.context.annotation.Bean)1 ConcurrentKafkaListenerContainerFactory (org.springframework.kafka.config.ConcurrentKafkaListenerContainerFactory)1 ConsumerFactory (org.springframework.kafka.core.ConsumerFactory)1 ConcurrentMessageListenerContainer (org.springframework.kafka.listener.ConcurrentMessageListenerContainer)1