tencent cloud

Using the Java SDK
Last updated:2026-01-23 17:52:23
Using the Java SDK
Last updated: 2026-01-23 17:52:23

Scenarios

TDMQ for RocketMQ supports software development kits (SDKs) in multiple languages for sending and receiving different types of messages. This document describes how to use the 5.x SDK (taking the Java SDK as an example) to connect to the TDMQ for RocketMQ server for normal message sending and receiving.

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:
<dependencies>
<dependency>
<groupId>org.apache.rocketmq</groupId>
<artifactId>rocketmq-client-java</artifactId>
<version>5.0.6</version>
</dependency>
</dependencies>
Note:
If you are using it in a Spring Boot environment, you may encounter annotations-api dependency conflicts. In such cases, you can add dependencies to exclude the conflict.
<dependencies>
<dependency>
<groupId>org.apache.rocketmq</groupId>
<artifactId>rocketmq-client-java</artifactId>
<version>5.0.6</version>
<exclusions>
<exclusion>
<groupId>org.apache.tomcat</groupId>
<artifactId>annotations-api</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>

Step 2: Producing Messages

Create and run a program for sending messages in the created Java project.
public class NormalMessageSyncProducer {
private static final Logger log = LoggerFactory.getLogger(NormalMessageSyncProducer.class);

private NormalMessageSyncProducer() {
}

public static void main(String[] args) throws ClientException, IOException {
final ClientServiceProvider provider = ClientServiceProvider.loadService();

// Add the configured AccessKey and SecretKey.
String accessKey = "yourAccessKey"; // AccessKey, which can be obtained from the Permission Management page in the console.
String secretKey = "yourSecretKey"; // SecretKey, which can be obtained from the Permission Management page in the console.
SessionCredentialsProvider sessionCredentialsProvider =
new StaticSessionCredentialsProvider(accessKey, secretKey);

// Enter the access address provided by Tencent Cloud.
String endpoints = "rmq-xxx.rocketmq.xxxtencenttdmq.com:8080";
ClientConfiguration clientConfiguration = ClientConfiguration.newBuilder()
.setEndpoints(endpoints)
.enableSsl(false)
.setCredentialProvider(sessionCredentialsProvider)
.build();
String topic = "yourNormalTopic";
// In most case, you don't need to create too many producers, singleton pattern is recommended.
final Producer producer = provider.newProducerBuilder()
.setClientConfiguration(clientConfiguration)
// Set the topic name(s), which is optional but recommended. It makes producer could prefetch the topic
// route before message publishing.
.setTopics(topic)
// May throw {@link ClientException} if the producer is not initialized.
.build();
// Define your message body.
byte[] body = "This is a normal message for Apache RocketMQ".getBytes(StandardCharsets.UTF_8);
String tag = "yourMessageTagA";
final Message message = provider.newMessageBuilder()
// Set topic for the current message.
.setTopic(topic)
// Message secondary classifier of message besides topic.
.setTag(tag)
// Key(s) of the message, another way to mark message besides message id.
.setKeys("yourMessageKey-1c151062f96e")
.setBody(body)
.build();
try {
final SendReceipt sendReceipt = producer.send(message);
log.info("Send message successfully, messageId={}", sendReceipt.getMessageId());
} catch (Throwable t) {
log.error("Failed to send message", t);
}
// Close the producer when you don't need it anymore.
producer.close();
}
}
Parameter
Description
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.
endpoints
Cluster access address. You can obtain the access address from the Access Information module on the Cluster Basic Information page in the console.
topic
Topic name. You can copy the name from the Topic Management page in the console.

Step 3: Consuming Messages

Create and run a program to subscribe to normal messages in the created Java project.
TDMQ for RocketMQ 5.x series supports two types of clients: Push Consumer and Simple Consumer. The following sample code uses Push Consumer as an example.
public class NormalPushConsumer {
private static final Logger log = LoggerFactory.getLogger(NormalPushConsumer.class);

private NormalPushConsumer() {
}

public static void main(String[] args) throws ClientException, IOException, InterruptedException {
final ClientServiceProvider provider = ClientServiceProvider.loadService();

// Add the configured AccessKey and SecretKey.
String accessKey = "yourAccessKey"; // AccessKey, which can be obtained from the Permission Management page in the console.
String secretKey = "yourSecretKey"; // SecretKey, which can be obtained from the Permission Management page in the console.
SessionCredentialsProvider sessionCredentialsProvider =
new StaticSessionCredentialsProvider(accessKey, secretKey);

// Enter the access address provided by Tencent Cloud.
String endpoints = "rmq-xxx.rocketmq.xxxtencenttdmq.com:8080";
ClientConfiguration clientConfiguration = ClientConfiguration.newBuilder()
.setEndpoints(endpoints)
.enableSsl(false)
.setCredentialProvider(sessionCredentialsProvider)
.build();
String tag = "*";
FilterExpression filterExpression = new FilterExpression(tag, FilterExpressionType.TAG);
String consumerGroup = "yourConsumerGroup";
String topic = "yourTopic";
// In most case, you don't need to create too many consumers, singleton pattern is recommended.
PushConsumer pushConsumer = provider.newPushConsumerBuilder()
.setClientConfiguration(clientConfiguration)
// Set the consumer group name.
.setConsumerGroup(consumerGroup)
// Set the subscription for the consumer.
.setSubscriptionExpressions(Collections.singletonMap(topic, filterExpression))
.setMessageListener(messageView -> {
// Handle the received message and return consume result.
log.info("Consume message={}", messageView);
return ConsumeResult.SUCCESS;
})
.build();
// Block the main thread, no need for production environment.
Thread.sleep(Long.MAX_VALUE);
// Close the push consumer when you don't need it anymore.
pushConsumer.close();
}
}
Parameter
Description
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.
endpoints
Cluster access address. You can obtain the access address from the Access Information module on the Cluster Basic Information page in the console.
consumerGroup
Consumer group name. You can copy the name from the Group Management page in the console.
topic
Topic name. You can copy the name from the Topic Management page in the console.

Step 4: Viewing Message Details

After a message is sent, you will receive a message ID (messageID). You can choose Message Query > General Query in the console to query the recently sent message, including the message details and trace.
Was this page helpful?
You can also Contact Sales or Submit a Ticket for help.
Yes
No

Feedback