RocketMQ高手之路系列之九:RocketMQ之事务消息(二)

本文详细解析了在分布式消息系统中,如何通过DefaultMQProducerImpl发送带有事务的消息,包括消息检测、事务标志设置、本地事务执行、后置处理以及事务状态回查的关键步骤。
摘要由CSDN通过智能技术生成

@Override

public TransactionSendResult sendMessageInTransaction(final Message msg,

final Object arg) throws MQClientException {

if (null == this.transactionListener) {

throw new MQClientException(“TransactionListener is null”, null);

}

return this.defaultMQProducerImpl.sendMessageInTransaction(msg, null, arg);

}

}

DefaultMQProducerImpl中进行事务消息调用,关键步骤在代码中进行了标注,如下所示:

public class DefaultMQProducerImpl implements MQProducerInner {

public TransactionSendResult sendMessageInTransaction(final Message msg,

final LocalTransactionExecuter localTransactionExecuter, final Object arg)

throws MQClientException {

TransactionListener transactionListener = getCheckListener();

if (null == localTransactionExecuter && null == transactionListener) {

throw new MQClientException(“tranExecutor is null”, null);

}

//1、进行消息检测

Validators.checkMessage(msg, this.defaultMQProducer);

SendResult sendResult = null;

//2、设置事务消息

MessageAccessor.putProperty(msg, MessageConst.PROPERTY_TRANSACTION_PREPARED, “true”);

MessageAccessor.putProperty(msg, MessageConst.PROPERTY_PRODUCER_GROUP, this.defaultMQProducer.getProducerGroup());

try {

//3、同步发送事务消息

sendResult = this.send(msg);

} catch (Exception e) {

throw new MQClientException(“send message Exception”, e);

}

LocalTransactionState localTransactionState = LocalTransactionState.UNKNOW;

Throwable localException = null;

switch (sendResult.getSendStatus()) {

case SEND_OK: {

try {

if (sendResult.getTransactionId() != null) {

msg.putUserProperty(“transactionId”, sendResult.getTransactionId());

}

String transactionId = msg.getProperty(MessageConst.PROPERTY_UNIQ_CLIENT_MESSAGE_ID_KEYIDX);

//4、发送成功,设置transactionId

if (null != transactionId && !“”.equals(transactionId)) {

msg.setTransactionId(transactionId);

}

if (null != localTransactionExecuter) {

localTransactionState = localTransactionExecuter.executeLocalTransactionBranch(msg, arg);

} else if (transactionListener != null) {

log.debug(“Used new transaction API”);

//5、执行本地事务,获取本地事务执行状态

localTransactionState = transactionListener.executeLocalTransaction(msg, arg);

}

if (null == localTransactionState) {

localTransactionState = LocalTransactionState.UNKNOW;

}

if (localTransactionState != LocalTransactionState.COMMIT_MESSAGE) {

log.info(“executeLocalTransactionBranch return {}”, localTransactionState);

log.info(msg.toString());

}

} catch (Throwable e) {

log.info(“executeLocalTransactionBranch exception”, e);

log.info(msg.toString());

localException = e;

}

}

break;

case FLUSH_DISK_TIMEOUT:

case FLUSH_SLAVE_TIMEOUT:

case SLAVE_NOT_AVAILABLE:

localTransactionState = LocalTransactionState.ROLLBACK_MESSAGE;

break;

default:

break;

}

try {

//6、更具本地事务执行状态,执行半消息的提交或者回滚

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值