Hi,
I need to create a bean that :
1 - will be a singleton
2 - Must never been released
3 - Must start after the EJB server is ready and all other
EJB are deployed.
I do not know how to do this !
I m searching for a design strategy. What bean I can use, ...
I really need some help
Thanks a lot for your help
Christophe
-
>>> Singleton + Never released + Last started <<< (6 messages)
- Posted by: Christophe Demez
- Posted on: December 28 2000 07:22 EST
Threaded Messages (6)
- >>> Singleton + Never released + Last started <<< by Gopesh Desai on December 28 2000 09:14 EST
- >>> Singleton + Never released + Last started <<< by Saruman White on December 28 2000 10:43 EST
-
>>> Singleton + Never released + Last started <<< by Amir Yasin on December 28 2000 09:19 EST
- >>> Singleton + Never released + Last started <<< by Amir Yasin on December 28 2000 09:24 EST
-
>>> Singleton + Never released + Last started <<< by Amir Yasin on December 28 2000 09:19 EST
- >>> Singleton + Never released + Last started <<< by Saruman White on December 28 2000 10:43 EST
- >>> Singleton + Never released + Last started <<< by Dimitri Rakitine on December 29 2000 16:20 EST
- >>> Singleton + Never released + Last started <<< by Oon Kean Lin on August 15 2001 02:19 EDT
-
>>> Singleton + Never released + Last started <<<[ Go to top ]
- Posted by: Gopesh Desai
- Posted on: December 28 2000 09:14 EST
- in response to Christophe Demez
Hi,
One easy way to solve all ur problem is by having a static class called SingletonEJB in following fashion
public class SingletonEJB
{
private EJBBean ejbInstance = null;
public SingletonEJB()
{
Context ctx = InitialContext.getInitialContext();
EJBBeanHome ejbBeanHome = ctx.lookUp("SomeName");
ejbInstance = ejbBeanHome.create();
}
public static EJBBean getInstance()
{
return ejbInstance;
}
}
Now u can load this class at start up by specifying in server properties file. This class will hold the reference for ur singleton EJBBean.....
U can access the instance by calling SingletonEJB.getInstance() in any of ur client code...
Hope this solves ur problem
Gopesh Desai
Cognizant Techonology Solutions -
>>> Singleton + Never released + Last started <<<[ Go to top ]
- Posted by: Saruman White
- Posted on: December 28 2000 10:43 EST
- in response to Gopesh Desai
I don't mean to critisize this last piece of code, but what's that??? It won't even compile. becuase static method will try to return instance variable. Making allwoance for typo - that is, ejbInstance should be declared static, it's still not clear where it is initialized. This public constructor, where is it called? And what prevents this bean instance from being released? -
>>> Singleton + Never released + Last started <<<[ Go to top ]
- Posted by: Amir Yasin
- Posted on: December 28 2000 21:19 EST
- in response to Saruman White
public class SingletonEJB
{
private static EJBBean ejbInstance = null;
/*this is protected so it can't be called outside this class and still be subclassed.*/
protected SingletonEJB()
{ }
public static synchronized EJBBean getInstance()
{
if (ejbInstance == null)
{
Context ctx = InitialContext.getInitialContext();
EJBBeanHome ejbBeanHome = ctx.lookUp("SomeName");
ejbInstance = ejbBeanHome.create();
}
return ejbInstance;
}
}
this version should compile and work (I didn't try to compile it but its a pretty simple singleton). Don't know if this will necessarily solve your problem without a more detailed understanding of your specific requirements, but hopefully it gets you thinking along the correct lines.
Cheers,
Amir -
>>> Singleton + Never released + Last started <<<[ Go to top ]
- Posted by: Amir Yasin
- Posted on: December 28 2000 21:24 EST
- in response to Amir Yasin
Sorry for the two replies, just thought I should mention that this is not a bean but a simple singleton class and that its actually a VM singleton rather than a machine or system singleton so if your using a distributed app server(multiple app server instances for load balanceing for example) or an app server that allows you to have multiple VMs in one app server (such as GemstoneJ) this type of singleton will not work. There are other things you can do, but they are more complicated, if you want to get into a more detailed discussion we can, but I don't have time to post too much more now
Cheers,
Amir -
>>> Singleton + Never released + Last started <<<[ Go to top ]
- Posted by: Dimitri Rakitine
- Posted on: December 29 2000 16:20 EST
- in response to Christophe Demez
You can implement this as an RMI object (pinned to a specific box if you want a cluster-wide singleton). This is probably the most portable.
Another option is to use stateless session bean and deploy it with max-beans-in-pool set to 1 - all accesses to it will be serialized by the container and it will act as a singleton. There is cheating involved, because the bean remembers it's state between invocations, and there is risk that container may decide to discard the instance. -
>>> Singleton + Never released + Last started <<<[ Go to top ]
- Posted by: Oon Kean Lin
- Posted on: August 15 2001 02:19 EDT
- in response to Dimitri Rakitine
Why not just use a stateful session bean ? This way , there will not be any risk of the bean being released ..