1、使用RocketMQTemplate发送事务消息
/**
* 发送RocketMq事务消息demo,事务提交和检查见类 SayHelloMsgListener
* @return
*/
@RequestMapping("rocket-mq-send-msg-with-transaction")
public String testRocketMQSendMsgWithTag(){
this.rocketMQTemplate.sendMessageInTransaction("hello-producer-tx-group",
"say-msg:tag1",
MessageBuilder.withPayload(
TransactionMsg.builder()
.msg("hello world 1")
.build()
)
.setHeader(RocketMQHeaders.TRANSACTION_ID, UUID.randomUUID())
.build(),
Integer.valueOf(1));
this.rocketMQTemplate.sendMessageInTransaction("hello-producer-tx-group",
"say-msg:tag2",
MessageBuilder.withPayload(
TransactionMsg.builder()
.msg("hello world 2")
.build()
)
.setHeader(RocketMQHeaders.TRANSACTION_ID, UUID.randomUUID())
.build(),
Integer.valueOf(2));
this.rocketMQTemplate.sendMessageInTransaction("hello-producer-tx-group",
"say-msg:tag3",
MessageBuilder.withPayload(
TransactionMsg.builder()
.msg("hello world 3")
.build()
)
.setHeader(RocketMQHeaders.TRANSACTION_ID, UUID.randomUUID())
.build(),
Integer.valueOf(3));
this.rocketMQTemplate.sendMessageInTransaction("hello-producer-tx-group",
"say-msg:tag4",
MessageBuilder.withPayload(
TransactionMsg.builder()
.msg("hello world 4")
.build()
)
.setHeader(RocketMQHeaders.TRANSACTION_ID, UUID.randomUUID())
.build(),
Integer.valueOf(4));
return "success";
}
2、编写RocketMQLocalTransactionListener实现类SayHelloMsgListener
注意:一定要指定txProducerGroup为RocketMQTemplate发送事务消息指定的txProducerGroup,如下:
@RocketMQTransactionListener(txProducerGroup="hello-producer-tx-group")
否则会报:Can not found MQProducer 'hello-producer-tx-group' in cache! please define @RocketMQLocalTransactionListener class or invoke createOrGetStartedTransactionMQProducer() to create it firstly
package com.codex.terry.rocketmq;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.apache.rocketmq.spring.annotation.RocketMQTransactionListener;
import org.apache.rocketmq.spring.core.RocketMQLocalTransactionListener;
import org.apache.rocketmq.spring.core.RocketMQLocalTransactionState;
import org.apache.rocketmq.spring.support.RocketMQHeaders;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageHeaders;
/**
* 文件名称: SayHelloMsgListener.java
* 编写人: yh.zeng
* 编写时间: 2024/8/20 13:47
* 文件描述: 发送事务消息的本地事务执行和事务状态检查
*/
@Slf4j
@RocketMQTransactionListener(txProducerGroup="hello-producer-tx-group")
public class SayHelloMsgListener implements RocketMQLocalTransactionListener
{
@Override
public RocketMQLocalTransactionState executeLocalTransaction(Message message, Object o) {
log.info("this.rocketMQTemplate.sendMessageInTransaction方法传入的arg={}", o);
MessageHeaders headers = message.getHeaders();
String tags = String.valueOf(headers.get(RocketMQHeaders.PREFIX + RocketMQHeaders.TAGS));
String topic = String.valueOf(headers.get(RocketMQHeaders.PREFIX+ RocketMQHeaders.TOPIC));
log.info("TOPIC={}, TRANSACTION_ID={}, TAGS={}", topic, headers.get(RocketMQHeaders.TRANSACTION_ID), tags);
if("say-msg".equals(topic)) {
if (StringUtils.contains(tags, "tag1")) {
return RocketMQLocalTransactionState.COMMIT;
} else if (StringUtils.contains(tags, "tag2")) {
return RocketMQLocalTransactionState.ROLLBACK;
} else if (StringUtils.contains(tags, "tag3")) {
return RocketMQLocalTransactionState.UNKNOWN;
} else if (StringUtils.contains(tags, "tag4")) {
return RocketMQLocalTransactionState.COMMIT;
} else {
return RocketMQLocalTransactionState.UNKNOWN;
}
} else {
return RocketMQLocalTransactionState.UNKNOWN;
}
}
@Override
public RocketMQLocalTransactionState checkLocalTransaction(Message message) {
MessageHeaders headers = message.getHeaders();
String tags = String.valueOf(headers.get(RocketMQHeaders.PREFIX + RocketMQHeaders.TAGS));
String topic = String.valueOf(headers.get(RocketMQHeaders.PREFIX+ RocketMQHeaders.TOPIC));
if ("say-msg".equals(topic)) {
if (StringUtils.contains(tags, "tag3")) {
return RocketMQLocalTransactionState.COMMIT;
} else {
return RocketMQLocalTransactionState.UNKNOWN;
}
} else {
return RocketMQLocalTransactionState.UNKNOWN;
}
}
}