Search code examples
javaserializationrmi

Updating server object with deserialized object through RMI


I have an object on a server which clients can retrieve in a distributed system. This all happens on a local machine and the clients call the server object through RMI lookup. The point is that the server object is supposed to be one single object which clients can modify. However after it's deserialized at the clients, they all have a different object ID. That is, they seem to get a new object, even though they are all supposed to retrieve the same object from the server. This makes sense, considering the objects are supposed to be on separate machines (per client).

I have tried to implement the hashcode and equals methods on the server object, but the clients still get a different object id. When the clients perform actions on the deserialized server object, the original server object does not receive the changes. I know this because any subsequent lookups retrieve the server object in its original state.

Basically my question is: How can I let my clients update the original server object by performing client operations on the respective deserialized objects? When another client looks at the getters of its deserialized object, it must see all the changes performed by other clients. To clarify further, despite that the objects are deserialized over different machines, I want them to behave as if they were one and the same object on the same machine.


Solution

  • The point is that the server object is supposed to be one single object which clients can modify. However after it's deserialized at the clients, they all have a different object ID

    The point is that this isn't a remote object, so it gets serialized, which you don't want.

    I want them to behave as if they were one and the same object on the same machine.

    Exactly. You need to:

    1. Have it implement a remote interface
    2. Export it, either by having it extend UnicastRemoteObject (preferred) or calling UnicastRemoteObject.exportObject() on construction.