question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Spring Boot 2.7.2 with Spring Kafka fails during initialisation with Exception: The 'ProducerFactory' must support transactions`

See original GitHub issue

I’m using Spring Boot v2.7.2 and the latest version of Spring Kafka provided by spring-boot-dependencies:

<dependency>
    <groupId>org.springframework.kafka</groupId>
    <artifactId>spring-kafka</artifactId>
</dependency>

I want the app to load all configuration from application.yaml, hence I created the beans with this bare minimum configuration:

    public class KakfaConfig {

    @Bean
    public ProducerFactory<Integer, FileUploadEvent> producerFactory() {
        return new DefaultKafkaProducerFactory<>(Collections.emptyMap());
    }

    @Bean
    public KafkaTemplate<Integer, FileUploadEvent> kafkaTemplate() {
        return new KafkaTemplate<Integer, anEvent>(producerFactory());
    }

}

It seems to work as expected and loads the configuration from the application.yaml below:

spring:
  application:
    name: my-app
  kafka:
    bootstrap-servers: localhost:9092
    producer:
      client-id: ${spring.application.name}
  #    transaction-id-prefix: "tx-"
    template:
      default-topic: my-topic

However, if I uncomment the transaction-id-prefix line, the application fails to start with the exception java.lang.IllegalArgumentException: The 'ProducerFactory' must support transactions
The documentation in here reads

If you provide a custom producer factory, it must support transactions. See ProducerFactory.transactionCapable().

The only way I have found to make it work is adding the transaction prefix programmatically as shown in the snippet below:

@Bean
public ProducerFactory<Integer, FileUploadEvent> fileUploadProducerFactory() {
    var pf = new DefaultKafkaProducerFactory<Integer, FileUploadEvent>(Collections.emptyMap());
    pf.setTransactionIdPrefix("tx-");
    return pf;
}

Any thoughts on how I can configure everything using application properties file?

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
wilkinsonacommented, Aug 15, 2022

AIUI, the intent is that transactions should be used and that the ProducerFactory should support them. The transaction-id-prefix property can be set and this results in the auto-configuration of the kafkaTransactionManager bean. However, if you define your own ProducerFactory (to constrain the types, for example) there’s no built-in way to have the transaction-id-prefix applied to that ProducerFactory.

@dbaltor One option could be to use @Value to inject the transaction ID prefix:

@Bean
public ProducerFactory<Integer, FileUploadEvent> fileUploadProducerFactory(
        @Value("spring.kafka.producer.transaction-id-prefix") String transactionIdPrefix) {
    var pf = new DefaultKafkaProducerFactory<Integer, FileUploadEvent>(Collections.emptyMap());
    pf.setTransactionIdPrefix(transactionIdPrefix);
    return pf;
}
0reactions
dbaltorcommented, Aug 18, 2022

That’s very interesting. Thank you @garyrussell ! At this time, I think I do need to create my own beans as I’m using bespoken (De)Serializers in both of my Producer and Consumer factories.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Spring Boot 2.7.2 with Spring Kafka fails during initialisation ...
The Spring Boot team replied as per below: The intent is that transactions should be used and that the ProducerFactory should support them....
Read more >
Spring for Apache Kafka
The NewTopic bean causes the topic to be created on the broker; it is not needed if the topic already exists. Spring Boot...
Read more >
Spring Boot + Kafka Transactions: ProducerFencedException
But when we deployed multiple instances of the application in production, we started seeing a lot of this error: org.apache.kafka.common.errors.
Read more >
spring-kafka 2.7.8 incompatible with spring-boot-starter-web ...
When using spring-kafka 2.7.8 with spring-boot-starter-web 2.5.X, a runtime error prevents the application from starting.
Read more >
Intro to Apache Kafka with Spring - Baeldung
In this tutorial, we'll cover Spring support for Kafka and the level ... To create messages, we first need to configure a ProducerFactory....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found