|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +package org.apache.seata.integration.rocketmq; |
| 18 | + |
| 19 | +import org.apache.rocketmq.client.producer.SendStatus; |
| 20 | +import org.apache.seata.common.util.StringUtils; |
| 21 | +import org.apache.seata.core.context.RootContext; |
| 22 | +import org.apache.seata.core.model.GlobalStatus; |
| 23 | +import org.apache.seata.rm.DefaultResourceManager; |
| 24 | +import org.apache.rocketmq.client.Validators; |
| 25 | +import org.apache.rocketmq.client.exception.MQBrokerException; |
| 26 | +import org.apache.rocketmq.client.exception.MQClientException; |
| 27 | +import org.apache.rocketmq.client.producer.LocalTransactionState; |
| 28 | +import org.apache.rocketmq.client.producer.SendResult; |
| 29 | +import org.apache.rocketmq.client.producer.TransactionListener; |
| 30 | +import org.apache.rocketmq.client.producer.TransactionMQProducer; |
| 31 | +import org.apache.rocketmq.common.message.Message; |
| 32 | +import org.apache.rocketmq.common.message.MessageAccessor; |
| 33 | +import org.apache.rocketmq.common.message.MessageConst; |
| 34 | +import org.apache.rocketmq.common.message.MessageExt; |
| 35 | +import org.apache.rocketmq.remoting.RPCHook; |
| 36 | +import org.apache.rocketmq.remoting.exception.RemotingException; |
| 37 | +import org.slf4j.Logger; |
| 38 | +import org.slf4j.LoggerFactory; |
| 39 | + |
| 40 | +import java.util.Arrays; |
| 41 | +import java.util.List; |
| 42 | + |
| 43 | +/** |
| 44 | + * Seata MQ Producer |
| 45 | + **/ |
| 46 | +public class SeataMQProducer extends TransactionMQProducer { |
| 47 | + |
| 48 | + private static final Logger LOGGER = LoggerFactory.getLogger(SeataMQProducer.class); |
| 49 | + |
| 50 | + private static final List<GlobalStatus> COMMIT_STATUSES = Arrays.asList(GlobalStatus.Committed, GlobalStatus.Committing, GlobalStatus.CommitRetrying); |
| 51 | + private static final List<GlobalStatus> ROLLBACK_STATUSES = Arrays.asList(GlobalStatus.Rollbacked, GlobalStatus.Rollbacking, GlobalStatus.RollbackRetrying); |
| 52 | + |
| 53 | + public static String PROPERTY_SEATA_XID = RootContext.KEY_XID; |
| 54 | + public static String PROPERTY_SEATA_BRANCHID = RootContext.KEY_BRANCHID; |
| 55 | + private TransactionListener transactionListener; |
| 56 | + |
| 57 | + private TCCRocketMQ tccRocketMQ; |
| 58 | + |
| 59 | + SeataMQProducer(final String producerGroup) { |
| 60 | + this(null, producerGroup, null); |
| 61 | + } |
| 62 | + |
| 63 | + SeataMQProducer(final String namespace, final String producerGroup, RPCHook rpcHook) { |
| 64 | + super(namespace, producerGroup, rpcHook); |
| 65 | + this.transactionListener = new TransactionListener() { |
| 66 | + @Override |
| 67 | + public LocalTransactionState executeLocalTransaction(Message msg, Object arg) { |
| 68 | + return LocalTransactionState.UNKNOW; |
| 69 | + } |
| 70 | + |
| 71 | + @Override |
| 72 | + public LocalTransactionState checkLocalTransaction(MessageExt msg) { |
| 73 | + String xid = msg.getProperty(PROPERTY_SEATA_XID); |
| 74 | + if (StringUtils.isBlank(xid)) { |
| 75 | + LOGGER.error("msg has no xid, msgTransactionId: {}, msg will be rollback", msg.getTransactionId()); |
| 76 | + return LocalTransactionState.ROLLBACK_MESSAGE; |
| 77 | + } |
| 78 | + GlobalStatus globalStatus = DefaultResourceManager.get().getGlobalStatus(SeataMQProducerFactory.ROCKET_BRANCH_TYPE, xid); |
| 79 | + if (COMMIT_STATUSES.contains(globalStatus)) { |
| 80 | + return LocalTransactionState.COMMIT_MESSAGE; |
| 81 | + } else if (ROLLBACK_STATUSES.contains(globalStatus) || GlobalStatus.isOnePhaseTimeout(globalStatus)) { |
| 82 | + return LocalTransactionState.ROLLBACK_MESSAGE; |
| 83 | + } else if (GlobalStatus.Finished.equals(globalStatus)) { |
| 84 | + LOGGER.error("global transaction finished, msg will be rollback, xid: {}", xid); |
| 85 | + return LocalTransactionState.ROLLBACK_MESSAGE; |
| 86 | + } |
| 87 | + return LocalTransactionState.UNKNOW; |
| 88 | + } |
| 89 | + }; |
| 90 | + } |
| 91 | + |
| 92 | + @Override |
| 93 | + public SendResult send(Message msg) throws MQClientException, MQBrokerException, RemotingException, InterruptedException { |
| 94 | + return send(msg, this.getSendMsgTimeout()); |
| 95 | + } |
| 96 | + |
| 97 | + @Override |
| 98 | + public SendResult send(Message msg, long timeout) throws MQClientException, MQBrokerException, RemotingException, InterruptedException { |
| 99 | + if (RootContext.inGlobalTransaction()) { |
| 100 | + if (tccRocketMQ == null) { |
| 101 | + throw new RuntimeException("TCCRocketMQ is not initialized"); |
| 102 | + } |
| 103 | + return tccRocketMQ.prepare(msg, timeout); |
| 104 | + } else { |
| 105 | + return super.send(msg, timeout); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + public SendResult doSendMessageInTransaction(final Message msg, long timeout, String xid, long branchId) throws MQClientException { |
| 110 | + msg.setTopic(withNamespace(msg.getTopic())); |
| 111 | + if (msg.getDelayTimeLevel() != 0) { |
| 112 | + MessageAccessor.clearProperty(msg, MessageConst.PROPERTY_DELAY_TIME_LEVEL); |
| 113 | + } |
| 114 | + Validators.checkMessage(msg, this); |
| 115 | + |
| 116 | + SendResult sendResult = null; |
| 117 | + MessageAccessor.putProperty(msg, MessageConst.PROPERTY_TRANSACTION_PREPARED, "true"); |
| 118 | + MessageAccessor.putProperty(msg, MessageConst.PROPERTY_PRODUCER_GROUP, this.getProducerGroup()); |
| 119 | + MessageAccessor.putProperty(msg, PROPERTY_SEATA_XID, xid); |
| 120 | + MessageAccessor.putProperty(msg, PROPERTY_SEATA_BRANCHID, String.valueOf(branchId)); |
| 121 | + try { |
| 122 | + sendResult = super.send(msg, timeout); |
| 123 | + } catch (Exception e) { |
| 124 | + throw new MQClientException("send message Exception", e); |
| 125 | + } |
| 126 | + |
| 127 | + if (SendStatus.SEND_OK != sendResult.getSendStatus()) { |
| 128 | + throw new RuntimeException("Message send fail.status=" + sendResult.getSendStatus()); |
| 129 | + } |
| 130 | + if (sendResult.getTransactionId() != null) { |
| 131 | + msg.putUserProperty("__transactionId__", sendResult.getTransactionId()); |
| 132 | + } |
| 133 | + String transactionId = msg.getProperty(MessageConst.PROPERTY_UNIQ_CLIENT_MESSAGE_ID_KEYIDX); |
| 134 | + if (null != transactionId && !"".equals(transactionId)) { |
| 135 | + msg.setTransactionId(transactionId); |
| 136 | + } |
| 137 | + return sendResult; |
| 138 | + } |
| 139 | + |
| 140 | + |
| 141 | + @Override |
| 142 | + public TransactionListener getTransactionListener() { |
| 143 | + return transactionListener; |
| 144 | + } |
| 145 | + |
| 146 | + public void setTccRocketMQ(TCCRocketMQ tccRocketMQ) { |
| 147 | + this.tccRocketMQ = tccRocketMQ; |
| 148 | + } |
| 149 | +} |
0 commit comments