Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Singleton Pattern in a clustered J2EE environment

 
Obiwan Kenobi
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In a clustered J2EE environment , what pattern should I use to mimick the functionality of a Singleton pattern ?

In the current setup the application is on a single instance of the application server and uses a singleton class that contains a hashMap which needs to be accessed / updated by multiple listeners , which receive messages of a queue.

The challenge is to move to J2EE components and use a clustered environment. In a clustered env. , there would be multiple JVMs and hence the singleton pattern will fail. Is there a solution to this issue ?
[ May 31, 2005: Message edited by: Obiwan Kenobi ]
 
Valentin Tanase
Ranch Hand
Posts: 704
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Unless your container doesn�t have a proprietary solution, it might not be possible, or at least not the same way a simple java singleton works. As a matter of fact with j2ee a singleton is not even a singleton within the same jvm. There could be many instances of the same singleton class running at the same time, loaded by different classloaders. Usually the container mimics the effect of singletons by replicating such objects and keeping them in sync. An example might be the jndi tree. Every server instance in the cluster has a copy of the global jndi tree that gets synchronized via ip-multicast.
One very poor solution to your singleton would be to bind such a singleton to jndi. This will definitely work, but is not a very good practice. Another solution might be provided using pinned object. These are special kind of rmi objects that bounds to a specific server instance and they are not cluster aware (hence the term of pinned objects). This could work as well, but again it might be a vendor specific solution. Besides the pinned object gets deployed to a unique server instance and this defeats the whole purpose of using a cluster. Finally you might consider reviewing your design and finding a better solution to your requirements without the need of using singletons.
Regards.
 
Balamaci Serban
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well i'm no expert but how about JBossCache(or i don;t know any other cache). It works even for clusters and from what i understand that cache could be modified syncronious. U could put those values in it.
Just a thought.
 
Obiwan Kenobi
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Valentin ,

I was planning to use the jndi look-up i.e. the ServiceLocator Pattern as a solution , but haven't found a lot of documentation on the same.
You mentioned there are some downsides to this ? What are the downsides ?

Here is one link that talks about the Service Locator pattern

http://dev2dev.bea.com/pub/a/2004/01/hurst.html

but its quite terse.

http://java.sun.com/blueprints/corej2eepatterns/Patterns/ServiceLocator.html

The pinned object soln. , is it similar to this one ..

http://www.theserverside.com/patterns/thread.tss?thread_id=20819

Btw, I'm using WAS 5.1 . Have you come across this scenario ?

http://www.theserverside.com/talks/videos/BillyNewportText/interview.tss
[ May 31, 2005: Message edited by: Obiwan Kenobi ]
 
Valentin Tanase
Ranch Hand
Posts: 704
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Serban,

Well i'm no expert but how about JBossCache(or i don;t know any other cache). It works even for clusters and from what i understand that cache could be modified syncronious. U could put those values in it.
Just a thought.

Well this is kind of vendor specific solution I was talking about. Weblogic doesn�t have any kind of similar cache (caching a graph of pojos), it uses read-only ejbs for the same purpose. However, neither one of the two looks like the kind of solution he�s looking for.

Hi Obi-Wan Kenobi,


was planning to use the jndi look-up i.e. the ServiceLocator Pattern as a solution , but haven't found a lot of documentation on the same.
You mentioned there are some downsides to this ? What are the downsides ?

If the object bound caches a lot of data, then all this data needs to be replicated across the cluster, resulting in lot of network traffic. As I read your requirements your singleton needs to cache a hashmap that gets updated by different clients. It looks to me like your object might abuse the jndi tree in this case.

Btw, I'm using WAS 5.1 . Have you come across this scenario ?

No I never used asymmetric/partitioned clusters. In order to go that far, you might follow Serban�s advice and check WAS documentation, I�m pretty sure they have very good caching strategies. I personally always admired IBM for their very good products.
Regards.
 
P. Ingle
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what about using good ol' static methods with final class?

I have a similar situation - I am using singleton implementation for DAOFactory which instatiates multiple DAOs as required. I don't want to create multiple instances of DAOFactory itself, hence am implementing as Singleton. Would it have same (or any) problems in Clustered env?
and if so, would static methods with final DAOFactory class work?

Thanks,
P.Ingle
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic