三、kafka中事务的使用

本来以为我用过幂等性了,再使用事务应该很简单,不知道幂等性使用的可以去查看上一篇。

首先肯定是照着文档配置生产者,配置如下:

Properties props = new Properties();
        props.put("bootstrap.servers", "localhost:9092,localhost:9093,localhost:9094");
        props.put("acks", "all");
        props.put("retries", 3);
        props.put("batch.size", 163840);
        props.put("linger.ms", 10);
        props.put("buffer.memory", 33554432);
        props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
        props.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");
        props.put("enable.idempotence",true);
        props.put("max.in.flight.requests.per.connection",5);
        props.put("transactional.id","aaa");
        Producer<String, String> producer = new KafkaProducer<>(props);

就比幂等性多配个transactional.id,这样配好之后启动生产者一看,竟然报错了:

Exception in thread "main" java.lang.IllegalStateException: Cannot perform a 'send' before completing a call to initTransactions when transactions are enabled.
	at org.apache.kafka.clients.producer.internals.TransactionManager.failIfNotReadyForSend(TransactionManager.java:422)
	at org.apache.kafka.clients.producer.KafkaProducer.doSend(KafkaProducer.java:925)
	at org.apache.kafka.clients.producer.KafkaProducer.send(KafkaProducer.java:862)
	at org.apache.kafka.clients.producer.KafkaProducer.send(KafkaProducer.java:750)
	at org.apache.kafka.clients.producer.MyProducer.main(MyProducer.java:24)

错误所在的源码如下:

if (isTransactional()) {
            if (!hasProducerId())
                throw new IllegalStateException("Cannot perform a 'send' before completing a call to initTransactions " +
                        "when transactions are enabled.");

            if (currentState != State.IN_TRANSACTION)
                throw new IllegalStateException("Cannot call send in state " + currentState);
        }

源码大体意思是说,如果事务id不为空,而producerId未初始化,则抛错,看到这里我就疑惑了,怎么样才能初始化producerId呢,原来我用幂等性不也好好的吗,后来一查才知道,事务一定要有这一步producer.initTransactions();具体生产者代码如下:

Properties props = new Properties();
        props.put("bootstrap.servers", "localhost:9092,localhost:9093,localhost:9094");
        props.put("acks", "all");
        props.put("retries", 3);
        props.put("batch.size", 163840);
        props.put("linger.ms", 10);
        props.put("buffer.memory", 33554432);
        props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
        props.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");
        props.put("enable.idempotence",true);
        props.put("max.in.flight.requests.per.connection",5);
        props.put("transactional.id","aaa");
        Producer<String, String> producer = new KafkaProducer<>(props);
        producer.initTransactions();
        producer.beginTransaction();
        for(int i = 0; i < 5; i++){
            byte[] log = new byte[2];
            String slog = new String(log);
            producer.send(new ProducerRecord<String, String>("te_transaction",0, Integer.toString(i),  slog));
        }
        producer.commitTransaction();
        producer.close();

  • 4
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小飞侠fly

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值