What is the best way to transfer Java objects over a network

I am trying to transfer objects of a certain class from one server to another.

The options I'm looking for are as follows:

  • Serialize the data as JSON and send it over the wiring using HTTP and de-serialize at the other end.
  • Serialize the data in binary form and transmit via TCP sockets.

What are the best practices in this area? What is gotchas?

Ideally, I would like the interface to be a version, so the sender and receiver can be updated independently.

I am considering a JSON approach, as I already have code that will serialize / deserialize objects in JSON.

+4
source share
8 answers

Whenever I needed to pass Java objects, assuming similar versions of the JVM, I always used simple Java serialization ( Sun's official guide ): it’s simpler and you don’t need to worry about the chaining of elements or aggregates because serialization already cares about that ( if you implement it correctly).

So, if you want to transfer a complex object made by many sub-objects, you do not need to separate it, send it and copy it: you just send the object, and it already contains everything that has already been included.

EDIT

About RMI: I used it along with serialization, and it worked like a charm! I used to develop remote swing (sending JPanels via TCP).

  • RMI can help you in calling remote methods and getting objects from the main server.
  • Clients can use serialization (through another socket) to send objects back or pass them as parameters for calling RMI, this should be a personal preference

Of course, I don’t know what exactly you want to do, but both tools work fine and can also be used orthogonally.

+6
source

If both ends are written in Java, just use your own Java serialization.

On the other hand, using JSON, XML, or YAML will simplify debugging, as the transmitted messages will be read.

+5
source

I know that this branch is already outdated, but I just stumbled upon it and decided that I would abandon my 2 cents for the benefit of future readers ...

If JSON is not required for any reason, there is no need for 2 applications to communicate with it. I agree with the Avro suggestions above. Avro allows you to write business logic code, and it is designed to serialize / deserialize for you. It will create a server-side interface for implementation (based on the scheme you provide), and a client proxy for calling the service. It can also be serialized in other formats.

There is not much to guide the documentation or examples, but here are a couple of decent views:

Here is a tutorial on using a socket server: http://gbif.blogspot.com/2011/06/getting-started-with-avro-rpc.html

Here's an open source project / demo project for using Avro over http: http://code.google.com/p/avro-http-example/

+4
source

I would recommend using Avro with JSON encoding; you will get version support when you use HTTP and JSON, and you don’t have to write serialization / de-serialization code. See http://hadoop.apache.org/avro/docs/current/spec.html for a brief description of Avro features and send a note to the mailing list or join the #avro channel on Freenode if you encounter any problems.

+3
source

Some options:

  • If you need to do this through public networks, use the json http approach, since you will not run into firewall problems - you can go through port 80
  • If your intranet is the fastest way to do this (in terms of setup and support), it's probably the old-fashioned Java rmi , which just requires you to define your remote interfaces and connect to the rmi server, you can start up and start up in a few minutes and constantly change your remote objects / services without any problems. Under the hood, it just uses the java series
  • If your needs are performance sensitive (low latency, high bandwidth), you can use something like Google protocol buffers
  • Also check the Externalizable interface for custom serialization.
+1
source

Why json This is commonly used for web applications.

It would be wiser to create a web service that can return the required Java object. Then you do not need to worry about serialization / deserialization.

0
source

You can share between data-only solutions and solutions for comparing data and behavior.

For data, you can use JSON , or you can use Java serialization. JSON is simple, but you have to collapse your own class wrappers. Using Java serialization may work well, because sorting problems are well known and handled in a consistent manner. An option for this would be to provide a Java class on both the local and the remote systems, but if you do, you can continue and make RMI . RMI is nice because you do not need to write a separate class for the remote end, but when distributing objects, you should follow some information, for example, make sure you set the serial identifier of the class or to distribute the exact binary class file on the remote system. You can do this with RPC , but if you go this far you can create a web service and use SOAP .

0
source

Over the years, I have tried several options or serialization / deserialization, including JSON, XML, SOAP, protobuf and some others that I am confused to name here :-)

Currently, we use JSON encoding almost exclusively to transmit Java-to-Java and JS-to-Java, and even to store internal data (if the amount of binary data does not make the JSON format too inefficient). Advantages of JSON are simplicity, low weight, both in terms of payload and complexity of implementation and accessibility of serialization solutions in all languages ​​affected by us. It also helps to have a “standard” save structure.

Jackson has been working well for us lately. Jabsorb also has a nice serialization package (de).

Versions: JSON does not have native version support ( Avro may have something), so you must run your own. It is usually recommended to maintain the numbering scheme of major / minor versions: major version numbers are incompatible, minors are backward compatible, so client 1.2 can talk to server 1.5, but not to server 2.1.

Gotchas:

  • It is sometimes difficult to convert Java generators such as TreeMap.
  • Some implementations may embed default implementation class names, making the protocol less resilient. You might want to do nothing more by continuing with the wire.
0
source

All Articles