tencent cloud

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

Operation Scenarios

RocketMQ 5.0 provides a brand-new 5.x SDK based on the gRPC protocol. The new version SDK is more lightweight and offers better multilingual support. We recommend prioritizing its use. Meanwhile, Message Queue RocketMQ 5.x series also supports existing services continuing to use the 4.x SDK for access. This document introduces the operation process of sending and receiving ordinary messages using the open-source SDK, with the example of calling the 4.x Java SDK.

Prerequisites

Resources Creation and Preparation already completed

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:
<!-- in your <dependencies> block -->
<dependency>
<groupId>org.apache.rocketmq</groupId>
<artifactId>rocketmq-client</artifactId>
<version>4.9.7</version>
</dependency>
<dependency>
<groupId>org.apache.rocketmq</groupId>
<artifactId>rocketmq-acl</artifactId>
<version>4.9.7</version>
</dependency>

Step 2: Generate a Message

Create a normal message sending program in the created Java project and run i.
// Instantiate the message producer.
DefaultMQProducer producer = new DefaultMQProducer(
groupName,
new AclClientRPCHook(new SessionCredentials(accessKey, secretKey)) // ACL Permission
);
// Set the NameServer address. The address should be in the form of xxx.tencenttdmq.com:8080.
producer.setNamesrvAddr(nameserver);
// Start the Producer instance.
producer.start();
for (int i = 0; i < 10; i++) {
// Create a message instance and establish the topic and content.
Message msg = new Message(topic_name, ("Hello RocketMQ " + i).getBytes(RemotingHelper.DEFAULT_CHARSET));
// Send the message.
SendResult sendResult = producer.send(msg);
System.out.printf("%s%n", sendResult);
}
Parameter
Description
accessKey
Role key, copied from the AccessKey column on the Cluster Permission page of the console.


secretKey
Role name, copied from the SecretKey column on the Cluster Permission page of the console.
nameserver
Get the cluster access address from the access information module on the console cluster basic information page.

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


Step 3: Consume the message

n the created Java project, create a subscription to regular messages and run the program. The following example code uses Push Consumer. For the rest, see more detailed 4.x operation instructions.
// Instantiate the consumer
DefaultMQPushConsumer pushConsumer = new DefaultMQPushConsumer(
groupName,
new AclClientRPCHook(new SessionCredentials(accessKey, secretKey))); //ACL permissions
// Set the NameServer address.
pushConsumer.setNamesrvAddr(nameserver);
// Subscribe to a topic.
pushConsumer.subscribe(topic_name, "*");
// Register a callback implementation class to process 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 being successfully consumed, and return to the processing status based on the consumption condition.
return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
});
// Start the consumer instance.
pushConsumer.start();
Parameter
Description
accessKey
Role key, copied from the AccessKey column on the Cluster Permission page of the console.

secretKey
Role name, copied from the SecretKey column on the Cluster Permission page of the console.
nameserver
Get the cluster access address from the access information module on the console cluster basic information page.

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

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


Step 4: View 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 under Message Query > Comprehensive Query.


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

Feedback