Accessing a RabbitMQ Instance Without SSL Encryption
RabbitMQ instances are compatible with the open-source RabbitMQ protocol. To access and use a RabbitMQ instance in different languages, see the tutorials at https://www.rabbitmq.com/getstarted.html.
The following demo shows how to access and use a RabbitMQ instance in a VPC, assuming that the RabbitMQ client is deployed in an ECS.
If SSL is enabled for the instance, see Accessing a RabbitMQ Instance with SSL Encryption for how to access the instance.
Prerequisites
- A RabbitMQ instance has been created following the instructions in Buying an Instance, and the username and password used to create the instance have been obtained.
- The Instance Address (Private Network) or Instance Address (Public Network) of the instance has been recorded from the instance details.
- An ECS has been created, and its VPC, subnet, and security group configurations are the same as those of the RabbitMQ instance.
Accessing the Instance in CLI Mode
- Log in to the ECS.
- Install JDK or JRE, and add the following lines to .bash_profile in the home directory to configure the environment variables JAVA_HOME and PATH:
export JAVA_HOME=/opt/java/jdk1.8.0_151 export PATH=$JAVA_HOME/bin:$PATH
Run the source .bash_profile command for the modification to take effect.
Use Oracle JDK instead of ECS's default JDK (for example, OpenJDK), because ECS's default JDK may not be suitable for the sample project. Obtain Oracle JDK 1.8.111 or later from Oracle's official website.
- Run the following command to download RabbitMQ-Tutorial.zip:
$ wget https://dms-demo.obs.cn-north-1.myhuaweicloud.com/RabbitMQ-Tutorial.zip
- Run the following command to decompress RabbitMQ-Tutorial.zip:
$ unzip RabbitMQ-Tutorial.zip
- Run the following command to navigate to the RabbitMQ-Tutorial directory, which contains the precompiled JAR file:
$ cd RabbitMQ-Tutorial
- Create messages using the sample project.
$ java -cp .:rabbitmq-tutorial.jar Send host port user password
host indicates the connection address for accessing the instance. port is the listening port of the instance, which is 5672 by default. user and password indicate the username and password used for accessing the instance.
Figure 1 Sample project for creating messages
Press Ctrl+C to exit.
- Retrieve messages using the sample project.
$ java -cp .:rabbitmq-tutorial.jar Recv host port user password
host indicates the connection address for accessing the instance. port is the listening port of the instance, which is 5672 by default. user and password indicate the username and password used for accessing the instance.
Figure 2 Sample project for retrieving messages
To stop retrieving messages, press Ctrl+C to exit.
Java Sample Code
Accessing a RabbitMQ instance and creating messages
ConnectionFactory factory = new ConnectionFactory(); factory.setHost(host); factory.setPort(port); factory.setUsername(user); factory.setPassword(password); Connection connection = factory.newConnection(); Channel channel = connection.createChannel(); channel.queueDeclare(QUEUE_NAME, false, false, false, null); String message = "Hello World!"; channel.basicPublish("", QUEUE_NAME, null, message.getBytes("UTF-8")); System.out.println(" [x] Sent '" + message + "'"); channel.close(); connection.close();
Accessing a RabbitMQ instance and retrieving messages
ConnectionFactory factory = new ConnectionFactory(); factory.setHost(host); factory.setPort(port); factory.setUsername(user); factory.setPassword(password); Connection connection = factory.newConnection(); Channel channel = connection.createChannel(); channel.queueDeclare(QUEUE_NAME, false, false, false, null); System.out.println(" [*] Waiting for messages. To exit press CTRL+C"); Consumer consumer = new DefaultConsumer(channel) { @Override public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException { String message = new String(body, "UTF-8"); System.out.println(" [x] Received '" + message + "'"); } }; channel.basicConsume(QUEUE_NAME, true, consumer);
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.