Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <blockquote> <p>1) If I implement my RMI remote methods as synchronized, are they guaranteed to be mutually exclusive? I need to make sure that no two of my RMI methods (methods offered to clients) execute at the same time.</p> </blockquote> <p>RMI does not provide such guarantee on its own (unlike EJB) and two calls on the same remote object may be executed concurrently unless you implement some synchronization. Your approach is correct, and synchronizing all methods will indeed ensure that no two of them run at the same time on the same object. Note: The keyword <code>synchronized</code> alone is equivalent to <code>synchronized( this )</code>.</p> <blockquote> <p>2) I have a method that the server executes periodically. It is used to do cleanups. I have to make sure that this particular method does not execute when there is any RMI method being run/used by remote clients.</p> </blockquote> <p>If the cleanup job is in another class, you will need to define a lock that you will share between the remote object and the cleanup job. In the remote object, define an instance variable that you will use as a lock. </p> <pre><code>protected Object lock = new Object(); </code></pre> <p>By convention, people use an <code>Object</code> for this purpose. Then you need to grab the lock in your periodic job with <code>synchronized( remoteObj.lock ) { ... }</code>, assuming it's in the same package.</p> <p>The other methods in the remote object will need to be synchronized the same way (<code>synchronized</code> alone is not enough), so that remote method calls and periodic job are both exclusive.</p> <blockquote> <p>I have considered implementing RMI methods as static and including that cleanup method inside the RMI interface, but it does not seem to be an elegant way of solving the problem.</p> <p>I have also written the cleanup method inside the RMI interface as synchronized. When I ran it for testing, there did not seem to be collisions between methods, but I cannot be sure.</p> </blockquote> <p>If I understand well, you would like to have the cleanup logic be a static method? A static method with <code>synchronized</code> alone grabs a lock on the class. A "regular" method with <code>synchronized</code> grabs a lock on the object instance. These are not the same implicit locks!</p> <p>But if you have only <em>one</em> remote object instantiated, you can make the <em>lock</em> static (That's the same as locking on the class, but is a bit cleaner). The cleanup code can then be static as well and be in the same class as the remote object or not.</p> <p>Skeleton:</p> <pre><code>public class MyRemoteClass { public static Object lock = new Object(); public void doStuff() { synchronized( lock ) { ... } } } public class Cleanup { public static void doIt() { synchronized( MyRemoteClass.lock ) { ... } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload