0

I get a message from a source-topic. Then I split the message into 3 parts and send every parts to 3 different topics. Now 2 messages are delivered to 1st 2 topic successfully. But while sending 3rd message we get exceptions (e.g. ProducerFencedException | OutOfOrderSequenceException | AuthorizationException | RecordLengthException)

How to roll back / revert 2 other messages from the previous 2 topics ?

Full Java Code example will be very helpful. Dont want to use producer.initTransactions() kind of methods.

I refer this also - Transactional Kafka Producer but have doubt really we need to - write all the @Bean for Producer, template, factory , Tx - because those can easily e provided in application.yml.

user3575226
  • 111
  • 5
  • 15

1 Answers1

0

See the documentation https://docs.spring.io/spring-kafka/docs/current/reference/html/#transactions

Spring Boot will automatically configure a KafkaTransactionManager bean into the container factory when the transactionIdPrefix property is set; the container will then start the transaction and if any of the publishing fails, they will all roll back.

@Bean
@ConditionalOnProperty(name = "spring.kafka.producer.transaction-id-prefix")
@ConditionalOnMissingBean
public KafkaTransactionManager<?, ?> kafkaTransactionManager(ProducerFactory<?, ?> producerFactory) {
    return new KafkaTransactionManager<>(producerFactory);
}
Gary Russell
  • 166,535
  • 14
  • 146
  • 179
  • did a detail post - that it is not working - can you please check https://stackoverflow.com/questions/71591355/kafka-transaction-rollback-not-working-with-3-topics-for-recordtoolargeexception – user3575226 Mar 23 '22 at 17:18