Cannot disable manual commits on Kafka message using Spring-integration-kafka in Spring-Boot App

3.3k Views Asked by At

As I am new to kafka, my group is using Spring-Integration-kafka:2.0.0.RELEASE on a Spring-boot application. I was able to consume kafka message with my KafkaConsumer based on this example here.

For this spring-boot application, I have my Application.java

package hello;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import java.util.concurrent.TimeUnit;
import hello.notification.Listener;

@SpringBootApplication
public class Application {

    private Listener listener;

    public void run(String... args) throws Exception {
        this.listener.countDownLatch1.await(60, TimeUnit.SECONDS);
    }

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

This is my KafkaConsumerConfig.java

package hello.notification;

import ....; //different imports

@Configuration
@EnableKafka
public class KafkaConsumerConfig {
    @Bean
    KafkaListenerContainerFactory<ConcurrentMessageListenerContainer<String, String>> kafkaListenerContainerFactory() {
        ConcurrentKafkaListenerContainerFactory<String, String> factory = new ConcurrentKafkaListenerContainerFactory<>();
        factory.setConsumerFactory(consumerFactory());
        factory.setConcurrency(1);
        factory.getContainerProperties().setPollTimeout(3000);
        return factory;
    }

    @Bean
    public ConsumerFactory<String, String> consumerFactory() {
        Map<String, Object> propsMap = new HashMap<>();
        propsMap.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092");
        propsMap.put(ConsumerConfig.GROUP_ID_CONFIG, "group1");
        propsMap.put(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG, false);
        //propsMap.put(ConsumerConfig.AUTO_COMMIT_INTERVAL_MS_CONFIG, 100);
        propsMap.put(ConsumerConfig.SESSION_TIMEOUT_MS_CONFIG, 6000);
        propsMap.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
        propsMap.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
        return new DefaultKafkaConsumerFactory<>(propsMap);
    }

    @Bean
    public Listener listener() {
        return new Listener();
    }
}

and finally my Listener.java

package hello.notification;

import ...; // different imports

public class Listener {

    public final CountDownLatch countDownLatch1 = new CountDownLatch(1);

    @KafkaListener(topics = "topic1")
    public void listen(ConsumerRecord<?, ?> record, Acknowledgment ack) {
        System.out.println(record + " and " + ack);
                // Place holder for "ack.acknowledge();"
        countDownLatch1.countDown();
    } 
}

my questions are:

1) In ConsumerConfig settings, I've already set "ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG" to false and commented out "ConsumerConfig.AUTO_COMMIT_INTERVAL_MS_CONFIG", why does it still commits automatically?(as I no longer would see the message if I restart my spring-boot app- I would expect it to try to resend until it's been committed)

2) In order to commit manually, I think I just need to add ack.acknowledge() inside my Listener.listen(please see place-holder)? Is there anything else that I would need other than this to ensure this is a manual commit?

3) I would like to have the simplest/cleanest kafkaconsumer, I wonder if you think what I have is the simplest way, originally I was looking for singlethreaded consumer but there are not a lot of examples out there so I stick with the concurrent listener.

Thanks for all of your help and inputs!

1

There are 1 best solutions below

7
On

why does it still commits automatically?

Just because by default KafkaMessageListenerContainer is supplied with:

private AbstractMessageListenerContainer.AckMode ackMode = AckMode.BATCH;

Is there anything else that I would need other than this to ensure this is a manual commit?

You have to switch to the

ContainerProperties.setAckMode(AbstractMessageListenerContainer.AckMode.MANUAL)

I wonder if you think what I have is the simplest way

That's correct. Just only need to get used to.

See Reference Manual for more info