English 中文(简体)
What is the best way to transmit Java objects over a network [closed]
原标题:

Closed. This question is opinion-based. It is not currently accepting answers.


Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.

Closed 7 years ago.

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.

最佳回答

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.

问题回答

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.

I know this thread is old now, but I just came across it and figured I d throw in my 2 cents for the benefit of future readers...

Unless JSON is required for some reason, there is really no need for 2 applications to communicate using it. I agree with the Avro suggestions above. Avro lets you write business logic code and it deals with the serializing/deserializing for you. It will generate a server-side interface to implement (based on a schema you provide) and a client proxy stub to call the service through. It can serialize to other formats if needed too.

There isn t much for guiding documentation or examples out there, but here are a couple decent looking ones:

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

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

I d recommend using Avro with the JSON encoding; you ll get the versioning support you need while still using HTTP and JSON, and you won t have to write the serialization/de-serialization code yourself. See http://hadoop.apache.org/avro/docs/current/spec.html for a concise description of the features of Avro, and drop a note to the mailing list or join the #avro channel on Freenode if you have any troubles getting started.

Some options:

  • If you need to do this over public networks, use the json http approach, as you will not run into firewall issues - you can go over port 80
  • If your on an intranet the fastest way to do this (from a setup & maintainability point of view) is probably old fashioned Java rmi, which just requires you to define your remote interfaces and connect to an rmi server, you can be up and running in a few minutes and make ongoing changes to your remote objects/services without much fuss. Under the hood this just uses java serialisation
  • If your needs are performance sensitive (low latency, high throughput) you could use something like google protocol buffers
  • Also check out the Externalizable interface for custom serialisation

Why JSON? That is typically used for web applications.

It would make more sense to make a web service which can return the Java object required. Then you don t need to worry about serializing/deserializing it.

You choices are divided between data only solutions and data plus behavior solutions.

For data, you can use JSON or you can use Java s serialization. JSON is simple but you ll have to roll your own class wrappers. Using Java s serialization can work well because the marshalling issues are well-known and handled in a consistent manner. A variation of this would be providing the Java class on both the local and remote systems but if you re doing that you might as well go on and do RMI. RMI is nice because you don t have to write a separate class for the remote end, but you do have watch out for some gotcha s when distributing objects, like making sure you set the Serial ID of the class or having to distribute the exact binary class file to the remote system. You could do it with RPC but if you re going that far, you might as well create a web service and use SOAP.

Over the years I have tried quite a few options or serialization/deserialization, including JSON, XML, SOAP, protobuf and some others I am embarrassed to name here :-)

Right now we are using JSON encoding almost exclusively for both Java-to-Java and JS-to-Java transfers, and even for storing internal data (unless the amount of binary data makes the JSON format too inefficient). The advantages of JSON are simplicity, light weight, both in terms of payload and the implementation complexity and availability of serialization solutions in all languages we have touched. It also helps to have "standard" persistence framework.

Lately Jackson has been working out well for us. Jabsorb also has good (de)serialization package.

Versioning: JSON has no built-in versioning support (Avro may have something), so you have to run your own. It is usually good idea to keep major/minor version numbering scheme: major version numbers are incompatible, minors are backward-compatible so that client 1.2 can talk to server 1.5 but not to server 2.1.

Gotchas:

  • It is sometimes challenging to convert Java generics, like TreeMap.
  • Some implementations may embed implementation class names by default which makes the protocol less change-resistant. You may want to make nothing redundant goes on the wire.




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签