Getting started with RabbitMQ Helloworld, rabbitmqhelloworld
1. Install RabbitMQ in Windows
Download Erlang, address: http://www.erlang.org/download/otp_win32_R15B.exe, double-click to install (first installed)
Download RabbitMQ, address: http://www.rabbitmq.com/releases/rabbitmq-server/v3.3.4/rabbitmq-server-3.3.4.exe, double-click to install
Download the rabbit-client.jar, which needs to be imported during Java code. 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 installation directory of RabbitMQ will first have: 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 and forwards them to consumers (consumers). In other words, he can forward, buffer, and persist messages according to your specified rules.
Some common terms of RabbitMQ:
Producing means sending. A message sending program is a producer ). Generally, it indicates Producer:
A Queue is similar to a mailbox. Depends on the inside of RabbitMQ. Although messages are transmitted in your application through RabbitMQ, they can only be stored in queue. The queue is unrestricted and can store any number of messages-essentially an unlimited cache. Many producers can send messages through the same queue, while many of the same consumers can receive messages from the same queue. Queue:
Consuming is similar to receiving. Consumer is basically a program waiting to receive messages. Consumer:
Note: producers, consumers, and brokers do not need to be deployed on the same machine. In most practical applications, it will not be deployed on the same machine.
2. Java entry instance
One producer sends a message, and one receiver receives the message and prints it on the console. For example:
Before writing code, add the jar package of rabbitMQ to classpath.
Sender:Send. java connects to RabbitMQ (the service needs to be started at this time), 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 10 private final static String QUEUE_NAME = "hello"; 11 12 public static void main (String [] argv) throws java. io. IOException13 {14/** 15 * create a connection to MabbitMQ16 */17 ConnectionFactory factory = new ConnectionFacto Ry (); 18 // set the ip address of the host where MabbitMQ is located or the host name 19 factory. setHost ("localhost"); 20 // create a Connection 21 connection Connection = factory. newConnection (); 22 // create a Channel 23 channel Channel = connection. createChannel (); 24 // specify a queue of 25 channels. queueDeclare (QUEUE_NAME, false, null); 26 // sent message 27 String message = "hello world! "; 28 // send a message to the queue 29 channel. basicPublish ("", QUEUE_NAME, null, message. getBytes (); 30 System. out. println ("[x] Sent '" + message + "'"); 31 // close the channel and connect to 32 channels. close (); 33 connection. close (); 34} 35}
It is worth noting that the queue will only be created when it does not exist. Multiple declarations will not be created again. The content of the information is a byte array, which means that you can transmit any data.
Acceptor:Recv. java keeps waiting for the server to push messages and then output them on 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 11 private final static String QUEUE_NAME = "hello"; 12 13 public static void main (String [] argv) throws java. io. IOException, 14 java. lang. interruptedException15 {16 // open the connection and create a channel, just like the sender 17 ConnectionFactory factory = new ConnectionFactory (); 18 factory. setHost ("localhost"); 19 Connection connection = factory. newConnection (); 20 Channel channel = connection. createChannel (); 21 // declare the queue, mainly to prevent the message recipient from running this program first, and create a queue when the queue does not exist. 22 channel. queueDeclare (QUEUE_NAME, false, null); 23 System. out. println ("[*] Waiting for messages. to exit press CTRL + C "); 24 25 // create a queue consumer 26 QueueingConsumer consumer = new QueueingConsumer (channel); 27 // specify the consumption queue 28 channel. basicConsume (QUEUE_NAME, true, consumer); 29 while (true) 30 {31 // nextDelivery is a blocking method (the internal implementation is actually the take method of the blocking Queue) 32 QueueingConsumer. delivery delivery = consumer. nextDelivery (); 33 String message = new String (delivery. getBody (); 34 System. out. println ("[x] received'" + message + "'"); 35} 36 37} 38}
It doesn't matter whether the order of sending. java and Recv. java is run separately. Before the RabbitMQ service is enabled.
Running result:
[X] Sent 'Hello world! '
----------------------------------------
[*] Waiting for messages. To exitpress CTRL + C
[X] Received 'Hello world! '
Reference: http://blog.csdn.net/lmj623565791/article/details/37607165