RabbitMQ instance Tutorial: Java Implementation of Hello RabbitMQ World

Source: Internet
Author: User

RabbitMQ instance Tutorial: Java Implementation of Hello RabbitMQ World

RabbitMQ is actually very easy to implement Hello World. Only one server is needed to send messages, and another client can receive messages.

The overall design process is as follows:

The message producer sends Hello messages to the message queue, and the message consumer receives messages from the queue.

Download the dependent Jar package

RabbitMQ must use the Java client library to send messages in Java. The latest version of RabbizMQ's Java client library is 3.5.5. You can download it from the Maven repository or go to the official website.

<Dependency>
<GroupId> com. rabbitmq </groupId>
<ArtifactId> amqp-client </artifactId>
<Version> 3.5.5 </version>
</Dependency>

Create a sender using Java

Package com. favccxx. favrabbit;
 
Import java. io. IOException;
Import java. util. concurrent. TimeoutException;
 
Import com. rabbitmq. client. Channel;
Import com. rabbitmq. client. Connection;
Import com. rabbitmq. client. ConnectionFactory;
 
Public class Sender {
 
Private final static String QUEUE_NAME = "hello ";
 
Public static void main (String [] argv) throws IOException, TimeoutException {
ConnectionFactory factory = new ConnectionFactory ();
Factory. setHost ("localhost ");
 
Connection connection = factory. newConnection ();
Channel channel = connection. createChannel ();
Channel. queueDeclare (QUEUE_NAME, false, null );
String message = "Hello RabbitMQ World! ";
Channel. basicPublish ("", QUEUE_NAME, null, message. getBytes ());
System. out. println ("[x] Sent '" + message + "'");
 
}
}

Monitoring message queue on the RabbitMQ Console

Run the above Code and you can see the message you just sent from the RabbitMQ console.

Receive messages using Java

Package com. favccxx. favrabbit;
 
Import java. io. IOException;
Import java. util. concurrent. TimeoutException;
 
Import com. rabbitmq. client. AMQP;
Import com. rabbitmq. client. Channel;
Import com. rabbitmq. client. Connection;
Import com. rabbitmq. client. ConnectionFactory;
Import com. rabbitmq. client. Consumer;
Import com. rabbitmq. client. DefaultConsumer;
Import com. rabbitmq. client. Envelope;
 
Public class extends er {
 
Private final static String QUEUE_NAME = "hello ";
 
Public static void main (String [] argv)
Throws java. io. IOException, java. lang. InterruptedException, TimeoutException {
 
ConnectionFactory factory = new ConnectionFactory ();
Factory. setHost ("localhost ");
Connection connection = factory. newConnection ();
Channel channel = connection. createChannel ();
 
Channel. queueDeclare (QUEUE_NAME, 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 );
}
}

Run the message senders and message recipients respectively. The real-time status is displayed on the RabbitMQ console.

So far, RabbitMQ's Hello World work has ended. Do you have a good impression on message queues?

Install RabbitMQ in CentOS 5.6

Detailed installation record of RabbitMQ client C ++

Try RabbitMQ in Python

Deployment of production instances in the RabbitMQ Cluster Environment

Use PHP + RabbitMQ in Ubuntu

Process of installing RabbitMQ on CentOS

RabbitMQ concept and Environment Construction

RabbitMQ getting started

RabbitMQ details: click here
RabbitMQ: click here

This article permanently updates the link address:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.