tencent cloud

TDMQ for RocketMQ

Release Notes and Announcements
Release Notes
Announcements
Product Introduction
Introduction and Selection of the TDMQ Product Series
What Is TDMQ for RocketMQ
Strengths
Scenarios
Product Series
Comparison with Open-Source RocketMQ
High Availability
Quotas and Limits
Supported Regions
Basic Concepts
Billing
Billing Overview
Pricing
Billing Examples
Pay-as-you-go Switch to Monthly Subscription (5.x)
Renewal
Viewing Consumption Details
Refund
Overdue Payments
Getting Started
Getting Started Guide
Preparations
Step 1: Creating TDMQ for RocketMQ Resources
Step 2: Using the SDK to Send and Receive Messages (Recommended)
Step 2: Running the TDMQ for RocketMQ Client (Optional)
Step 3: Querying Messages
Step 4: Deleting Resources
User Guide
Usage Process Guide
Configuring Account Permissions
Creating the Cluster
Configuring the Namespace
Configuring the Topic
Configuring the Group
Connecting to the Cluster
Managing Messages
Managing the Cluster
Viewing Monitoring Data and Configuring Alarms
Cross-Cluster Message Replication
Use Cases
Naming Conventions for Common Concepts of TDMQ for RocketMQ
RocketMQ Client Use Cases
RocketMQ Performance Load Testing and Capacity Assessment
Access over HTTP
Client Risk Descriptions and Update Guide
Migration Guide for TencentCloud API Operations Related to RocketMQ 4.x Cluster Roles
Migration Guide
Disruptive Migration
Seamless Migration
Developer Guide
Message Types
Message Filtering
Message Retries
POP Consumption Mode (5.x)
Clustering Consumption and Broadcasting Consumption
Subscription Relationship Consistency
Traffic Throttling
​​API Reference(5.x)
History
API Category
Making API Requests
Topic APIs
Consumer Group APIs
Message APIs
Role Authentication APIs
Hitless Migration APIs
Cloud Migration APIs
Cluster APIs
Data Types
Error Codes
​​API Reference(4.x)
SDK Reference
SDK Overview
5.x SDK
4.x SDK
Security and Compliance
Permission Management
CloudAudit
Deletion Protection
FAQs
4.x Instance FAQs
Agreements
TDMQ for RocketMQ Service Level Agreement
Contact Us
DocumentationTDMQ for RocketMQSDK Reference4.x SDKSDK for JavaSending and Receiving Transactional Messages

Sending and Receiving Transactional Messages

PDF
Focus Mode
Font Size
Last updated: 2026-01-23 17:52:24

Scenarios

This document uses the Java SDK as an example to describe how to send and receive transactional messages through an open-source software development kit (SDK).

Prerequisites

You have obtained the client connection parameters as instructed in SDK Overview.

Operation Steps

Step 1: Installing the Java Dependency Library

Introduce related dependencies into the Java project. Taking the Maven project as an example, add the following dependencies to pom.xml:
Note:
The dependency version should be 4.9.4 or later, and 4.9.5 is recommended.
<!-- in your <dependencies> block -->
<dependency>
<groupId>org.apache.rocketmq</groupId>
<artifactId>rocketmq-client</artifactId>
<version>4.9.4</version>
</dependency>
<dependency>
<groupId>org.apache.rocketmq</groupId>
<artifactId>rocketmq-acl</artifactId>
<version>4.9.4</version>
</dependency>

Step 2: Producing Messages

Implementing TransactionListener

public class TransactionListenerImpl implements TransactionListener {

//After half messages are sent successfully, call back this method to execute the local transaction.
@Override
public LocalTransactionState executeLocalTransaction(Message msg, Object arg) {
//Execute the database transaction here. If it is successful, success is returned; otherwise, unknown is returned, or a rollback is performed, waiting for recheck.
return LocalTransactionState.UNKNOW;
}
//Recheck the local transaction.
@Override
public LocalTransactionState checkLocalTransaction(MessageExt msg) {
//Query the data status of the local database here and then determine whether to submit the local database data status.
return LocalTransactionState.COMMIT_MESSAGE;
}

}

Creating a Message Producer

//The user is required to implement one TransactionListener instance.
TransactionListener transactionListener = new TransactionListenerImpl();
// Instantiate the transactional message producer.
ProducerTransactionMQProducer producer = new TransactionMQProducer("transaction_group",
// Access control list (ACL) permissions.
new AclClientRPCHook(new SessionCredentials(ClientCreater.ACCESS_KEY, ClientCreater.SECRET_KEY)));
// Set the NameServer address.
producer.setNamesrvAddr(ClientCreater.NAMESERVER);
producer.setTransactionListener(transactionListener);
producer.start();
Note:
You can log in to the TDMQ for RocketMQ console to obtain the following parameters.
Parameter
Description
groupName
Producer group name. It is recommended that the topic name be used.
accessKey
Role token. You can copy the token from the AccessKey column on the Cluster Permissions page in the console.
secretKey
Role name. You can copy the role name from the SecretKey column on the Cluster Permissions page in the console.
nameserver
Cluster access address. You can obtain the access address from the Access Information module on the cluster basic information page in the console.

Sending Messages

for (int i = 0; i < 3; i++) {
// Message construction example.
Message msg = new Message(TOPIC_NAME, "your tag", "KEY" + i,("Hello RocketMQ " + i).getBytes(StandardCharsets.UTF_8));
SendResult sendResult = producer.sendMessageInTransaction(msg,null);
System.out.printf("%s%n", sendResult);
}

Step 3: Consuming Messages

Creating a Consumer

TDMQ for RocketMQ supports two consumption modes: push and pull. The push mode is recommended.
// Instantiate a consumer.
DefaultMQPushConsumer pushConsumer = new DefaultMQPushConsumer(
groupName,
new AclClientRPCHook(new SessionCredentials(accessKey, secretKey))); // ACL permissions.
// Set the NameServer address.
pushConsumer.setNamesrvAddr(nameserver);
pushConsumer.registerMessageListener((MessageListenerConcurrently) (msgs, context) -> {
// Message processing logic.
System.out.printf("%s Receive transaction messages: %s %n", Thread.currentThread().getName(), msgs);
// Mark the message as successfully consumed.
return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
});
Note:
You can log in to the TDMQ for RocketMQ console to obtain the following parameters.
Parameter
Description
groupName
Group name. You can copy the name from the Group Management page in the console.
4.x virtual cluster/exclusive cluster: Concatenate the namespace name in the format of full namespace name%group name, such as MQ_INSTxxx_aaa%GroupTest.
4.x general cluster/5.x cluster: The namespace name does not need to be concatenated. Enter the group name.
nameserver
Cluster access address. You can obtain the access address from the Access Information module on the cluster basic information page in the console.
secretKey
Role name. You can copy the role name from the SecretKey column on the Cluster Permissions page in the console.
accessKey
Role token. You can copy the token from the AccessKey column on the Cluster Permissions page in the console.

Subscribing to Messages

The subscription method varies depending on the consumption mode.
// Subscribe to a topic.
pushConsumer.subscribe(topic_name, "*");
// Register a callback implementation class to handle messages pulled from the broker.
pushConsumer.registerMessageListener((MessageListenerConcurrently) (msgs, context) -> {
// Message processing logic.
System.out.printf("%s Receive New Messages: %s %n", Thread.currentThread().getName(), msgs);
// Mark the message as successfully consumed and return an appropriate status.
return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
});
// Start the consumer instance.
pushConsumer.start();

Step 4: Viewing Consumption Details

Log in to the TDMQ for RocketMQ console, choose Cluster Management > Group to view the list of clients connected to the group. Click View Details in the operation column to view consumer details.
Note:
The above is a brief introduction to message publishing and subscription. For details, see Demo or TDMQ for RocketMQ Official Documentation.


Help and Support

Was this page helpful?

Help us improve! Rate your documentation experience in 5 mins.

Feedback