Java RMI Method Synchronization

I have a class that is stored on a "server" and multiple clients can call this method. This method returns a class. Now, when clients access the accessor methods in this class, for example a set of accessor methods. I want the object on the server to update and sync across all other clients.

How to use:

public synchronized setStatus(String s) { this.status = s; }

      

in java to achieve this.

thanks

+2


source to share


2 answers


The keyword synchronized

does not fulfill this function; rather, the keyword is synchronized

trying to acquire an internal lock on the current object.

Have all of your clients export themselves as objects Remote

and register for updates with the server. When one of your clients has updated the server, the server will then call all registered clients Remote

to update them.



This can be achieved with a template Observer

. If all of your clients implement the interface java.util.Observer

, and either the server or (if your class is Remote

), then the class you want to update can extend java.util.Observable

. When the class / server is updated, Observers can be notified with the new value, which will save some network latency for clients requesting the new value from the server.

+3


source


I am trying to understand this question. I have two ideas:

  • you are calling a remote call getObject()

    and then you want to update the returned object and keep it in sync across all clients. It won't work. When an object is passed to a client, the updates are only local to that client.

  • you are asking if the remote call can be synchronized and be meaningful for all clients after upgrade. The answer here is yes . It is assumed that all of your clients access the value not locally, but via a remote call:

      public String getStatus() throws RemoteException;
    
          



If so, your client calls, regardless of whether they invoked the method set

, will all get the new value.

0


source







All Articles