هذه الصفحة غير متوفرة حاليًا بلغتك المحلية. نحن نعمل جاهدين على إضافة المزيد من اللغات. شاكرين تفهمك ودعمك المستمر لنا.
- What's New
- Function Overview
- Product Bulletin
- Service Overview
- Billing
- Getting Started
-
User Guide
- Process of Using RocketMQ
- Permissions Management
- Buying a RocketMQ Instance
- Configuring a Topic
- Accessing an Instance
- Managing Messages
- Managing Consumer Groups
-
Managing Instances
- Viewing and Modifying Basic Information of a RocketMQ Instance
- Viewing Background Tasks of a RocketMQ Instance
- Configuring Tags for a RocketMQ Instance
- Exporting RocketMQ Instances
- Diagnosing a RocketMQ Instance
- Restarting Brokers of a RocketMQ Instance
- Deleting a RocketMQ Instance
- Configuring RocketMQ Recycling Policies
- Configuring SSL of a RocketMQ Instance
- Enabling Flexible TPS for a RocketMQ Instance
- Modifying RocketMQ Specifications
- Migrating Metadata
- Testing Instance Performance
- Viewing Monitoring Metrics and Configuring Alarms
- Viewing RocketMQ Audit Logs
- Best Practices
- Developer Guide
-
API Reference
- Before You Start
- API Overview
- Calling APIs
- Getting Started
-
APIs V2 (Recommended)
- Lifecycle Management
-
Consumer Group Management
- Querying the Consumer Group List
- Creating a Consumer Group or Batch Deleting Consumer Groups
- Batch Modifying Consumer Groups
- Deleting a Consumer Group
- Querying a Consumer Group
- Modifying a Consumer Group
- Querying the Consumer Group List or Details
- Resetting the Consumer Offset
- Querying the Consumer List
- Topic Management
- Message Management
- User Management
- Metadata Migration
- Managing Parameters
- Tag Management
- Other APIs
- Specification Modification Management
- Example Applications
- Permissions and Supported Actions
- Appendix
- Change History
- SDK Reference
- FAQs
- Videos
-
More Documents
-
User Guide (ME-Abu Dhabi Region)
- Service Overview
- Getting Started
- Permissions Management
- Preparing Required Resources
- Buying an Instance
- Accessing an Instance
- Managing Instances
- Managing Topics
- Querying Messages
- Managing Consumer Groups
- Managing Users
- Managing Dead Letter Queues
- Diagnosing an Instance
- Migrating Metadata
- Monitoring
- Auditing
- FAQs
- Change History
- API Reference (ME-Abu Dhabi Region)
- Developer Guide(ME-Abu Dhabi Region)
-
User Guide (Paris Region)
- Service Overview
- Getting Started
- Permissions Management
- Preparing Required Resources
- Creating an Instance
- Accessing an Instance
- Managing Instances
- Managing Topics
- Managing Messages
- Managing Consumer Groups
- Managing Users
- Managing Dead Letter Queues
- Diagnosing an Instance
- Migrating Metadata
- Monitoring
- Auditing
- FAQs
- Change History
- API Reference (Paris Region)
-
User Guide (Kuala Lumpur Region)
- Service Overview
- Getting Started
- Permissions Management
- Preparing Required Resources
- Buying an Instance
- Accessing an Instance
- Managing Instances
- Managing Topics
- Managing Messages
- Managing Consumer Groups
- Managing Users
- Managing Dead Letter Queues
- Diagnosing an Instance
- Migrating Metadata
- Monitoring
- Auditing
- FAQs
- Change History
-
API Reference (Kuala Lumpur Region)
- Before You Start
- API Overview
- Calling APIs
- Getting Started
- APIs V2 (Recommended)
- Permissions and Supported Actions
- Appendix
- Change History
-
User Guide (ME-Abu Dhabi Region)
- General Reference
Copied.
Sending and Receiving Transactional Messages
DMS for RocketMQ ensures transaction consistency between the service logic and message transmission, and implements transaction support in two phases. Figure 1 illustrates the interaction of transactional messages.
The producer sends a half message and then executes the local transaction. If the execution is successful, the transaction is committed. If the execution fails, the transaction is rolled back. If the server does not receive any commit or rollback request after a period of time, it initiates a check. After receiving the check request, the producer resends a transaction commit or rollback request. The message is delivered to the consumer only after being committed. The consumer is unaware of the rollback.
Before sending and receiving transactional messages, collect RocketMQ connection information by referring to Collecting Connection Information.
Notes and Constraints
To receive and send transactional messages, ensure the topic message type is Transactional before connecting a client to a RocketMQ instance of v5.x.
Preparing the Environment
You can connect open-source Java clients to DMS for RocketMQ. The recommended Java client version is 5.1.4.
- Using Maven
<dependency> <groupId>org.apache.rocketmq</groupId> <artifactId>rocketmq-client</artifactId> <version>5.1.4</version> </dependency>
- Downloading the dependency.
Sending Transactional Messages
Refer to the following sample code or obtain more sample code from TransactionProducer.java.
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.MessageExt; import org.apache.rocketmq.remoting.common.RemotingHelper; import java.io.UnsupportedEncodingException; public class Main { public static void main(String[] args) throws MQClientException, UnsupportedEncodingException { TransactionListener transactionListener = new TransactionListener() { @Override public LocalTransactionState executeLocalTransaction(Message message, Object o) { System.out.println("Start to execute the local transaction: "+ message); return LocalTransactionState.COMMIT_MESSAGE; } @Override public LocalTransactionState checkLocalTransaction(MessageExt messageExt) { System.out.println("Check request received. Check the transaction status: "+ messageExt); return LocalTransactionState.COMMIT_MESSAGE; } }; TransactionMQProducer producer = new TransactionMQProducer("please_rename_unique_group_name"); // Enter the address. producer.setNamesrvAddr("192.168.0.1:8100"); //producer.setUseTLS(true); // Add this line if SSL has been enabled during instance creation. producer.setTransactionListener(transactionListener); producer.start(); Message msg = new Message("TopicTest", "TagA", "KEY", "Hello RocketMQ ".getBytes(RemotingHelper.DEFAULT_CHARSET)); SendResult sendResult = producer.sendMessageInTransaction(msg, null); System.out.printf("%s%n", sendResult); producer.shutdown(); }}
The producer needs to implement two callback functions. The executeLocalTransaction callback function is called after the half message is sent (see step 3 in the diagram). The checkLocalTransaction callback function is called after the check request is received (see step 6 in the diagram). The two callback functions can return three transaction states:
- LocalTransactionState.COMMIT_MESSAGE: Transaction committed. The consumer can retrieve the message.
- LocalTransactionState.ROLLBACK_MESSAGE: Transaction rolled back. The message will be discarded and cannot be retrieved.
- LocalTransactionState.UNKNOW: The status cannot be determined and the server is expected to check the message status from the producer again.
Subscribing to Transactional Messages
The code for subscribing to transactional messages is the same as that for subscribing to normal messages.
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot