作者:岁月完好 | 来源:互联网 | 2023-08-17 18:06
RocketMQ实战(3)-事务消息干干净净没有故事,一个酷字贯穿一生。springBoot快速整合rabbitMQ。话不多少,上才艺~1.生产者方面1.1定
RocketMQ实战(3) -事务消息
干干净净没有故事,一个酷字贯穿一生。
springBoot快速整合rabbitMQ。话不多少,上才艺~
1.生产者方面
1.1 定义监听器------执行本地事务和事务会查:
@RocketMQTransactionListener
@Slf4j
public class TransactionListener implements RocketMQLocalTransactionListener {@Overridepublic RocketMQLocalTransactionState executeLocalTransaction(Message message, Object o) {log.info("============== executeLocalTransaction");return RocketMQLocalTransactionState.UNKNOWN;}@Overridepublic RocketMQLocalTransactionState checkLocalTransaction(Message message) {log.info("============== checkLocalTransaction");return RocketMQLocalTransactionState.COMMIT;}
}
1.2发送事务消息
@RequestMapping("/temSendTran")public String temSendTransaction(String message){String transactionId = UUID.randomUUID().toString();TransactionSendResult result = this.rocketMQTemplate.sendMessageInTransaction("test-tranc", MessageBuilder.withPayload(message).setHeader(RocketMQHeaders.TRANSACTION_ID, transactionId).build(), message);return "ok";}
2.消费者方面–定义对应的消息监听即可
@Component
@Slf4j
@RocketMQMessageListener(topic = "test-tranc", consumerGroup = "test-tranc-consumer")
public class TransConsumerListener implements RocketMQListener<Message> {&#64;Overridepublic void onMessage(Message message) {log.info("topic-tx received message: {}", message);}
}
创作不易&#xff0c;点个赞吧&#xff0c;您的赞&#xff0c;是我继续创作的动力。
编程并不是一蹴而就的&#xff0c;每天都是新的革命&#xff0c;加油呀~