By passing Java objects through a messaging server, the Java class must implement a serialization interface that transforms a Java object into a byte array, passed from a consumer or producer to another JVM, and requires two JVMs to share the class, such as the UserInfo class.
1. Class UserInfo for defining serialization
2. In the consumer, instantiate the UserInfo object and take out its byte array
3. Preparation of Producers
Code:Userinfo.javaPackage COM.TEST.RFC; public class UserInfo implements Java.io.serializable{private string name = null, public String getName () {return name;} p ublic void SetName (String name) {this.name = name;}}Server.java package Com.test.rfc; import Java.io.bytearrayoutputstream;import Java.io.ioexception;import Java.io.objectoutputstream; import Com.rabbitmq.client.amqp;import Com.rabbitmq.client.channel;import Com.rabbitmq.client.connection;import Com.rabbitmq.client.connectionfactory;import Com.rabbitmq.client.Consumer; Import Com.rabbitmq.client.defaultconsumer;import Com.rabbitmq.client.envelope; public class Server {public Byte[] Getuserbyte () throws Exception{userinfo U = new UserInfo (); U.setname ("Hello I come from MQ server"); Bytearrayoutputstream BAOs = Newbytearrayoutputstream () objectoutputstream oos = new ObjectOutputStream (BAOs); o Os.writeobject (u); Oos.close (); Baos.close (); return Baos.tobytearray (); public static void Main (string[] argv) {Server s = new server (); ConnectionFactory factory = new ConnectionFactory (); factory.setusername ("admin"); Factory.setpassword ("admin"); factory.sethost ("192.168.169.142");//Use default port 5672&NBsp Connection Connection = null; try {Connection = factory.newconnection (); final Channel channel = Connection.createchannel ();//Serialized Object final byte[] data = S.getuserbyte (); System.out.println (data.length); String queuename = "Queue_rpc"; Channel.queuedeclare (QueueName, False, False, false,null); Consumer Consumer = new Defaultconsumer (channel) { @Overridepublic void Handledelivery (String consumertag, Envelope Envelope, AMQP. Basicproperties properties,byte[] body) throws Ioexception {system.out.println ("rfc=" + new String (body)); Amqp. Basicproperties replyprops = NewAMQP.BasicProperties.Builder (). Correlationid (Properties.getcorrelationid ()). Build (); Channel.basicpublish ("", Properties.getreplyto (), replyprops, data); Channel.basicack (Envelope.getdeliverytag () , false);}}; Channel.basicconsume (QueueName, false, consumer);} catch (Exception e) {e.printstacktrace ();}} } client.java package Com.test.rfc; import Java.io.bytearrayinputstream;import Java.io.ioexception;import Java.io.objectinputstream;import Java.util.uuid;import Java.util.concurrent.arrayblockingqueue;import Java.util.concurrent.blockingqueue; import Com.rabbitmq.client.*; public class Client {public static void Main (string[] argv) {try{//The queue where the message is sent, and the server accepts the message on this queue, String queuename = "Queue_rpc"; ConnectionFactory factory = new ConnectionFactory (); factory.setusername ("admin"); Factory.setpassword ("admin"); factory.sethost ("192.168.169.142");//Use default port 5672 connection Connection = Null;connection = Factory.newconnection (); Channel channel = Connection.createchannel ();//generates a temporary queue where the client waits for the server to return information, and the server sends a message to this queue string replyqueuename =channel.queuedeclare (). Getqueue ();//generates a unique idfinal String Corrid = Uuid.randomuuid (). toString (); Amqp. Basicproperties props = NewAMQP.BasicProperties.Builder (). Correlationid (Corrid). RePlyto (Replyqueuename). build ();//client sends RFC request Channel.basicpublish ("", QueueName, props, "GetUserInfo". GetBytes ());// The server returns the message final blockingqueue response = newarrayblockingqueue (1); Channel.basicconsume (Replyqueuename, true,new Defaultconsumer (channel) {@Overridepublic void Handledelivery (String consumertag,envelope Envelope, AMQP. Basicproperties properties,byte[] body) throws IOException {System.out.println (Properties.getcorrelationid () + ", body = "+body.length"); if (Properties.getcorrelationid (). Equals (Corrid)) {Response.offer (body);}}); Byte[] B = response.take (); System.out.println (b.length);//deserialization of object Bytearrayinputstream Bais = new Bytearrayinputstream (b); ObjectInputStream OII = New ObjectInputStream (Bais); UserInfo u = (UserInfo) oii.readobject (); System.out.println (U.getname ()); Channel.close (); Connection.close ();} catch (Exception e) {e.printstacktrace ();}}}
8.RabbitMQ Message passing Java object