Eminem Eminem - 7 months ago 37
Java Question

RabbitMQ for Java: how to send multiple values?

This is an extension on RabbitMQ for Java: how to send multiple float values?

Instead of 3 float parameters, I want to send 3 different classes as parameters and I want to use the JSon protocol.

The server is written in C#. Hence I decode the JSon string method of the server side.

Basically the solution offered on the other post in terms of floats is as follows:

final ByteBuffer buf = ByteBuffer.allocate(12) // 3 floats
.putFloat(f1).putFloat(f2).putFloat(f3); // put them; .put*() return this
channel.basicPublish(buf.array()); // send


This will write the floats in big endian (default network order and what Java uses as well).

On the receiving side, you would do:

// delivery is a QueuingConsumer.Delivery

final ByteBuffer buf = ByteBuffer.wrap(delivery.getBody());
final float f1 = buf.getFloat();
final float f2 = buf.getFloat();
final float f3 = buf.getFloat();


but I want to send class Car, Airplane, Boat as JSon formatted from Java to C#

Answer

I assume Car, Airplane and Boat are simple JavaBeans classes and their field members map the json contract.

You could use a json codec to serialize your objects into JSON. For example you can use Jackson or gson.

Classes:

class Car {
    private String model;
    public String getModel(){};
    public void setModel(String model){...};            
}
class Airplane {
    private String model;
    public String getModel(){};
    public void setModel(String model){...};            
}
class Boat {
    private String model;
    public String getModel(){};
    public void setModel(String model){...};            
}

The example will use Jackson and will output a structure like that: {"car":{"model":"xxx"},"boat":{"model":"xxx"}, ,"airplane":{"model":"xxx"}}

// Create the jsonFactory with an object mapper to serialize object to json
JsonFactory jsonFactory = new JsonFactory(new ObjectMapper());

// Create the byte array output stream
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

// Create the json generator 
JsonGenerator generator = jsonFactory.createGenerator(outputStream);

// Write the start object, ie. {}
generator.writeStartObject();
// Write the car "car":{}
generator.writeObjectField("car" , car);
// Write the car "boat":{}
generator.writeObjectField("boat" , boat);
// Write the car "airplane":{}
generator.writeObjectField("airplane" , airplane);
// Close the object
generator.writeEndObject();
// And the generator
generator.close();

// Convert the byte array stream to a byte array and publish the message
channel.basicPublish(outputStream.toByteArray());   

If you have a JavaBeans or a map which wraps theses 3 classes the code may be simplier:

ObjectMapper mapper = new ObjectMapper();
byte[] bur = mapper.writeValueAsBytes(wrapper);
channel.basicPublish(outputStream.toByteArray());   

Finally on the c# side you should create the same class and deserialize them.

You could use some json to java/c# generator, eg. jsonschema2pojo.