Bughue commented on code in PR #6230:
URL: https://github.com/apache/incubator-seata/pull/6230#discussion_r1495185387


##########
rocketmq/src/main/java/org/apache/seata/integration/rocketmq/SeataMQProducer.java:
##########
@@ -0,0 +1,148 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.seata.integration.rocketmq;
+
+import org.apache.rocketmq.client.producer.SendStatus;
+import org.apache.seata.common.util.StringUtils;
+import org.apache.seata.core.context.RootContext;
+import org.apache.seata.core.model.GlobalStatus;
+import org.apache.seata.rm.DefaultResourceManager;
+import org.apache.rocketmq.client.Validators;
+import org.apache.rocketmq.client.exception.MQBrokerException;
+import org.apache.rocketmq.client.exception.MQClientException;
+import org.apache.rocketmq.client.producer.LocalTransactionState;
+import org.apache.rocketmq.client.producer.SendResult;
+import org.apache.rocketmq.client.producer.TransactionListener;
+import org.apache.rocketmq.client.producer.TransactionMQProducer;
+import org.apache.rocketmq.common.message.Message;
+import org.apache.rocketmq.common.message.MessageAccessor;
+import org.apache.rocketmq.common.message.MessageConst;
+import org.apache.rocketmq.common.message.MessageExt;
+import org.apache.rocketmq.remoting.RPCHook;
+import org.apache.rocketmq.remoting.exception.RemotingException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.Arrays;
+import java.util.List;
+import java.util.concurrent.TimeoutException;
+
+/**
+ * Seata MQ Producer
+ **/
+public class SeataMQProducer extends TransactionMQProducer {
+
+    private static final Logger LOGGER = 
LoggerFactory.getLogger(SeataMQProducer.class);
+
+    public static String PROPERTY_SEATA_XID = RootContext.KEY_XID;
+    public static String PROPERTY_SEATA_BRANCHID = "TX_BRANCHID";
+    private TransactionListener transactionListener;
+
+    SeataMQProducer(final String producerGroup) {
+        this(null, producerGroup, null);
+    }
+
+    SeataMQProducer(final String namespace, final String producerGroup, 
RPCHook rpcHook) {
+        super(namespace, producerGroup, rpcHook);
+        this.transactionListener = new TransactionListener() {
+            @Override
+            public LocalTransactionState executeLocalTransaction(Message msg, 
Object arg) {
+                return LocalTransactionState.UNKNOW;
+            }
+
+            @Override
+            public LocalTransactionState checkLocalTransaction(MessageExt msg) 
{
+                String xid = msg.getProperty(PROPERTY_SEATA_XID);
+                if (StringUtils.isBlank(xid)) {
+                    LOGGER.error("msg has no xid, msgTransactionId: {}, msg 
will be rollback", msg.getTransactionId());
+                    return LocalTransactionState.ROLLBACK_MESSAGE;
+                }
+                List<GlobalStatus> commitStatuses = 
Arrays.asList(GlobalStatus.Committed, GlobalStatus.Committing, 
GlobalStatus.CommitRetrying);
+                List<GlobalStatus> rollbackStatuses = 
Arrays.asList(GlobalStatus.Rollbacked, GlobalStatus.Rollbacking, 
GlobalStatus.RollbackRetrying);
+                try {
+                    GlobalStatus globalStatus = 
DefaultResourceManager.get().getGlobalStatus(xid);
+                    if (commitStatuses.contains(globalStatus)) {
+                        return LocalTransactionState.COMMIT_MESSAGE;
+                    } else if (rollbackStatuses.contains(globalStatus) || 
GlobalStatus.isOnePhaseTimeout(globalStatus)) {
+                        return LocalTransactionState.ROLLBACK_MESSAGE;
+                    } else if (GlobalStatus.Finished.equals(globalStatus)) {
+                        LOGGER.error("global transaction finished, msg will be 
rollback, xid: {}", xid);
+                        return LocalTransactionState.ROLLBACK_MESSAGE;
+                    }
+                } catch (TimeoutException e) {
+                    LOGGER.error("getGlobalStatus error, xid: {}, 
msgTransactionId: {}", xid, msg.getTransactionId(), e);
+                }
+                return LocalTransactionState.UNKNOW;
+            }
+        };
+    }
+
+
+    public SendResult send(Message msg) throws MQClientException, 
MQBrokerException, RemotingException, InterruptedException {
+        return send(msg, 
this.defaultMQProducerImpl.getDefaultMQProducer().getSendMsgTimeout());
+    }
+
+    @Override
+    public SendResult send(Message msg, long timeout) throws 
MQClientException, MQBrokerException, RemotingException, InterruptedException {
+        if (RootContext.inGlobalTransaction()) {
+            if (SeataMQProducerFactory.getTccRocketMQ() == null) {
+                throw new RuntimeException("TCCRocketMQ is not initialized");
+            }
+            return SeataMQProducerFactory.getTccRocketMQ().prepare(msg, 
timeout);
+        } else {
+            return super.send(msg, timeout);
+        }
+    }
+
+    public SendResult doSendMessageInTransaction(final Message msg, long 
timeout, String xid, long branchId) throws MQClientException {
+        msg.setTopic(withNamespace(msg.getTopic()));
+        if (msg.getDelayTimeLevel() != 0) {
+            MessageAccessor.clearProperty(msg, 
MessageConst.PROPERTY_DELAY_TIME_LEVEL);
+        }
+
+        Validators.checkMessage(msg, 
this.defaultMQProducerImpl.getDefaultMQProducer());
+
+        SendResult sendResult = null;
+        MessageAccessor.putProperty(msg, 
MessageConst.PROPERTY_TRANSACTION_PREPARED, "true");
+        MessageAccessor.putProperty(msg, MessageConst.PROPERTY_PRODUCER_GROUP, 
this.defaultMQProducerImpl.getDefaultMQProducer().getProducerGroup());
+        MessageAccessor.putProperty(msg, PROPERTY_SEATA_XID, xid);
+        MessageAccessor.putProperty(msg, PROPERTY_SEATA_BRANCHID, 
String.valueOf(branchId));
+        try {
+            sendResult = super.send(msg, timeout);
+        } catch (Exception e) {
+            throw new MQClientException("send message Exception", e);
+        }
+
+        if (SendStatus.SEND_OK != sendResult.getSendStatus()) {

Review Comment:
   这里说的是做seata的 branchreport?这段逻辑是在prepare内部发生的,所以这里抛出异常之后tcc自己会上报fail的



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to