aspose file tools*
The moose likes EJB and other Java EE Technologies and the fly likes Singleton in Cluster Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "Singleton in Cluster" Watch "Singleton in Cluster" New topic
Author

Singleton in Cluster

Rahul Devgan
Greenhorn

Joined: Aug 04, 2004
Posts: 24
Hi All,

I would like to understand as how to implement a singleton in a clustered environment.
One instance of a class across all server instances!


Rahul Devgan<br />SCJP<br />--- one must be willing to let go of small pleasures in life to achieve greater things ---
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

There are patterns you can use for this. If you are using JBoss and don't have to worry about other app resvers, you could just use their clustered singleton service. An alternative is to use RMI - create the singleton on one server and access it via RMI. But this will be a bottleneck. Perhaps you could consider why you need the singleton in the first place - its in an EJB environment so has to be read only. Is there therefore any real problem in having more than one?


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Rahul Devgan
Greenhorn

Joined: Aug 04, 2004
Posts: 24
Hi Paul,
Thanks for your reply.
I would like to understand how to maintain a single cache, to avoid cache synchronization like service locators for resource factories etc, across multiple server instances. I am aware that servers do this for us but what i want is to understand how it is achieved programmatically.

You said rmi with performance bottlenecks. Are you trying to imply that I should check on all server instances if the instance of the class I am dependent on exists using rmi? If not then pls explain your concept.

Also, I do not know in detail how servers implement the same. do they use some tools or congfiguration parameters etc ? So, if you or anyone else has an idea, all are welcome to help and contribute, thanks.
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

I think people refer to this as the "networked RMI object singleton" pattern, but don't quote me. What it involves is creating a remote service (i.e. an interface which extends java.rmi.Remote and an implementation which also extends javax.rmi.PortableRemoteObject) and deploying it on an app server. Now any part of your application can lookup the service and make remote calls to its public methods. Performance will always be a factor with singletons in a clustered envirenment (one instance of a class can conceivable have thousands of threads calling it). This is compounded when every call is via RMI. Added to this you've basically undermined the fail-over you get with clusters. Your service exists on one machine, it fails and all other instances in the cluster fail (in the sense that they can't use the service).


Also, I do not know in detail how servers implement the same

'fraid I don't know either, so can't help you there.
 
wood burning stoves
 
subject: Singleton in Cluster