Hi all,
I am in the design phase of a new project, and since I will be using JMS I plan to use EJB's for other parts also. My problem is that I need to implement a component that should only have one instance which will be handling connection to other API's and inserts some messages to JMS Queue. Previously I was using the ServletContext object for storing a similar object. Then I could access it from all servlets and I can control that there only exists one instance. I searched all EJB types but nothing seemed to serve my needs. Any suggestions.
Thanks
-
Single instance - Which EJB to use? (6 messages)
- Posted by: Ali Akkaya
- Posted on: February 09 2005 16:24 EST
Threaded Messages (6)
- Single instance - Which EJB to use? by Martin Straus on February 10 2005 08:30 EST
- Single instance - Which EJB to use? by Ali Akkaya on February 10 2005 09:16 EST
- Single instance - Which EJB to use? by Ali Akkaya on February 10 2005 09:19 EST
- Single instance - Which EJB to use? by adrian osullivan on February 10 2005 12:16 EST
- Single instance - Which EJB to use? by Ali Akkaya on February 10 2005 09:16 EST
- Does this need to EJB ? by Vee Gee on February 10 2005 15:15 EST
- Does this need to EJB ? by Ali Akkaya on February 11 2005 07:32 EST
-
Single instance - Which EJB to use?[ Go to top ]
- Posted by: Martin Straus
- Posted on: February 10 2005 08:30 EST
- in response to Ali Akkaya
We had to to such a thing in the system I'm working on. The trick was to use a normal EJB access a unique instance of the class implementing the business logic. NOT A SINGLETON IMPLEMENTATION. That unique instance was accessible through RMI. Thus, the flow looks pretty much like a lazy loading:
public class MySession implements SessionBean
RMIUnique rmiUnique = null;
// other lifecycle methods...
public void ejbActivate()
{
rmiUnique = (RMIUnique) rmiRegistry.get("name");
if (rmiUnique == null)
{
rmiUnique = new RMIUnique();
rmiRegistry.register("name", rmiUnique);
}
}
public void someLogic()
{
rmiUnique.someLogic();
}
The sintax sucks, but you get the idea, right? Different instances of the session delegate excecution on a unique instance. Applying singleton here makes no sense since the instance obtained is unique to the JVM.
Cheers and happy coding,
Martin -
Single instance - Which EJB to use?[ Go to top ]
- Posted by: Ali Akkaya
- Posted on: February 10 2005 09:16 EST
- in response to Martin Straus
Thanks for the quick reply, it is very helpful but I have one question about the code. Single instance is really very crutial to me and I wonder if it is possible that two instance s of my EJBs refer to RMI object, see that it is null, create one and register that to JNDI. There will be a unique instance bound to JNDI but two will be created. If I am right, is there a better way to register the RMI object that makes sure that only one instance is created
Ali -
Single instance - Which EJB to use?[ Go to top ]
- Posted by: Ali Akkaya
- Posted on: February 10 2005 09:19 EST
- in response to Ali Akkaya
And one more thing, can the RMI object be a non serialized object, cause I will have to deal with TCP sockets and similar stuff that can not be serialized. I heard about JNDI references but dont know if it solves my problem.
Ali -
Single instance - Which EJB to use?[ Go to top ]
- Posted by: adrian osullivan
- Posted on: February 10 2005 12:16 EST
- in response to Ali Akkaya
It is possible that the ejbs will attempt to create 2 instances of the RMI object, as EJB methods cannot be synchronized, but only one can be bound to a JNDI name: you can detect the 'already bound' error and handle this. -
Does this need to EJB ?[ Go to top ]
- Posted by: Vee Gee
- Posted on: February 10 2005 15:15 EST
- in response to Ali Akkaya
May be you can define a factory object and register that in JNDI to handle the connections, much similar to a DataSource. That factory class for the connection could provide the single instance of connection handler ( independent of the EJB it is being invoked from ). -
Does this need to EJB ?[ Go to top ]
- Posted by: Ali Akkaya
- Posted on: February 11 2005 07:32 EST
- in response to Vee Gee
Of course it does not have to be EJB, I only need an Class that will have one instance even if the system is running as a cluster. And access this object from my EJBs. RMI solution seems nice, I will try that, thanx for all replies.
Cheers
Ali