There are three types of Message Queuing currently in vogue
1. RabbitMQ
2. ActiveMQ
3. Kafka
These three kinds are very powerful, RABBITMQ now use more, also more popular, Ali also in use
ACTIVEMQ is an Apache production, but the performance and RMQ compared to a relatively poor
Kafka, the use of different scenarios, not much introduction, mainly for the log collection, combined with Hadoop very flexible
RABBITMQ Official website: http://www.rabbitmq.com/
Install not much said, you can download the version of Windows, or the Linux version download page: http://www.rabbitmq.com/download.html
I installed on the Linux virtual machine, the installation procedure is simple, the RPM directly installs the line, the step is slightly
After successful, you can see the following page:
Simple answer queue diagram
The Pom side needs to introduce the following jar package
1 <Dependencies>2 3 <Dependency>4 <groupId>Com.rabbitmq</groupId>5 <Artifactid>Amqp-client</Artifactid>6 <version>3.4.1</version>7 </Dependency>8 9 <Dependency>Ten <groupId>Org.slf4j</groupId> One <Artifactid>Slf4j-log4j12</Artifactid> A <version>1.7.7</version> - </Dependency> - the <Dependency> - <groupId>Org.apache.commons</groupId> - <Artifactid>Commons-lang3</Artifactid> - <version>3.3.2</version> + </Dependency> - + <Dependency> A <groupId>Org.springframework.amqp</groupId> at <Artifactid>Spring-rabbit</Artifactid> - <version>1.5.6.RELEASE</version> - </Dependency> - - </Dependencies>
Define a class that resembles a connection pool
Public classConnectionutil { Public StaticConnection getconnection ()throwsException {//Defining a connection factoryConnectionFactory factory =NewConnectionFactory (); //Set up service addressFactory.sethost ("192.168.1.205"); //PortFactory.setport (5672); //Set account information, user name, password, vhostFactory.setvirtualhost ("Lee-shop"); Factory.setusername ("Lee"); Factory.setpassword ("Lee"); //getting connections through engineeringConnection Connection =factory.newconnection (); returnconnection; }}
Create a producer
1 Public classSend {2 3 Private Final StaticString queue_name = "Test_queue";4 5 Public Static voidMain (string[] argv)throwsException {6 //get to the connection and MQ channel7Connection Connection =connectionutil.getconnection ();8 //Create a channel from a connection9Channel Channel =Connection.createchannel ();Ten One //declaring (creating) a queue AChannel.queuedeclare (Queue_name,false,false,false,NULL); - - //Message Content theString message = "Hello world!"; -Channel.basicpublish ("", Queue_name,NULL, Message.getbytes ()); -System.out.println ("[X] Sent '" + Message + "'"); - + //close channels and connections - channel.close (); + connection.close (); A } at}
Create consumer
1 Public classRECV {2 3 Private Final StaticString queue_name = "Test_queue";4 5 Public Static voidMain (string[] argv)throwsException {6 7 //get to the connection and MQ channel8Connection Connection =connectionutil.getconnection ();9Channel Channel =Connection.createchannel ();Ten One //declaring queues AChannel.queuedeclare (Queue_name,false,false,false,NULL); - - //consumers who define queues theQueueingconsumer consumer =NewQueueingconsumer (channel); - //Listening Queue -Channel.basicconsume (Queue_name,true, consumer); - + //Get Message - while(true) { +Queueingconsumer.delivery Delivery =consumer.nextdelivery (); AString message =NewString (Delivery.getbody ()); atSystem.out.println ("[X] Received '" + Message + "'"); - } - } -}
When you debug, you can go to the RMQ Admin page to see the number of connections, channels, and message queues:
Message received by the consumer:
The English documents of the corresponding official website are as follows:
Http://www.rabbitmq.com/getstarted.html
RabbitMQ one or two things-simple queue use