Search in sources :

Example 11 with ContainerProperties

use of org.springframework.kafka.listener.ContainerProperties in project team-catalog by navikt.

the class KafkaConfig method nomRessursContainer.

@Bean
public KafkaMessageListenerContainer<String, String> nomRessursContainer(@Value("${kafka.topics.nom-ressurs}") String topic, NomClient nomClient, MeterRegistry meterRegistry) {
    var containerProps = new ContainerProperties(topic);
    containerProps.setMessageListener(new NomListener(nomClient));
    containerProps.setAckMode(AckMode.MANUAL);
    containerProps.setPollTimeout(500);
    var props = consumerProps(StringDeserializer.class, "nom-cons");
    props.put(ConsumerConfig.MAX_POLL_RECORDS_CONFIG, "100");
    var cf = new DefaultKafkaConsumerFactory<String, String>(props);
    cf.addListener(new MicrometerConsumerListener<>(meterRegistry));
    var container = new KafkaMessageListenerContainer<>(cf, containerProps);
    container.setBatchErrorHandler(new KafkaErrorHandler());
    container.getContainerProperties().setAuthorizationExceptionRetryInterval(Duration.ofMinutes(5));
    return container;
}
Also used : NomListener(no.nav.data.team.resource.NomListener) ContainerProperties(org.springframework.kafka.listener.ContainerProperties) KafkaMessageListenerContainer(org.springframework.kafka.listener.KafkaMessageListenerContainer) DefaultKafkaConsumerFactory(org.springframework.kafka.core.DefaultKafkaConsumerFactory) Bean(org.springframework.context.annotation.Bean)

Example 12 with ContainerProperties

use of org.springframework.kafka.listener.ContainerProperties in project spring-kafka-study by MinChul-Son.

the class MessageListenerContainerConfig method kafkaMessageListenerContainer.

@Bean
public KafkaMessageListenerContainer<String, String> kafkaMessageListenerContainer() {
    ContainerProperties containerProps = new ContainerProperties("test4");
    containerProps.setGroupId("test4-container");
    containerProps.setAckMode(ContainerProperties.AckMode.BATCH);
    containerProps.setMessageListener(new DefaultMessageListener());
    KafkaMessageListenerContainer<String, String> container = new KafkaMessageListenerContainer<>(containerFactory(), containerProps);
    container.setAutoStartup(false);
    return container;
}
Also used : DefaultMessageListener(com.minchul.springkafkastudy.consumer.listener.DefaultMessageListener) ContainerProperties(org.springframework.kafka.listener.ContainerProperties) KafkaMessageListenerContainer(org.springframework.kafka.listener.KafkaMessageListenerContainer) Bean(org.springframework.context.annotation.Bean)

Example 13 with ContainerProperties

use of org.springframework.kafka.listener.ContainerProperties in project skywalking-java by apache.

the class CaseController method setUpConsumer.

private void setUpConsumer() {
    Map<String, Object> configs = new HashMap<>();
    configs.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServers);
    configs.put(ConsumerConfig.GROUP_ID_CONFIG, "grop:" + topicName);
    configs.put(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG, false);
    Deserializer<String> stringDeserializer = new StringDeserializer();
    DefaultKafkaConsumerFactory<String, String> factory = new DefaultKafkaConsumerFactory(configs, stringDeserializer, stringDeserializer);
    ContainerProperties props = new ContainerProperties(topicName);
    props.setMessageListener(new AcknowledgingMessageListener<String, String>() {

        @Override
        public void onMessage(ConsumerRecord<String, String> data, Acknowledgment acknowledgment) {
            if (data.value().equals(helloWorld)) {
                OkHttpClient client = new OkHttpClient.Builder().build();
                Request request = new Request.Builder().url("http://localhost:8080/spring-kafka-2.3.x-scenario/case/spring-kafka-consumer-ping").build();
                Response response = null;
                try {
                    response = client.newCall(request).execute();
                } catch (IOException e) {
                }
                response.body().close();
                acknowledgment.acknowledge();
                latch.countDown();
            }
        }
    });
    KafkaMessageListenerContainer<String, String> container = new KafkaMessageListenerContainer<>(factory, props);
    container.getContainerProperties().setAckMode(ContainerProperties.AckMode.MANUAL_IMMEDIATE);
    container.start();
}
Also used : OkHttpClient(okhttp3.OkHttpClient) HashMap(java.util.HashMap) StringDeserializer(org.apache.kafka.common.serialization.StringDeserializer) Request(okhttp3.Request) KafkaMessageListenerContainer(org.springframework.kafka.listener.KafkaMessageListenerContainer) DefaultKafkaConsumerFactory(org.springframework.kafka.core.DefaultKafkaConsumerFactory) IOException(java.io.IOException) Response(okhttp3.Response) ContainerProperties(org.springframework.kafka.listener.ContainerProperties) Acknowledgment(org.springframework.kafka.support.Acknowledgment)

Example 14 with ContainerProperties

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

the class CoreIndexConsumerConfig method container.

public KafkaMessageListenerContainer<String, String> container() throws Exception {
    setTopics();
    // set more properties
    ContainerProperties properties = new ContainerProperties(this.topics);
    // properties.setPauseEnabled(true);
    // properties.setPauseAfter(0);
    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)

Example 15 with ContainerProperties

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

the class ReindexConsumerConfig method container.

public KafkaMessageListenerContainer<String, String> container() throws Exception {
    setTopics();
    // set more properties
    ContainerProperties properties = new ContainerProperties(this.topics);
    // properties.setPauseEnabled(true);
    // properties.setPauseAfter(0);
    properties.setMessageListener(indexerMessageListener);
    log.info("Reindex KafkaListenerContainer built...");
    return new KafkaMessageListenerContainer<>(consumerFactory(), properties);
}
Also used : ContainerProperties(org.springframework.kafka.listener.ContainerProperties) KafkaMessageListenerContainer(org.springframework.kafka.listener.KafkaMessageListenerContainer)

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