<!-- 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>
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. |
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. |

Feedback