Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best way to transmit Java objects over a network [closed]

I am trying to transmit objects of a particular class from one server to another.

The options I'm looking at are:

  • Serialize the data as JSON and send it over the wire using HTTP, and de-serialize at the other end.
  • Serialize the data into some binary form and transmit using TCP sockets.

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

Ideally I would like the interface to be versioned, so the sender and receiver can be upgraded independently.

I'm considering the JSON approach as I already have code that will serialize/deserialize the objects into JSON.

like image 853
tomdee Avatar asked Dec 01 '22 06:12

tomdee


2 Answers

Whenever I needed to transmit Java objects, assuming similar JVM versions, I always used plain Java serialization (Sun official tutorial): it's simpler and you don't have to care about transmit chains of items or aggregates because serialization already cares about it (if you implement it correctly).

So if you want to transmit a complex object made by many sub-objects you don't have to split it, send it and recompose it: you just send the object and it already contains everything that was already included.

EDIT

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

  1. RMI can help you in calling remote methods and obtaining objects from a master server
  2. Clients can use serialization (over a different socket) to send back objects or pass them as parameters to RMI invocation, this should be personal preference

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

like image 101
Jack Avatar answered Dec 21 '22 23:12

Jack


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

On the other hand, using JSON, XML or YAML will make it easier to debug as what gets transmitted will be readable.

like image 31
Emyr Avatar answered Dec 21 '22 23:12

Emyr