RABBITMQ is a high-performance message queue that supports, or is implemented, the AMQP protocol (Advanced Message Queue Protocol Premium Messaging Queuing protocol).
Here's a quick example. We first need to deploy a good rabbitmq, and then implement a producer-consumer, the producer to Rabbit to publish a message, the consumer to rabbit to take this message, after the correct receipt of this message, the consumer will return to the queue to write back to inform the producer himself received the message.
Deploying rabbit in Windows is simple, installing the Erlang runtime First, and then installing the RABBITMQ installation files, all EXE, is simple. Then find the bat in the Rabbit sbin directory to start the RABBITMQ.
Here is the Producer-consumer code:
PackageCOM.HZFI.RABBITMQ;Importjava.io.IOException;Importjava.util.concurrent.TimeoutException;Importcom.rabbitmq.client.AMQP.BasicProperties;ImportCom.rabbitmq.client.QueueingConsumer.Delivery;Importcom.rabbitmq.client.ShutdownSignalException;ImportCom.rabbitmq.client.Channel;Importcom.rabbitmq.client.Connection;Importcom.rabbitmq.client.ConnectionFactory;Importcom.rabbitmq.client.ConsumerCancelledException;ImportCom.rabbitmq.client.QueueingConsumer; Public classProducer {Private Final StaticString queue_name = "Myqueue";//on-Delivery queue Public Static voidMain (string[] args)throwsIOException, timeoutexception{String replyqueuename=NULL;//return queue nameconnectionfactory connfactory=NULL; Connection Conn=NULL; Channel Channel=NULL; Try{connfactory=NewConnectionFactory (); Connfactory.sethost ("LocalHost"); Conn=connfactory.newconnection (); Channel=Conn.createchannel (); //Return to queueReplyqueuename =Channel.queuedeclare (). Getqueue (); Queueingconsumer Consumer=NewQueueingconsumer (channel); Channel.basicconsume (Replyqueuename,true, consumer); String Corrid= Java.util.UUID.randomUUID (). toString ();//the ID used to indicate the return queue result, uniqueBasicproperties props =NewBasicproperties.builder (). Correlationid (Corrid). ReplyTo (Replyqueuename). build (); String msg= "[Email protected]"; Channel.queuedeclare (Queue_name,false,false,false,NULL); Channel.basicpublish ("", queue_name, props, msg.getbytes ()); System.out.println ("producer has published: \" "+ msg +" \ ""); while(true) {Thread.Sleep (1000); Delivery Delivery=Consumer.nextdelivery (); System.out.println ("From server reply:" +NewString (Delivery.getbody ())); } }Catch(IOException IoE) {ioe.printstacktrace (); }Catch(timeoutexception toe) {toe.printstacktrace (); } Catch(shutdownsignalexception e) {e.printstacktrace (); } Catch(consumercancelledexception e) {e.printstacktrace (); } Catch(interruptedexception e) {e.printstacktrace (); }finally{ if(channel!=NULL) Channel.close (); if(conn!=NULL) Conn.close (); } }}
PackageCOM.HZFI.RABBITMQ;Importjava.io.IOException;Importjava.util.concurrent.TimeoutException;Importcom.rabbitmq.client.AMQP.BasicProperties;ImportCom.rabbitmq.client.Channel;Importcom.rabbitmq.client.Connection;Importcom.rabbitmq.client.ConnectionFactory;ImportCom.rabbitmq.client.QueueingConsumer;ImportCom.rabbitmq.client.QueueingConsumer.Delivery; Public classConsumer {Private Final StaticString queue_name = "Myqueue"; Public Static voidMain (string[] args)throwsIOException, timeoutexception{connectionfactory connfactory=NULL; Connection Conn=NULL; Channel Channel=NULL; Try{connfactory=NewConnectionFactory (); Connfactory.sethost ("LocalHost"); Conn=connfactory.newconnection (); Channel=Conn.createchannel (); Channel.queuedeclare (Queue_name,false,false,false,NULL); System.out.println ("Listening for event message ..."); Queueingconsumer Consumer=NewQueueingconsumer (channel); Channel.basicconsume (Queue_name,true, consumer); while(true) {Thread.Sleep (1000); Delivery Delivery=Consumer.nextdelivery (); Basicproperties Props=delivery.getproperties (); Basicproperties Reply_props=NewBasicproperties.builder (). Correlationid (Props.getcorrelationid ()). build (); String msg=NewString (Delivery.getbody (), "Utf-8"); System.out.println ("Receive msg:" +msg); String retmsg= "OK, give you reply:" +NewString (Msg.getbytes (), "Utf-8"); System.out.println ("Return queue name in consumer" +Props.getreplyto ()); Channel.basicpublish ("", Props.getreplyto (), Reply_props, Retmsg.getbytes ()); } }Catch(IOException IoE) {ioe.printstacktrace (); }Catch(timeoutexception toe) {toe.printstacktrace (); } Catch(interruptedexception e) {e.printstacktrace (); }finally{ if(channel!=NULL) Channel.close (); if(conn!=NULL) Conn.close (); } }}
Simple instance of RABBITMQ--AMQP protocol with data write-back mechanism