Best way to creating a Factory for stateless EJBs


I have 5 Stateless EJBs and I want to create a factory that will give me an instance of the EJB type that I specify in get EJB()

For the getEJB(), I can pass in a whatever will make the most sense; a string, the class def of EJB to create, etc...

For removeEJB(), I guess I should pass in the instance returned from getEJB()???

Please notice the package structure.


How do I rewrite this given the above?


================================================
package com.mgeiser.gwmp.amsvcs.ejb;


import com.mgeiser.gwmp.amsvcs.ejb.ActivityEjb;
import com.mgeiser.gwmp.amsvcs.ejb.ActivitySummaryEjb;
import com.mgeiser.gwmp.amsvcs.ejb.ActivitySupportEjb;
import com.mgeiser.gwmp.amsvcs.ejb.ActivityTemplateEjb;
import com.mgeiser.gwmp.amsvcs.ejb.TemplateSupportEjb;

import com.mgeiser.gwmp.amsvcs.ejb.ActivityEjbHome;
import com.mgeiser.gwmp.amsvcs.ejb.ActivitySummaryEjbHome;
import com.mgeiser.gwmp.amsvcs.ejb.ActivitySupportEjbHome;
import com.mgeiser.gwmp.amsvcs.ejb.ActivityTemplateEjbHome;
import com.mgeiser.gwmp.amsvcs.ejb.TemplateSupportEjbHome;

import com.mgeiser.gwmp.amsvcs.ejb.AMSvcsJndiNames;

import com.mgeiser.framework.logging.SeiLogger;
import com.mgeiser.framework.jndi.ServiceLocator;
import com.mgeiser.framework.jndi.ServiceLocatorException;

import javax.ejb.EJBHome;
import javax.ejb.EJBObject;



public class AMSVCSEjbFactory {
  public AMSVCSEjbFactory() {
  }


  /**
    * <B>Description: </B> Gets an instance of ActivitySupportEjb
    * <B>Design Notes: </B>
    *
    * @author Michael J. Geiser
    * @version GWMP .1
    * @return ActivityEjb
    */
  private ActivitySupportEjb getEJB() {

    ActivitySupportEjb activitySupportEjb = null;
    ActivitySupportEjbHome activitySupportEjbHome;

    try {
      ServiceLocator serviceLocator = ServiceLocator.getInstance();
      activitySupportEjbHome = (ActivitySupportEjbHome) serviceLocator.getHome(AMSvcsJndiNames.JNDI_REMOTE_TemplateSupportEjb,
          TemplateSupportEjbTest.class, ActivitySupportEjbHome.class);
      activitySupportEjb = activitySupportEjbHome.create();

    } catch (ServiceLocatorException sle) {
// Throw an exception from ExceptionFactory
    }  catch (Exception e) {
// Throw an exception from ExceptionFactory
    }

    if (activitySupportEjb == null) {
// Throw an exception if the EJB is null here
    }

    return activitySupportEjb;
  }
  /**
   * <B>Description: </B>Calls remove on ActivityEjb
   *
   * @todo Document the impact on pool size when removeEJB() is called vs
   * when returning the SLSB to the pool is left to Garbage Collection
   *
   *
   * @author Michael J. Geiser
   * @version GWMP .1
   * @param activitySupportEjb ActivityEjb
   */
  private void removeEJB(ActivitySupportEjb activitySupportEjb) {

    try {
      activitySupportEjb.remove();
    }  catch (Exception e) {
//Throw Exception from ExceptionFactory
    }

  }

}

================================================





Thanks!


Geezer
mgeiserAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

petmagdyCommented:
Hi mgeiser,


as an example method in the Service Locator to lookup on an EJB local Home like this:

  public EJBHome getEjbHome(String ejbName, Class ejbClass) throws
      Exception
  {
    try
    {
      Object object = context.lookup(ejbName);
      EJBHome ejbHome = null;
      ejbHome = (EJBHome) PortableRemoteObject.narrow(object, ejbClass);
      if (ejbHome == null)
      {
        throw new ServiceLocatorException("Could not get home for " + ejbName);
      }
      return ejbHome;
    }
    catch (NamingException ne)
    {
      throw new ServiceLocatorException(ne.getMessage());
    }
  }
 
u use this method like this:

 ActivitySupportEjbHome activityRemoteInstance = (ActivitySupportEjbHome)
        ServiceLocator.getInstance().getEjbLocalHome(
        "java:comp/env/ejb/ActivitySupport", ActivitySupportEjbHome.class);

and then create ur ActivitySupportEjb remoteinstance
      activitySupportEjb = activitySupportEjbHome.create();


No better than this, and for the Stateless Bean u need not to warry about activitySupportEjb.remove(0 because it is stateless
Cheers!
0
petmagdyCommented:
take a look at this, Sun Core Patterns for the service locator:

http://java.sun.com/blueprints/corej2eepatterns/Patterns/ServiceLocator.html
0
mgeiserAuthor Commented:
right... but look at what I'm trying to do...

The ServiceLocator code is repeated everywhere I use the EJB.  In the example above, I'm looking to return an instance of ActivitySummaryEjb and not of ActivitySummaryEjbHome (or the bean for one of the other 4 EJBs if that is what is needed).

Yea, I could do something like create 5 methods

getActivityEjb();
getActivitySummaryEjb();
getActivitySupportEjb();
getActivityTemplateEjb();
getTemplateSupportEjb();

but that is not eloquent.

I'm after something like this in the calling class:

ActivityEjb activityEjb = (ActivityEjb)AMSVCSEjbFactory.getEJB(ActivityEjb);

and I do not want to overload getEJB() 5 times; I should need to...

BTW:  I want to call remove because the SLSB is returned to the pool immediately instead of during GC if I call remove.  This will be a frequently used SLSB and I'd like to minimize pool size whenever possible.



0
petmagdyCommented:
u can't grantee that SLSB.remove() will send to GC, the container automatically return the SLSB instance to the pool after finishing the method invocation, u control the size of the pool by configuring the application server parameters

and I am afraid their is no better way than u have to get an SLSB instance, but this will be better and much easier in EJB 3.0 specification as Sun promise
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java EE

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.