how to do a singleton in a java cluster without Java EE app server

Go To StackoverFlow.com

2

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

  1. when the cluster started up(3 machines), that only one started the singleton up
  2. When the machine with the running singleton failed, one of the other machines started up the singleton

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).

2012-04-05 18:28
by Dean Hiller
You know that JPA is a subset of Java EE, right - Matt Ball 2012-04-05 18:31
You seem to be confused about what "JEE" means. It is not a synonym for "EJB's". Aside for that, though... if you do not want to use JEE/EJB's, then why is "java-ee" the ONLY tag that you've tagged this question with - Steve Perkins 2012-04-05 18:38
yes, I did know JPA was subset of Java EE so yes, I mispoke there, I guess I should have said with no JavaEE app servers - Dean Hiller 2012-04-06 13:27
errr, I said JEE in the title as JPA, etc. etc. now all work outside JEE containers which is my main goal - Dean Hiller 2012-04-06 13:29
The Servlet API is a subset of JEE too. Tomcat, Jetty, etc may lack an EJB container, but their servlet container is still "JEE". I think what you mean to say is a solution that "requires only a servlet container", or that "doesn't require an EJB container" - Steve Perkins 2012-04-07 10:41
@Steve yes, we want to be in a plain pojo java app with a main - Dean Hiller 2012-04-10 12:45


1

I would go for and distributed locking, have a look at Hazelcast and Singleton.

Probably can do the trick as well.

2012-04-05 18:35
by Tomasz Nurkiewicz
I have seen this done with EHCache (open source project by Terracotta). However, you may get more thorough answers if you provide more detail about your application. Nine times out of ten, when you need a singleton in a clustered environment... what you really need is a JMS message queue based approach - Steve Perkins 2012-04-05 18:44


1

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

2012-04-10 13:15
by Rob.Mac


1

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

2014-10-16 16:17
by Pragalathan M


0

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.

2012-04-10 12:46
by Dean Hiller
Ads