Discussions

EJB design: Single instance - Which EJB to use?

  1. Single instance - Which EJB to use? (6 messages)

    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

    Threaded Messages (6)

  2. Single instance - Which EJB to use?[ Go to top ]

    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
  3. Single instance - Which EJB to use?[ Go to top ]

    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
  4. Single instance - Which EJB to use?[ Go to top ]

    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
  5. Single instance - Which EJB to use?[ Go to top ]

    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.
  6. Does this need to EJB ?[ Go to top ]

    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 ).
  7. Does this need to EJB ?[ Go to top ]

    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