0

I have three agents, each is running in their own JVM. The first agent writes objects to a stream that the second reads. The second agent needs a way to read objects from a stream, and store the content of any objects not known to the JVM as a byte array so it can write the objects to the stream that the third agent (who knows of all objects) reads. The third agent can read the stream and deserialise all the objects received through it; it can do this without knowing whether the second agent did or did not know about the objects.

Hence my questions. How do I deserialise: 1. unknown objects (stream information that generates ClassNotFoundException) as byte arrays? and 2. how do I do this for class names (objects of type Class) where the named class cannot be resolved?

I found this, but it only tells me how to serialise objects as a byte array on the sending agent and deserialise them on the receiving agent; it tells me nothing about how I must control the streams in order to handle undefined objects or unknown class names.

Perhaps an agent needs to manipulate control information written to a stream so non-existent objects can be stored as an array of bytes that can be inserted at the appropriate time when the agent writes the array to an output stream as part of the usual stream data.

Any pointers to information that may give me a clue about how this is done would be appreciated.

  • 1
    Serialization is really mostly a legacy API for Java. I'd suggest JSON for transferring objects like this. – daniu Jun 15 '18 at 10:19
  • Thanks, I'll keep it in mind as a good reason to move to JSON, but for now, I'm stuck with the legacy. –  Jun 15 '18 at 10:22
  • 3
    Serialisation is not legacy as core idea. But reallly Hamlet question is "to have dependency on external classs or not to have?", JSON communication is try to solve this problem – Jacek Cz Jun 15 '18 at 10:27
  • I observe my particular problem should be easily surmountable if the second agent can simply store the unknown objects as a string of bytes so they can be written out when needed. I feel that I'll be able to move on easier if I deal with it as a few lines of Java code. –  Jun 15 '18 at 10:59
  • What you are looking for is `MarshalledObject`. – user207421 Jun 16 '18 at 00:48
  • @EJP A useful tidbit; thanks. If you gave this as an answer, I'd give it a tick. –  Jun 16 '18 at 01:07

1 Answers1

0

What you are looking for is java.rmi.MarshalledObject. It contains an object opaquely, so it can be passed through JVMs that don't have the class and indeed to a target JVM that doesn't have the class either, if you use the RMI codebase feature (java.rmi.server.codebase).

user207421
  • 305,947
  • 44
  • 307
  • 483
  • Perhaps this is an adequate solution, however, there are still two unknowns for me: 1. how do I get raw data of the stream to create the MarshalledObject? and 2. how do I access the raw data if the object cannot be created? –  Jun 17 '18 at 00:03
  • @000 You don't need the 'raw data of the stream'. You wrap your object in a `MarshalledObject`, serialize that, pass it through all the intermediate JVMs, deserialize it at the target JVM, and then call `get()` to retrieve the original object. As described in the Javadoc. – user207421 Jun 17 '18 at 01:40
  • I did read the Javadoc but missed the fact that a `MarshalledObject` implements `Serializable`. Thanks for the help. –  Jun 17 '18 at 04:34
  • Thank you, but it baffles me how you found this a 'useful tidbit', let alone an answer to be accepted, if you didn't understand it. – user207421 Jun 17 '18 at 09:44
  • Don't worry about it Esmond. People ask questions because they don't understand something... until their questions are answered. I think the answer you gave here will help others with a similar question. You helped me connect a few dots, and I'm thankful for that. Now, like Little Jack Horner, bask in the glory of your achievement and go help someone else. –  Jun 17 '18 at 10:55
  • But you accepted the answer before you understood it. It baffles me. – user207421 Jun 18 '18 at 22:00
  • If you must know... I thought I understood the answer, but I had some further thought and further questions emerged around how I get the data off the stream and into a `MarshalledObject` instance. When I looked at the Javadoc, I didn't see that objects of this type were serialisable. You pointed this fact out to me and that cleared the issue up; I needed only to create a `MarshalledObject` once when serialising data at the source. If you want to know the nature of misunderstanding, my reply is that asking such a question makes no sense, and you should stop pondering that which makes no sense. –  Jun 19 '18 at 01:32