首页 > 解决方案 > psringboot 应用程序中的 KafkaProducer 事务

问题描述

我想在成功发送关于 kafka 主题的所有消息后执行一些代码。我读过很多文章。知道我们可以使用事务发送消息。

kafkaTemplate.executeInTransaction(operations -> {
            for (String s : request) {
                kafkaTemplate.send(new ProducerRecord<>(topic,key,s));
            }
            return null;
        });

我尝试了这么短的代码,但不确定在成功执行此事务后如何执行某些代码。我也通过开始交易尝试了 kafkaProducer,但这也不适用于我的情况。如果有任何想法会有所帮助。

标签: apache-kafkaspring-kafka

解决方案


  1. Consumers must have isolation.level=read_committed to not see records until they are committed.

  2. There is no way to prevent a consumer from seeing a record that was sent outside of a transaction.


推荐阅读