-
Couldn't load subscription status.
- Fork 8.9k
feature : RocketMQ transaction are supported #6230
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 21 commits
Commits
Show all changes
59 commits
Select commit
Hold shift + click to select a range
8edf4d2
license
Bughue 5397478
rocket
Bughue f67c651
style
Bughue 304df90
check listener
Bughue 93b96f3
bean holder
Bughue 052532b
bean holder
Bughue b90978b
TCCRocketMQ
Bughue 8803a39
rocketmq
Bughue 0c335bc
Merge branch '2.x' of https://github.com/seata/seata into dev-rocketmq
Bughue 625ff48
rocketmq
Bughue e11a818
rocketmq
Bughue 1d76507
rocketmq
Bughue 4f74db2
rocketmq
Bughue 4f10e9e
rocketmq
Bughue bafee71
independent module
Bughue 7fb2ee1
style
Bughue cc7cec4
style
Bughue daf0888
style bom
Bughue 5f42ad9
module
Bughue b85c799
style
Bughue 87643f7
style
Bughue 0a16810
XID
Bughue 768f862
rocket mq factory
Bughue 6efaa5c
optimize producer
Bughue 2e9d615
Merge branch '2.x' of https://github.com/seata/seata into dev-rocketmq
Bughue 1580478
style
Bughue f1f37c0
fix configuration
Bughue 748e733
getGlobalStatus in AbstractResourceManager
Bughue d31fc70
test
Bughue fc71d14
test
Bughue 2b700ab
remove configuration
Bughue bb41a20
Merge branch '2.x' of https://github.com/seata/seata into dev-rocketmq
Bughue b8cc88f
tcc api
Bughue dc9e7c5
tcc api
Bughue e5a4790
style
Bughue 4faffc7
to many getXX
Bughue 2554991
unit test
Bughue 2e53a3a
unit test
Bughue 3b57588
style
Bughue 547df7e
style
Bughue cfbb664
style
Bughue 1be3c83
style
Bughue 345baad
mock test
Bughue 30bef40
mock test
Bughue c91c58d
includeFromCI
Bughue 343c586
excludeCI
Bughue d17c95e
excludeCI
Bughue b7af430
excludeCI
Bughue 1318eb0
test
Bughue de54524
test
Bughue 5b16241
test
Bughue 4d74963
test
Bughue 96e91c4
update
funky-eyes e273e87
Merge branch '2.x' of github.com:seata/seata into dev-rocketmq
funky-eyes 1de6fd8
update
funky-eyes c67ad75
update
funky-eyes eef9e66
update
funky-eyes 8fd38b7
update
funky-eyes 0c6e901
update
funky-eyes File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <!-- | ||
| 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. | ||
| --> | ||
| <project xmlns="http://maven.apache.org/POM/4.0.0" | ||
| xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
| xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
| <parent> | ||
| <groupId>org.apache.seata</groupId> | ||
| <artifactId>seata-parent</artifactId> | ||
| <version>${revision}</version> | ||
| <relativePath>../pom.xml</relativePath> | ||
| </parent> | ||
| <modelVersion>4.0.0</modelVersion> | ||
| <artifactId>seata-rocketmq</artifactId> | ||
| <packaging>jar</packaging> | ||
| <name>seata-rocketmq ${project.version}</name> | ||
| <description>rocketmq integration for Seata built with Maven</description> | ||
|
|
||
| <dependencies> | ||
| <dependency> | ||
| <groupId>${project.groupId}</groupId> | ||
| <artifactId>seata-tcc</artifactId> | ||
| <version>${project.version}</version> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>org.apache.rocketmq</groupId> | ||
| <artifactId>rocketmq-client</artifactId> | ||
| <scope>provided</scope> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>org.springframework</groupId> | ||
| <artifactId>spring-context</artifactId> | ||
Bughue marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| </dependency> | ||
| </dependencies> | ||
|
|
||
|
|
||
| </project> | ||
166 changes: 166 additions & 0 deletions
166
rocketmq/src/main/java/org/apache/seata/integration/rocketmq/SeataMQProducer.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,166 @@ | ||
| /* | ||
| * 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 = "__SEATA_XID"; | ||
Bughue marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| public static String PROPERTY_SEATA_BRANCHID = "__SEATA_BRANCHID"; | ||
| private TransactionListener transactionListener; | ||
|
|
||
| private TCCRocketMQ tccRocketMQ; | ||
|
|
||
| public SeataMQProducer(final String producerGroup) { | ||
| this(null, producerGroup, null); | ||
| } | ||
|
|
||
| public 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); | ||
Bughue marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| try { | ||
| GlobalStatus globalStatus = DefaultResourceManager.get().getGlobalStatus(xid); | ||
| if (commitStatuses.contains(globalStatus)) { | ||
| return LocalTransactionState.COMMIT_MESSAGE; | ||
| } else if (rollbackStatuses.contains(globalStatus) || GlobalStatus.isOnePhaseTimeout(globalStatus)) { | ||
Bughue marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| 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 void setTccRocketMQ(TCCRocketMQ tccRocketMQ) { | ||
| this.tccRocketMQ = tccRocketMQ; | ||
| } | ||
|
|
||
| public SendResult send(Message msg) throws MQClientException, MQBrokerException, RemotingException, InterruptedException { | ||
Bughue marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return send(msg, this.defaultMQProducerImpl.getDefaultMQProducer().getSendMsgTimeout()); | ||
| } | ||
|
|
||
| @Override | ||
| public SendResult send(Message msg, long timeout) throws MQClientException, MQBrokerException, RemotingException, InterruptedException { | ||
| if (RootContext.inGlobalTransaction()) { | ||
| if (tccRocketMQ == null) { | ||
| throw new RuntimeException("TCCRocketMQ is null"); | ||
| } | ||
| return tccRocketMQ.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 = this.send(msg, timeout); | ||
| } catch (Exception e) { | ||
| throw new MQClientException("send message Exception", e); | ||
| } | ||
|
|
||
| if (SendStatus.SEND_OK != sendResult.getSendStatus()) { | ||
funky-eyes marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| throw new RuntimeException("Message send fail.status=" + sendResult.getSendStatus()); | ||
| } | ||
| if (sendResult.getTransactionId() != null) { | ||
| msg.putUserProperty("__transactionId__", sendResult.getTransactionId()); | ||
| } | ||
| String transactionId = msg.getProperty(MessageConst.PROPERTY_UNIQ_CLIENT_MESSAGE_ID_KEYIDX); | ||
| if (null != transactionId && !"".equals(transactionId)) { | ||
| msg.setTransactionId(transactionId); | ||
| } | ||
| return sendResult; | ||
| } | ||
|
|
||
|
|
||
| public static SeataMQProducer create(String groupName, String ak, String sk, boolean isEnableMsgTrace, String customizedTraceTopic) { | ||
| boolean isEnableAcl = !StringUtils.isEmpty(ak) && !StringUtils.isEmpty(sk); | ||
| if (isEnableAcl) { | ||
| LOGGER.warn("ACL is not supported yet in SeataMQProducer"); | ||
| } | ||
| if (isEnableMsgTrace) { | ||
| LOGGER.warn("MessageTrace is not supported yet in SeataMQProducer"); | ||
| } | ||
| return new SeataMQProducer(groupName); | ||
| } | ||
|
|
||
|
|
||
| @Override | ||
| public TransactionListener getTransactionListener() { | ||
| return transactionListener; | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.