?
Solved

Best way to creating a Factory for stateless EJBs

Posted on 2004-11-08
6
Medium Priority
?
330 Views
Last Modified: 2013-11-24

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
0
Comment
Question by:mgeiser
  • 3
4 Comments
 
LVL 13

Expert Comment

by:petmagdy
ID: 12527436
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
 
LVL 13

Expert Comment

by:petmagdy
ID: 12527628
take a look at this, Sun Core Patterns for the service locator:

http://java.sun.com/blueprints/corej2eepatterns/Patterns/ServiceLocator.html
0
 

Author Comment

by:mgeiser
ID: 12528233
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
 
LVL 13

Accepted Solution

by:
petmagdy earned 2000 total points
ID: 12530363
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

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn different types of Android Layout and some basics of an Android App.
A solution for Fortify Path Manipulation.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Suggested Courses
Course of the Month14 days, 5 hours left to enroll

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question