java.lang.IllegalStateException: No transaction is in process; possible solutions: run the template operation within the scope of a template.executeInTransaction() operation, start a transaction with @Transactional before invoking the template method, run in a transaction started by a listener container when consuming a record at org.springframework.util.Assert.state(Assert.java:76) ~[spring-core-5.3.22.jar:5.3.22] at org.springframework.kafka.core.KafkaTemplate.getTheProducer(KafkaTemplate.java:734) ~[spring-kafka-2.8.8.jar:2.8.8] at org.springframework.kafka.core.KafkaTemplate.doSend(KafkaTemplate.java:644) ~[spring-kafka-2.8.8.jar:2.8.8] at org.springframework.kafka.core.KafkaTemplate.send(KafkaTemplate.java:415) ~[spring-kafka-2.8.8.jar:2.8.8]
意思是发送事务消息不能使用此种消息发送。
编写事务消费发送。
1 2 3 4 5 6 7 8 9 10 11 12 13 14
@RequestMapping("/sendTransaction") @Transactional public String sendTransaction(String msg){ kafkaTemplate.executeInTransaction(newKafkaOperations.OperationsCallback<String, String, Object>() { @Override public Boolean doInOperations(KafkaOperations<String, String> operations) { for (inti=0; i < 10; i++) { operations.send("range_topic",i % 5,"","tx_"+msg); } returntrue; } }); return"true"; }