Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error while staring spring boot Kafka project

Error getting when starting spring boot kafka project.

spring boot : 2.1.2.RELEASE Spring kafka version : 2.2.5.RELEASE

Consumer cannot be configured for auto commit for ackMode MANUAL_IMMEDIATE

Consumer config

@Bean
public Map<String, Object> consumerConfigs() {
    Map<String, Object> props = new HashMap<String, Object>();
    props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServer);
    props.put(ConsumerConfig.GROUP_ID_CONFIG, consumerGroup);
    props.put(ConsumerConfig.SESSION_TIMEOUT_MS_CONFIG, 30000);
    props.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
    props.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
    return props;
}

Kafka Listener container factory

@Bean("kafkaListenerContainerFactory")
public ConcurrentKafkaListenerContainerFactory<String, String> kafkaListenerContainerFactory(RetryTemplate retryTemplate) {
    ConcurrentKafkaListenerContainerFactory<String, String> factory = new ConcurrentKafkaListenerContainerFactory<>();
    factory.setConsumerFactory(consumerFactory());
    factory.getContainerProperties().setAckMode(ContainerProperties.AckMode.MANUAL_IMMEDIATE);
    factory.setRetryTemplate(retryTemplate);
    factory.setRecoveryCallback(context -> {
        log.error("Maximum retry policy has been reached {}", context.getAttribute("record"));
        Acknowledgment ack = (Acknowledgment) context.getAttribute(RetryingMessageListenerAdapter.CONTEXT_ACKNOWLEDGMENT);
        ack.acknowledge();
        return null;
    });
    factory.setConcurrency(Integer.parseInt(kafkaConcurrency));
    return factory;
}
like image 462
Bharat Wadhwa Avatar asked Sep 11 '26 08:09

Bharat Wadhwa


1 Answers

For MANUAL_IMMEDIATE ack mode (for any manual mode, essentilly), the ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG consumer property must be turned off.

That's the reason of that exception.

I guess you can just don't use a spring.kafka.consumer.enableAutoCommit in your application.properties.

like image 96
Artem Bilan Avatar answered Sep 13 '26 06:09

Artem Bilan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!