Rabbitmq Series II: Quick Start helloworld

Source: Internet
Author: User
Tags rabbitmq
1. Related Concepts

Rabbitmq is a message proxy. In fact, it receives the messages generated by the producer and then delivers the messages to the consumer. In this process, it can be routed, buffered, or more configured rules to persist messages. Rabbitmq and message transmission generally use some terms:

  • Producer (Producing ):

It means the end of the message. If a program sends a message, it is calledProducerIn uppercase.

  • Queue (Queue ):

It is equivalent to the name of the mailbox, which is active on the rabbitmq server. Although the message stream will pass through rabbitmq and your application, it will only be stored in the queue. A queue is unrestricted. It can store as many messages as possible (essentially, it is an infinite buffer ). Multiple producers can send messages to the same message queue, or multiple consumers can receive messages from one message queue at the same time. A Message Queue can be a model.

  • Consumer (Consuming ):

That is, the receiving end. The consumer is mainly a program waiting to receive messages, expressed as follows:

Note: In most application scenarios, the producer, consumer, and rabbitmq services do not run on one machine at the same time.

The following two Java programs are implemented: one producer that sends only one message from the producer, and the other consumer that receives and prints the message.

In the following dialog, "P" is our producer, "C" is our consumer, and the rectangle in the middle is queue (Message Buffer maintained by babbitmq)

2. Java client package

Rabbitmq complies with the amqp protocol, which is an open and common message protocol. Clients of the amqp Protocol are implemented in multiple languages. This article uses the Java client provided by rabbitmq.

You can download the corresponding jar package to your work directory at http://www.rabbitmq.com/java-client.html. Java clients of rabbitmq are also available in the maven library. The groupid is com. rabbitmq, And the artifactid is amqp-client.

3. Sending end

We name the message sender send and the message receiver Recv. The sender connects to rabbitmq, sends a message, and then exits.

Import COM. rabbitmq. client. connectionfactory; import COM. rabbitmq. client. connection; import COM. rabbitmq. client. channel; public class send {private final static string queue_name = "hello"; public static void main (string [] argv) throws Java. io. ioexception {/* connections can be abstracted as socket connections, maintaining protocol version information and Protocol certificates for us. Here we connect to the local Message Server entity (localhost ). To connect to the rabbitmq service on other hosts, you only need to modify the host name or IP address. */connectionfactory factory = new connectionfactory (); factory. sethost ("localhost"); connection = factory. newconnection (); channel = connection. createchannel ();/* Next, create a channel, which is used by most APIs. To send messages, you must declare a message queue and then push messages to the queue */channel. queuedeclare (queue_name, false, null); string message = "Hello world! "; Channel. basicpublish ("", queue_name, null, message. getbytes (); system. out. println ("[x] sent '" + message + "'");/* declares a idempotent Queue (created only when the queue does not exist ). The message context is a byte array. You can specify its encoding. */Channel. Close (); connection. Close ();}}

Note: If the rabbitmq service disk space is insufficient, the Operation will encounter errors, the parameter is set to: disk_free_limit, more parameter configuration can be found here http://www.rabbitmq.com/configure.html#config-items

4. Acceptor

Rabbitmq will push messages to the receiver, which is different from sending only one message. Here we will listen to the messages and print the messages.

Import COM. rabbitmq. client. connectionfactory; import COM. rabbitmq. client. connection; import COM. rabbitmq. client. channel; import COM. rabbitmq. client. queueingconsumer; public class Recv {private final static string queue_name = "hello"; public static void main (string [] argv) throws Java. io. ioexception, Java. lang. interruptedexception {/* how to enable the connection and channel and declare the queue used to receive messages. These steps are basically the same as those of the sender */connectionfactory facto Ry = new connectionfactory (); factory. sethost ("localhost"); connection = factory. newconnection (); channel = connection. createchannel ();/* Ensure that the queue exists */channel. queuedeclare (queue_name, false, null); system. out. println ("[*] waiting for messages. to exit press Ctrl + C ");/* here an additional queueingconsumer class is used to cache the messages to be pushed by the server. We notify the server to push messages to the receiving end,
The server then asynchronously pushes messages to the client. Here, a callback object is provided to cache messages until we are ready to use them.
It. This class is queueingconsumer */queueingconsumer consumer = new queueingconsumer (Channel); channel. basicconsume (queue_name, true, consumer); While (true) {queueingconsumer. delivery delivery = consumer. nextdelivery (); string message = new string (delivery. getbody (); system. out. println ("[x] received'" + message + "'");}}}

After receiving the message, queueingconsumer. nextdelivery () will be blocked and paused until other messages are pushed.

If you need to check and verify the queue, you need to use rabbitmqctl list_queues.

 

Reference: http://www.rabbitmq.com/tutorials/tutorial-one-java.html

Rabbitmq Series II: Quick Start helloworld

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.