RocketMQ实战(3) -事务消息
干干净净没有故事,一个酷字贯穿一生。
springBoot快速整合rabbitMQ。话不多少,上才艺~
1.生产者方面
1.1 定义监听器------执行本地事务和事务会查:
@RocketMQTransactionListener
@Slf4j
public class TransactionListener implements RocketMQLocalTransactionListener {
@Override
public RocketMQLocalTransactionState executeLocalTransaction(Message message, Object o) {
// 模拟本地事务不通过
log.info("============== executeLocalTransaction");
return RocketMQLocalTransactionState.UNKNOWN;
}
@Override
public 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> {
@Override
public void onMessage(Message message) {
log.info("topic-tx received message: {}", message);
}
}
创作不易,点个赞吧,您的赞,是我继续创作的动力。
编程并不是一蹴而就的,每天都是新的革命,加油呀~