Search code examples
springspring-bootapache-kafkaspring-kafkakafka-transactions-api

What is the non deprecated way of setting the transaction manager into the listener container is spring kafka 3.2?


I am using sprking kafka and noticed in version 3.2 the setTransactionManager has been deprecated in ContainerProperties:

@Bean
public ConcurrentKafkaListenerContainerFactory<Long, PersonDto> inContainerFactory() {
    ConcurrentKafkaListenerContainerFactory<Long, PersonDto> factory = new ConcurrentKafkaListenerContainerFactory<>();
    factory.setConsumerFactory(inConsumerFactory());
    factory.getContainerProperties().setTransactionManager(kafkaTransactionManager);
    return factory;
}

The problem is it doesn't say what to use instead in order to wire the transaction manager into the listener container ( I cannot use spring boot's configuration only in .yml since I need to configure multiple listener container, some transactional and some not ).

How can I set the transaction manager in spring kafka 3.2 the right way?


Solution

  • That method is deprecated in favor of:

    /**
     * Set the transaction manager to start a transaction; replace {@link #setTransactionManager}.
     * @param kafkaAwareTransactionManager the transaction manager.
     * @since 3.2
     */
    public void setKafkaAwareTransactionManager(@Nullable KafkaAwareTransactionManager<?, ?> kafkaAwareTransactionManager) {
    

    The change has been done here: https://github.com/spring-projects/spring-kafka/pull/3066

    And it is mentioned in the What's New chapter of that Spring for Apache Kafka 3.2: https://docs.spring.io/spring-kafka/reference/whats-new.html#x32-cp-ptm