Search code examples
javaserializationrmirpc

Can I (easily) use a third-party library to handle serialization for Java RMI?


I very much like the simplicity of calling remote methods via Java's RMI, but the verbosity of its serialization format is a major buzz kill (Yes, I have benchmarked, thanks). It seems that the architects at Sun did the obvious right thing when designing the RPC (speaking loosely) component, but pulled an epic fail when it came to implementing serialization.

Conversely, it seems the architects of Thrift, Avro, Kryo (especially), protocol buffers (not so much), etc. generally did the obvious right thing when designing their serialization formats, but either do not provide a RPC mechanism, provide one that is needlessly convoluted (or immature), or else one that is more geared toward data transfer than invoking remote methods (perfectly fine for many purposes, but not what I'm looking for).

So, the obvious question: How can I use RMI's method-invocation loveliness but employ one of the above libraries for the wire protocol? Is this possible without a lot of work? Am I evaluating one of the aforementioned libraries too harshly (N.B. I very much dislike code generation, in general; I dislike unnecessary annotations somewhat, and XML configuration quite a bit more; any sort of "beans" make me cringe--I don't need the weight; ideally, I'm looking to just implement an interface for my remote objects, as with RMI).


Solution

  • Once upon a time, I did have the same requirement. I had changed rmi methods arguments and return types to byte[].

    I had serialized objects with my preferred serializer to byte array, then called my modified rmi methods.

    Well, as you mentioned java serialization is too verbose, therefore 5 years ago I did implement a space efficient serialization algorithm. It saves too much space, if you are sending a very complex object graph.. Recently, I have to port this serialization implementation to GWT, because GWT serialization in Dev mode is incredibly slow.

    As an example;

    rmi method

    public void saveEmployee(Employee emp){
      //business code
     }
    

    you should change it like below ,

    public void saveEmployee(byte[] empByte) {
            YourPreferredSerializer serialier =   YourPreferredSerializerFactory.creteSerializer();
            Employee emp = (Employee) serializer.deSerialize(empByte);
            //business code
        }
    

    EDIT :

    You should check MessagePack . it looks promising.