When a client wants to invoke a method of a server to complete a function, it can use the PRC services supported by RABBITMQ.
In fact, the RPC service and ordinary send and receive messages are not very different, the process of RPC is actually
The client sends a message to the server-defined queue that carries the message, which should be the parameter of the method that the server will invoke, and use Propertis to tell the server to return the result to the specified queue.
Example:
1 Packagecom.zf.rabbitmq07;2 3 Importjava.io.IOException;4 5 Importcom.rabbitmq.client.AMQP.BasicProperties;6 ImportCom.rabbitmq.client.Channel;7 Importcom.rabbitmq.client.Connection;8 Importcom.rabbitmq.client.ConnectionFactory;9 Importcom.rabbitmq.client.ConsumerCancelledException;Ten ImportCom.rabbitmq.client.QueueingConsumer; One ImportCom.rabbitmq.client.QueueingConsumer.Delivery; A Importcom.rabbitmq.client.ShutdownSignalException; - - Public classRpcserver { the - Public Static FinalString rpc_queue_name = "Rpc_queue"; - - Public Staticstring SayHello (string name) { + return"Hello" +name; - } + A Public Static voidMain (string[] args)throwsIOException, Shutdownsignalexception, Consumercancelledexception, interruptedexception { at -ConnectionFactory CONNFAC =Newconnectionfactory (); -Connfac.sethost ("localhost"); - -Connection conn =connfac.newconnection (); - inChannel Channel =Conn.createchannel (); - toChannel.queuedeclare (Rpc_queue_name,false,false,false,NULL) ; + -Queueingconsumer consumer =NewQueueingconsumer (channel); the *Channel.basicconsume (Rpc_queue_name,false, consumer); $ Panax Notoginseng while(true){ -SYSTEM.OUT.PRINTLN ("Service side waits to receive message ..."); theDelivery deliver =consumer.nextdelivery (); +SYSTEM.OUT.PRINTLN ("Server successfully received message ..."); ABasicproperties props =deliver.getproperties (); the +String message =NewString (Deliver.getbody (), "UTF-8") ; - $String Responsemessage =SayHello (message); $ -Basicproperties Responseprops =NewBasicproperties.builder () - . Correlationid (Props.getcorrelationid ()) the . Build (); - Wuyi //return the results to the client queue theChannel.basicpublish ("", Props.getreplyto (), Responseprops, Responsemessage.getbytes ("UTF-8") ) ; - Wu //to confirm a message to the client -Channel.basicack (Deliver.getenvelope (). Getdeliverytag (),false); AboutSYSTEM.OUT.PRINTLN ("Service side return message complete ..."); $ } - - } - A}
1 Packagecom.zf.rabbitmq07;2 3 Importjava.io.IOException;4 ImportJava.util.UUID;5 6 ImportCom.rabbitmq.client.Channel;7 Importcom.rabbitmq.client.Connection;8 Importcom.rabbitmq.client.ConnectionFactory;9 Importcom.rabbitmq.client.ConsumerCancelledException;Ten ImportCom.rabbitmq.client.QueueingConsumer; One Importcom.rabbitmq.client.AMQP.BasicProperties; A ImportCom.rabbitmq.client.QueueingConsumer.Delivery; - Importcom.rabbitmq.client.ShutdownSignalException; - the Public classrpcclient { - - Public Static FinalString rpc_queue_name = "Rpc_queue"; - + Public Static voidMain (string[] args)throwsIOException, Shutdownsignalexception, Consumercancelledexception, interruptedexception { - +ConnectionFactory CONNFAC =Newconnectionfactory (); AConnfac.sethost ("localhost"); atConnection conn =connfac.newconnection (); -Channel Channel =Conn.createchannel (); - - //In response to QueueName, the server will send the information to be returned to the queue -String ResponseQueue =Channel.queuedeclare (). Getqueue (); - inString Correlationid =Uuid.randomuuid (). toString (); - toBasicproperties props =NewBasicproperties.builder () + . ReplyTo (responsequeue) - . Correlationid (Correlationid) the . Build (); * $String message = "Is_zhoufeng";Panax NotoginsengChannel.basicpublish ("", rpc_queue_name, props, Message.getbytes ("UTF-8")); - theQueueingconsumer consumer =NewQueueingconsumer (channel); + A Channel.basicconsume (responsequeue, consumer); the + while(true){ - $Delivery Delivery =consumer.nextdelivery (); $ - if(Delivery.getproperties (). Getcorrelationid (). Equals (Correlationid)) { -String result =NewString (Delivery.getbody ()); the System.out.println (result); - }Wuyi the } - } Wu -}
RABBITMQ Learning Notes (vii) RPC remote Procedure Call