Today building platforms is extremely quick with JPA, Guice, JSR 303 bean validation. It's rather easy to whip something up.
Let's say I was using Guice and I had a poller process as a singleton within a cluster of JVM machines. How could I easily make it so
This can't be too hard with today's libraries out there, right? The more I look, I think jgroups is the answer but would like to hear from others(Jboss uses jgroups from what I have seen so far).
I would go for hazelcast and distributed locking, have a look at Hazelcast and Singleton.
Probably terracotta can do the trick as well.
If you have a central database available in your application, you can use a pessimistic lock on a database table row. Every singleton on a particular server node tries to get a pessimistic lock (in Oracle aquired by "SELECT FOR UPDATE") on a specific table row. If the singleton gets the lock, it can do whatever is needed. Otherwise it just waits for the lock. If the working singleton instance goes down, another waiting singleton gets the lock and thus starts working. You have to take care not to end the transaction by commit or rollback in order to ensure that the row level lock is held as long as the instance is alive.
Alternatively, the working singleton could save a timestamp in a periodic manner and the other waiting singletons check that this timestamp is updated during a particular time interval. If it gets not updated anymore, another singleton can get the "owner" of that timestamp and start working itself. This approach doesn't need a dedicated database connection for holding the lock, but takeover to another cluster member is slower because the timeout must be awaited.
-- Robert
You can use JGroups (without any App server) to accomplish this as many people have suggested. Sample implementation can be found at http://speakingjava.blogspot.in/2014/10/how-to-execute-singleton-task-in-cluster.html
I was looking into this more. JBoss looks like they use jgroups which seems simple enough and that may be a way to go as well.