• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

J2EE and Singletons?

 
velli fieldman
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have a question about the singleton classes and J2EE.
As i understand, singletons and J2EE do not match because of scalabiliy issues. Like i.e applications running in multiple vm's.
Is there an alternative when you really need someting like a singleton?
I have an application with creates business reports each in a seperate thread and i want to create a thread overview of running reports.
regards,
velli
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Singleton can also be used in J2EE. It depends if you can use it
for a J2EE environment. E.g. a Factory you can implement with a
Singleton, cause then it exists once per VM. You only shouldn't
use it to keep some information like you want.
Maybe you can put it into the application context. You find some info
here for
Orion Server, but this should work everywhere
[ August 04, 2003: Message edited by: Oliver Refle ]
 
Vinod John
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by velli fieldman:
Hi,
I have a question about the singleton classes and J2EE.
As i understand, singletons and J2EE do not match because of scalabiliy issues. Like i.e applications running in multiple vm's.
Is there an alternative when you really need someting like a singleton?
I have an application with creates business reports each in a seperate thread and i want to create a thread overview of running reports.
regards,
velli

A Singleton designed properly (or used in proper portion of the application) will not have scalibility issues. In many singleton implementations only the initialization and the subsequent access to its (singleton object) reference require serial access, if your initialization in done only once and the getting the instance portion of the code dosen't do any time consuming operation, then you singleton as good as a Java class that isn't sychronized.
Good example of usage of singleton in J2EE is implementation of Service locator pattern. check this out
http://java.sun.com/blueprints/corej2eepatterns/Patterns/ServiceLocator.html
 
Chris Mathews
Ranch Hand
Posts: 2712
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check out this link for some good information on Singletons in a J2EE Environment. It is an issue of The Java Specialists' Newsletter.
Overall, I prefer Kent Beck's view on Singletons: Don't use them.
 
Mark Whipple
Author
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I sounds like your needs might be met using a JMX MBean. Use of a singleton should not reduce scaleability, but spinning a thread in an EJB is not allowed by the spec. JMX provides the capability to have a 'service' that can monitor your report threads and collect the information that you need. It could be accessed via EJBs or external resources.
JBoss is a good example of using MBeans in an application server to provide 'services' to J2EE resources.
Mark
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic