"Go" RabbitMQ Getting Started Helloworld

Source: Internet
Author: User
Tags 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. Under cmd: Enter the Sbin directory and 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 common terms for RabbitMQ are:
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:

Before you start writing your code, you need to add the RABBITMQ jar package to classpath. Send side:Send.java connects to RABBITMQ (at which point the service needs to be started), sends a piece of data, and then exits.
 1 package com.zhy.rabbit._01; 2 3 Import Com.rabbitmq.client.Channel; 4 Import com.rabbitmq.client.Connection; 5 Import Com.rabbitmq.client.ConnectionFactory; 6 7 public class Send 8 {9//queue name All private final static String queue_name = "Hello"; all public static Voi         D Main (string[] argv) throws Java.io.IOException13 {14/**15 * Create connection connect to MabbitMQ16 */17 ConnectionFactory factory = new ConnectionFactory (); 18//Set MABBITMQ host IP or host name Factory.sethost ("Localh OST "); 20//Create a connection Connection Connection = Factory.newconnection (); 22//Create a channel Channel = Connection.createchannel (); 24//Specifies a queue of Channel.queuedeclare (Queue_name, False, False, FALSE, n ULL); 26//message sent with String message = "Hello world!"; 28//Send a message to the queue Channel.basicpublish ("", queue_name, NULL, message.getbytes ()); System.out.pri   Ntln ("[X] Sent '" + Message + "'"); 31      Close the channel and connect the Channel.close (); Connection.close (); 34}35} 

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.

 1 package com.zhy.rabbit._01; 2 3 Import Com.rabbitmq.client.Channel; 4 Import com.rabbitmq.client.Connection; 5 Import Com.rabbitmq.client.ConnectionFactory; 6 Import Com.rabbitmq.client.QueueingConsumer; 7 8 public class RECV 9 {10//queue name one private final static String queue_name = "Hello"; public static Voi D Main (string[] argv) throws java.io.ioexception,14 java.lang.InterruptedException15 {16//open connection and create         Channel, as with the sending end, ConnectionFactory factory = new ConnectionFactory (); Factory.sethost ("localhost"); 19 Connection Connection = Factory.newconnection (); Channel channel = Connection.createchannel (); 21//Declaration Queue , primarily to prevent the message receiver from running this program before the queue is still present when the queue is created. Channel.queuedeclare (Queue_name, False, False, false, null), SYSTEM.OUT.PRINTLN ("[*] Waiting for MES Sages. To exit Press CTRL + C "); 24 25//Create queue consumer Queueingconsumer consumer = new Queueingconsumer (channel) ; 27//Specify consumption queue 28         Channel.basicconsume (Queue_name, true, consumer), while (true) {//nextdelivery is a blocking method (an internal implementation is actually a take method that blocks the queue) queueingconsumer.delivery Delivery = Consumer.nextdelivery (); stri         NG message = new String (Delivery.getbody ()), System.out.println ("[X] Received '" + Message + "'"); 35 }36 37}38}

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

Reference article: http://blog.csdn.net/lmj623565791/article/details/37607165

"Go" RabbitMQ Getting Started Helloworld

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.