tencent cloud

Cluster usage for 5.x Send and receive ordinary messages with 5.x SDK
Last updated: 2025-07-24 10:23:02
Cluster usage for 5.x Send and receive ordinary messages with 5.x SDK
Last updated: 2025-07-24 10:23:02

Operation Scenarios

TDMQ for RocketMQ supports multiple language SDKs to send and receive different types of messages. This document uses Java SDK calls as an example to introduce the operation process of sending and receiving ordinary messages by connecting to TDMQ for RocketMQ server via the 5.x SDK.

Prerequisites

Steps

Step 1: Install the Java dependency

Incorporate the relevant dependencies in the Java project. Take a Maven project as an example, add the following dependency to pom.xml:
<dependencies>
<dependency>
<groupId>org.apache.rocketmq</groupId>
<artifactId>rocketmq-client-java</artifactId>
<version>5.0.5</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.7</version>
</dependency>
</dependencies>

Step 2: Producing Messages

In the created Java project, create a normal message sending program and run it.
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import org.apache.rocketmq.client.apis.ClientConfiguration;
import org.apache.rocketmq.client.apis.ClientException;
import org.apache.rocketmq.client.apis.ClientServiceProvider;
import org.apache.rocketmq.client.apis.SessionCredentialsProvider;
import org.apache.rocketmq.client.apis.StaticSessionCredentialsProvider;
import org.apache.rocketmq.client.apis.message.Message;
import org.apache.rocketmq.client.apis.producer.Producer;
import org.apache.rocketmq.client.apis.producer.SendReceipt;
import org.apache.rocketmq.client.java.example.AsyncProducerExample;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

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 configuration ak and sk
String accessKey = "yourAccessKey"; //ak
String secretKey = "yourSecretKey"; //sk
SessionCredentialsProvider sessionCredentialsProvider =
new StaticSessionCredentialsProvider(accessKey, secretKey);

// Fill in the access address provided by Tencent Cloud
String endpoints = "rmq-xxx.rocketmq.xxxtencenttdmq.com:8081";
ClientConfiguration clientConfiguration = ClientConfiguration.newBuilder()
.setEndpoints(endpoints)
.enableSsl(false)
.setCredentialProvider(sessionCredentialsProvider)
.build();
String topic = "yourNormalTopic";
// Generally, no need to create too many producers in a client.
final Producer producer = provider.newProducerBuilder()
.setClientConfiguration(clientConfiguration)
// Set the topic name. This setting is optional, but recommended so that the producer can capture message routing in advance before sending messages.
.setTopics(topic)
// If the producer is uninitialized, it may report an M {@link ClientException} error.
.build();
// Define the message body here.
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)
// Secondary category of messages under a topic to differ messages within the same topic.
.setTag(tag)
// Message key, another means to differentiate messages 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);
}
// After sending is complete, close the producer client if not needed.
producer.close();
}
}
Parameter
Description
accessKey
Role key, copy from the AccessKey column on the cluster permissions page in the console.

secretKey
Role name, copy from the SecretKey column on the cluster permissions page in the console.
endpoints
Obtain the cluster access address from the access information module on the console cluster basic information page.

topic
Copy the Topic name from the Topic management page in the console.


Step 3: Consuming Messages

In the created Java project, create a subscription normal message program and run it.
TDMQ RocketMQ 5.x series supports two consumption modes: Push Consumer and Simple Consumer. The following example code uses Push Consumer.
import java.io.IOException;
import java.util.Collections;
import org.apache.rocketmq.client.apis.ClientConfiguration;
import org.apache.rocketmq.client.apis.ClientException;
import org.apache.rocketmq.client.apis.ClientServiceProvider;
import org.apache.rocketmq.client.apis.SessionCredentialsProvider;
import org.apache.rocketmq.client.apis.StaticSessionCredentialsProvider;
import org.apache.rocketmq.client.apis.consumer.ConsumeResult;
import org.apache.rocketmq.client.apis.consumer.FilterExpression;
import org.apache.rocketmq.client.apis.consumer.FilterExpressionType;
import org.apache.rocketmq.client.apis.consumer.PushConsumer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

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 configuration ak and sk
String accessKey = "yourAccessKey"; //ak
String secretKey = "yourSecretKey"; //sk
SessionCredentialsProvider sessionCredentialsProvider =
new StaticSessionCredentialsProvider(accessKey, secretKey);

// Fill in the access address provided by Tencent Cloud
String endpoints = "rmq-xxx.rocketmq.xxxtencenttdmq.com:8081";
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";
// Generally, no need to create too many consumers in a client.
PushConsumer pushConsumer = provider.newPushConsumerBuilder()
.setClientConfiguration(clientConfiguration)
// Set consumer group name.
.setConsumerGroup(consumerGroup)
// Set consumer subscription name
.setSubscriptionExpressions(Collections.singletonMap(topic, filterExpression))
.setMessageListener(messageView -> {
Process the message and return the consumption result.
log.info("Consume message={}", messageView);
return ConsumeResult.SUCCESS;
})
.build();
// No need to block main thread in production environment.
Thread.sleep(Long.MAX_VALUE);
// After consumption is completed, close the consumer client if not needed.
pushConsumer.close();
}
}
Parameter
Description
accessKey
Role key, copy from the AccessKey column on the cluster permissions page in the console.

secretKey
Role name, copy from the SecretKey column on the cluster permissions page in the console.
endpoints
Obtain the cluster access address from the access information module on the console cluster basic information page.

consumerGroup
Copy the consumer group name from the Group Management page in the console.

topic
Copy the Topic name from the Topic management page in the console.


Step 4: Viewing Message Details

After sending the completion message, you will get a message ID. You can query the just-sent message, as well as its details and path, on the console in Message Query > Comprehensive Query.


Was this page helpful?
You can also Contact Sales or Submit a Ticket for help.
Yes
No

Feedback