tencent cloud

Java SDK
Last updated: 2026-01-05 11:20:03
Java SDK
Last updated: 2026-01-05 11:20:03

Scenarios

This document introduces how to use an open-source SDK (taking the Java SDK as an example) to send and receive messages, so as to help you better understand the complete process of sending and receiving messages.

Prerequisites

You have obtained the related client connection parameters as instructed in SDK Overview. If a Managed Edition cluster is used, the trace plugin needs to be enabled for the vhost.

Operation Steps

Step 1: Installing the Java Dependency Library

Add the following dependencies to pom.xml:
<!-- in your <dependencies> block -->
<dependency>
<groupId>com.rabbitmq</groupId>
<artifactId>amqp-client</artifactId>
<version>5.17.1</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.30</version> <!-- Use the latest version. -->
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version> <!-- Use the latest version. -->
</dependency>

Step 2: Producing Messages

1. Create, compile, and run MessageProducer.java.
import com.rabbitmq.client.Channel; import com.rabbitmq.client.Connection; import com.rabbitmq.client.ConnectionFactory; /** * rabbitmq Hello World model example. * Message producer. */ public class MessageProducer { /** * Message queue name. */ public static final String QUEUE_NAME = "hello-world-queue"; public static void main(String[] args) throws Exception { // Connection factory. ConnectionFactory factory = new ConnectionFactory(); // Set the service address. factory.setUri("amqp://*****"); // Set the virtual hosts. factory.setVirtualHost("VHOST-NAME"); // Set the username. factory.setUsername("USERNAME"); // Set the password. factory.setPassword("PASSWORD"); Connection connection = null; Channel channel = null; try { // Obtain the connection. connection = factory.newConnection(); // Establish a channel. channel = connection.createChannel(); // Bind a message queue. channel.queueDeclare(QUEUE_NAME, true, false, false, null); String message = "Hello World!"; // Publish a message (the exchange type is not required to be specified in the Hello World message model). channel.basicPublish("", QUEUE_NAME, null, message.getBytes()); System.out.println(" [Producer(hello world)] Sent '" + message + "'"); } catch (Exception e) { e.printStackTrace(); } finally { // Release resources. if (channel != null) { channel.close(); } if (connection != null) { connection.close(); } } } }
Parameter
Description
QUEUE_NAME
Queue name, which can be obtained from the queue list in the console.
factory.setUri
Cluster access address, which can be obtained from the Client Access module on the basic cluster information page.

factory.setVirtualHost
Vhost name, which can be obtained from the vhost list in the console.
factory.setUsername
Username. Enter the username created in the console.
factory.setPassword
User password. Enter the password specified during user creation in the console.
2. You can select the specified vhost and queue on the Query Message page to check whether the message is sent successfully.


Step 3: Consuming Messages

Create, compile, and run MessageConsumer.java.
import com.rabbitmq.client.Channel; import com.rabbitmq.client.Connection; import com.rabbitmq.client.ConnectionFactory; import com.rabbitmq.client.DeliverCallback; import java.nio.charset.StandardCharsets; /** * rabbitmq Hello World model example. * Message consumer */ public class MessageConsumer { /** * Message queue name. */ public static final String QUEUE_NAME = "hello-world-queue"; public static void main(String[] args) throws Exception { // Connection factory. ConnectionFactory factory = new ConnectionFactory(); // Set the service address. factory.setUri("amqp://*****"); // Set the virtual hosts. factory.setVirtualHost("VHOST-NAME"); // Set the username. factory.setUsername("USERNAME"); // Set the password. factory.setPassword("PASSWORD"); // Obtain the connection and establish a channel. Connection connection = factory.newConnection(); Channel channel = connection.createChannel(); // Bind a message queue. channel.queueDeclare(QUEUE_NAME, true, false, false, null); System.out.println(" [Consumer(hello world)] Waiting for messages."); // Message processing callback. The message processing logic is executed when a message is received. DeliverCallback deliverCallback = (consumerTag, delivery) -> { String message = new String(delivery.getBody(), StandardCharsets.UTF_8); System.out.println(" [Consumer(hello world)] Received '" + message + "'"); }; // Subscribe to messages (If the second parameter is true, it indicates that the message is automatically acknowledged upon being received.). channel.basicConsume(QUEUE_NAME, true, deliverCallback, consumerTag -> { System.out.println("consumerTag = " + consumerTag); }); } }
Parameter
Description
QUEUE_NAME
Queue name, which can be obtained from the queue list in the console.
factory.setUri
Cluster access address, which can be obtained from the Client Access module on the basic cluster information page.
factory.setVirtualHost
Vhost name, which can be obtained from the vhost list in the console.
factory.setUsername
Username. Enter the username created in the console.
factory.setPassword
User password. Enter the password specified during user creation in the console.

Step 4: Viewing Consumers

Log in to the console and choose Cluster > Queue to go to the basic information page to view the details of consumers accessing the cluster.

Note:
Above is a simple example based on the publish/subscribe model of RabbitMQ. For other examples, see Demo or the RabbitMQ official website instances.
Was this page helpful?
You can also Contact Sales or Submit a Ticket for help.
Yes
No

Feedback