Purpose of study RABBITMQ is an implementation of AMQP. We will follow the example on the RABBITMQ Web site to learn about the different Message Queuing patterns supported by AMQP and how the code is implemented. Installing RABBITMQ Server
The Ubuntu RABBITMQ Server is installed with the following
sudo apt-get install rabbitmq-server
sudo service rabbitmq-server start
sudo rabbitmq-plugins enable rabbitmq_ Management Open Management
sudo service rabbitmq-server restart
sudo rabbitmqctl add_user test 123456 Add user test, password is 123456 C6/>sudo rabbitmqctl set_user_tags Test Administrator sets test to the administrator
sudo rabbitmqctl set_permissions-p/ Test ". *" ". *" ". *" Set permissions
We open the http://localhost:15672/can be a RABBITMQ web interface, you can use the newly configured account to login, you can login with guest. The default guest is also an administrator, and we can use it to log on to the Web and create users on the Web. Based on security, we can delete the default guest user.
sudo rabbitmqctl Delete_user Guest
Java client with RABBITMQ
Join in the Pom.xml
<dependency>
<groupId>com.rabbitmq</groupId>
<artifactid>amqp-client</ artifactid>
<version>5.0.0</version>
</dependency>
different queue modes Minimalist mode, similar to FIFO
There are a lot of information on the Internet, we grasp the package to understand the various steps. There are multiple queue patterns between client and server, we first look at the interaction between client and server in the simplest way, and have a rudimentary understanding of some concepts. See [1] for details
Simple mode: The publisher puts the message in the specified queue, and the receiver takes the message away from the specified queue
Publisher
/* "1" and client and server to establish a connection * client-> Server:protocol-header 9-1, to inform the client-supported min version =1, max version =9 * Client <-server:connection Start, gives some information about the server, including supported validation mechanism for Amqplain and plain * client-> server:connection Start-ok, according to the relevant account password, and mechanism (small example for plain), need to note: Do not use the Guest/guest account, will report (Login was refused using authentication Mechanism PLAIN).
We can not use the Guest/guest account to log on the page, so that the guest password concurrency is guest, we could reset the password, or simply use other user account login can. * Client <-server:connection tune, giving tune information such as Channel-max (0), Frame-max (131072), Heartbeat (a) * Client-> Server:connection tune-ok * Client-> server:connection Open * Client <-server:connection OPEN-OK///The default port is 56
71,virtualhost is/, we can also use Factory.seturi ("Amqp://username:password@hostname:portnumber/virtualhost") to set the one-time.
ConnectionFactory factory = new ConnectionFactory ();
Factory.sethost ("191.8.1.107");
Factory.setusername ("test");
Factory.setpassword ("123456");
Connection Connection = Factory.newconnection (); "2" creates channel. In a connection, you can have more than one ChanneL
From a programmatic point of view, we can set some of its properties for each channel so that it does not need to be set for each send or receive.
Channel Channel = Connection.createchannel (); "3" Declaration queue: The queue published by the declaration queue to that QUEUE,RABBITMQ server is created by the client, and if it does not exist, the queue is created. A channel can be published to a number of queues, routed according to the queue's name (Routing_key), where private final static String queue_name = "Hello"; here's a queue,queue name for " Hello ", in direct mode, only the recipient Route-id will receive it.
Once received, the message is removed from the queue.
Channel.queuedeclare (Queue_name, False, False, false, NULL);
"4" post message, when Basicpublish first parameter exchange is "", the simple mode does not set this parameter, the second parameter is Routekey, indicating that it will be placed in the queue hello.
String message = "Hello world!";
Channel.basicpublish ("", queue_name, NULL, message.getbytes ());
... channel.close (); Connection.close ();
Deliver
A small example of the receiver's code is as follows
ConnectionFactory factory = new ConnectionFactory ();
Factory.sethost ("191.8.1.107");
Factory.setusername ("test");
Factory.setpassword ("123456");
Connection Connection = Factory.newconnection ();
Channel Channel = Connection.createchannel ();
There is still a need for declare, and if Recv is started before send, and there is no "hello" queue on the server, the error
Channel.queuedeclare (Queue_name, False, False, False , null);
Consumer Consumer = new Defaultconsumer (channel) {
//Handledelivery is an asynchronous trigger
@Override public
Void Handledelivery (String Consumertag, Envelope Envelope, basicproperties Properties, byte[] body)
throws IOException {
String message = new String (Body, "UTF-8");
Log.info ("[X] Received '" + Message + "'");
}
;
Channel.basicconsume (Queue_name, true, consumer); Start a non-nolocal, non-exclusive consumer
... Similarly, the end should be closed
channel.close ();
Connection.close ();
RELATED links: My professional Java for WEB applications related articles