RabbitMQ Getting Started Helloworld-excerpt from the Web

Source: Internet
Author: User
Tags rabbitmq

This series of tutorials mainly comes from the translation of the official website introductory course, and then makes some modifications and experiments on its own, the content is for reference only.

"Hello World" of RabbitMQ

1. Installation of RABBITMQ under Windows

Download Erlang, Address: Http://www.erlang.org/download/otp_win32_R15B.exe, double-click to install (first loaded)

Download RABBITMQ, Address: Http://www.rabbitmq.com/releases/rabbitmq-server/v3.3.4/rabbitmq-server-3.3.4.exe, double click to install

When downloading Rabbit-client.jar, Java code needs to be imported. Address: Http://www.rabbitmq.com/releases/rabbitmq-java-client/v3.3.4/rabbitmq-java-client-bin-3.3.4.zip

After the installation is complete, the sbin in the RABBITMQ installation directory will first be: Rabbitmq-server.bat

For example:

Under cmd: Enter Sbin directory, run rabbitmq-server start

2. Introduction

RabbitMQ is the intermediary of information transmission. Essentially, he receives messages from producers (producers) and forwards these messages to the consumer (consumers). In other words, he is able to forward, buffer, and persist messages according to the rules you specify.

Some of the common terms of RabbitMQ: producing means nothing more than a send. A program that sends a message is a producer (producer). Generally used to denote producer:

A queue is similar to a mailbox. dependent on internal RABBITMQ. Although messages are passed through RABBITMQ in your app, they can only be stored in a queue. Queues are unrestricted and can store any number of messages-essentially an unrestricted cache. Many producers can send messages through the same queue, and many of the same consumers can receive messages from the same queue. Generally used to represent queues:

Consuming (consumption) is similar to receiving. Consumer is basically a program waiting to receive messages. General use represents consumer:

Note: producer (producer), Consumer (consumer), Broker (RABBITMQ service) does not need to be deployed on the same machine, and in most real-world applications, it will not be deployed on the same machine.

2. Java Getting started instance a producer sends a message, a recipient receives the message, and prints it out on the console. Such as:

Note: You need to download rabbitmq-java-client-bin-*.zip the jar into the project's classpath on the official website.

Send side: Send.java connects to RABBITMQ (at which point the service needs to be started), sends a piece of data, and then exits.

[Java]View Plaincopyprint?
  1. Package com.zhy.rabbit._01;
  2. Import Com.rabbitmq.client.Channel;
  3. Import com.rabbitmq.client.Connection;
  4. Import Com.rabbitmq.client.ConnectionFactory;
  5. Public class Send
  6. {
  7. //Queue name
  8. Private final static String queue_name = "Hello";
  9. public static void Main (string[] argv) throws java.io.IOException
  10. {
  11. /** 
  12. * Create a connection to connect to MABBITMQ
  13. */
  14. ConnectionFactory factory = new ConnectionFactory ();
  15. //Set MABBITMQ host IP or host name
  16. Factory.sethost ("localhost");
  17. //Create a connection
  18. Connection Connection = Factory.newconnection ();
  19. //Create a channel
  20. Channel channel = Connection.createchannel ();
  21. //Specify a queue
  22. Channel.queuedeclare (Queue_name, false, false, false, null);
  23. //Messages sent
  24. String message = "Hello world!";
  25. //Send a message to the queue
  26. Channel.basicpublish ("", queue_name, null, message.getbytes ());
  27. System.out.println ("[x] Sent '" + Message + "'");
  28. //Turn off channels and connections
  29. Channel.close ();
  30. Connection.close ();
  31. }
  32. }
Package Com.zhy.rabbit._01;import Com.rabbitmq.client.channel;import Com.rabbitmq.client.connection;import Com.rabbitmq.client.connectionfactory;public class send{//queue name private final static String queue_name = "Hello";p ublic static void Main (string[] argv) throws java.io.ioexception{/** * Create connection connection to MABBITMQ */connectionfactory factory = new Connect Ionfactory ();//Set MABBITMQ host IP or host name factory.sethost ("localhost");//Create a connection connection connection = Factory.newconnection ();//create a channel channels channel = Connection.createchannel ();//Specify a queue Channel.queuedeclare (queue_ NAME, False, False, false, null);//The message sent string = "Hello world!"; /Send a message to the queue Channel.basicpublish ("", queue_name, NULL, message.getbytes ()); System.out.println ("[X] Sent '" + Message + "'");//close channel and connect Channel.close (); Connection.close ();}}

It is important to note that the queue is created only when it does not exist, and multiple declarations are not created repeatedly. The content of the information is a byte array, which means that you can pass any data.

Receiving end: Recv.java continuously waits for the server to push the message and then outputs it in the console.

[Java]View Plaincopyprint?
  1. Package com.zhy.rabbit._01;
  2. Import Com.rabbitmq.client.Channel;
  3. Import com.rabbitmq.client.Connection;
  4. Import Com.rabbitmq.client.ConnectionFactory;
  5. Import Com.rabbitmq.client.QueueingConsumer;
  6. Public class Recv
  7. {
  8. //Queue name
  9. Private final static String queue_name = "Hello";
  10. public static void Main (string[] argv) throws Java.io.IOException,
  11. Java.lang.InterruptedException
  12. {
  13. //Open connection and create channel, same as Send end
  14. ConnectionFactory factory = new ConnectionFactory ();
  15. Factory.sethost ("localhost");
  16. Connection Connection = Factory.newconnection ();
  17. Channel channel = Connection.createchannel ();
  18. //Declares the queue, primarily to prevent the message receiver from running this program before the queue does not exist when the queue is created.
  19. Channel.queuedeclare (Queue_name, false, false, false, null);
  20. System.out.println ("[*] waiting for messages.  To exit Press CTRL + C ");
  21. //Create queue consumer
  22. Queueingconsumer consumer = New Queueingconsumer (channel);
  23. //Specify consumption queue
  24. Channel.basicconsume (Queue_name, true, consumer);
  25. While (true)
  26. {
  27. //nextdelivery is a blocking method (the internal implementation is actually the take method of the blocking queue)
  28. Queueingconsumer.delivery Delivery = Consumer.nextdelivery ();
  29. String message = new String (Delivery.getbody ());
  30. System.out.println ("[x] Received '" + Message + "'");
  31. }
  32. }
  33. }
Package Com.zhy.rabbit._01;import Com.rabbitmq.client.channel;import Com.rabbitmq.client.connection;import Com.rabbitmq.client.connectionfactory;import Com.rabbitmq.client.queueingconsumer;public class recv{//queue name private Final static String queue_name = "Hello";p ublic static void Main (string[] argv) throws Java.io.ioexception,java.lang.inte rruptedexception{//Open the connection and create the channel, as with the sender connectionfactory factory = new ConnectionFactory (); Factory.sethost ("localhost" ); Connection Connection = Factory.newconnection (); Channel channel = Connection.createchannel ();//declares a queue, primarily to prevent the message receiver from running this program before the queue does not already exist when the queue is created. Channel.queuedeclare (Queue_name, False, False, false, NULL); SYSTEM.OUT.PRINTLN ("[*] waiting for messages. To exit Press CTRL + C ");//Create queue consumer Queueingconsumer consumer = new Queueingconsumer (channel);// Specify consumption queue Channel.basicconsume (Queue_name, true, consumer); while (true) {// Nextdelivery is a blocking method (the internal implementation is actually the take method of the blocking queue) queueingconsumer.delivery Delivery = Consumer.nextdelivery (); String message = new String (delivery.getbodY ()); System.out.println ("[X] Received '" + Message + "'");}}}

It doesn't matter if you run Send.java and Recv.java separately. The premise RABBITMQ service opens.

Operation Result:

[x] Sent ' Hello world! '

----------------------------------------

[*] Waiting for messages. To exitpress Ctrl + C

[x] Received ' Hello world! '

RabbitMQ Getting Started Helloworld-excerpt from the Web

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.