Question

I'm trying to implement an ID lock system that is managed by RMI. Pretty much, when a client enters an ID, it sends that ID along to the RMI server, which stores it in a list, along with the client ID, and a cycle counter.

The server, after 30 seconds, will increment all the cycle counters. Unless the client sends along an Unlock message, the client will send along a Heartbeat, which will update that cycle counter back to 0. If a cycle counter >= 3, then the server will assume the client has disconnected improperly and release the lock on the ID.

I've been researching online, and I found a great deal of very specific ways of doing this, but too many that seem too vague, or too specific to the one project's needs. I think I have a general idea of how to do this, but I want to get things straight:

1 - Most implementations would have this "cycle update" logic happening in an endless loop on a separate thread. Is this correct? It just seems like my RMI server's Main class has very little going on, and that maybe I could implement the loop in there.

2 - If indeed the thread is the right way to go, when do I launch the thread? Is there any special way that this needs to be accomplished? I've looked online and found many sites saying to use threads, but none showing a good example of where they execute the thread.

Thanks for any help you can provide. I'm still kind of green with RMI, so I thank you for your patience.

Was it helpful?

Solution

I think, your problem is that how to increment cyclic counters every 30 minutes, not about RMI.

if so, you may use ScheduledExecutorService.

there is a pseudo code below.

public class IdLockMgr {
  private Map<String, Integer> idToCycleMap = new HashMap<>();
  private ScheduledExecutorService service;
  public IdLockMgr() {
     service = Executors.newScheduledThreadPool(1);
     //BAD
((ScheduledThreadPoolExecutor)service).setExecuteExistingDelayedTasksAfterShutdownPolicy(false);
     service.scheduleAtFixedRate(...);//this is point.
  }
  public void lockId(String id) {
    idToCycleMap.put(id, 0);
  }
  public void shutdown() {
     service.shutdown();
  }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top