aspose file tools*
The moose likes EJB and other Java EE Technologies and the fly likes Service Locator Pattern Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "Service Locator Pattern" Watch "Service Locator Pattern" New topic
Author

Service Locator Pattern

aparna chi
Greenhorn

Joined: May 19, 2004
Posts: 16
I have a class called EJBHomeFactory which as the name suggests caches the Home objects for EJB's which gives better performance since JNDI look up is not done every time.
I have one question if my object of this class contains several cached home objects and now the object of this class is not used by anyone so will it get garbage collected as no other object is referencing this factory object?
Ajith Kallambella
Sheriff

Joined: Mar 17, 2000
Posts: 5782
How is your factory implemented? Often factories are realized as Singletons - the class will have a static reference to it self.


Open Group Certified Distinguished IT Architect. Open Group Certified Master IT Architect. Sun Certified Architect (SCEA).
aparna chi
Greenhorn

Joined: May 19, 2004
Posts: 16
Thanks.
Here is the code for my factory:

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import javax.ejb.EJBHome;
import javax.ejb.EJBLocalHome;

import javax.naming.InitialContext;
import javax.naming.NamingException;

import javax.rmi.PortableRemoteObject;
public class EJBHomeFactory
{
private Map ejbHomes;
private static EJBHomeFactory aFactorySingleton;
InitialContext ctx;
/**
* EJB home factory private constructor
*/
private EJBHomeFactory() throws NamingException
{
ctx = new InitialContext();
this.ejbHomes = Collections.synchronizedMap(new HashMap());
}
/*
* Returns the singleton instance of EJBHomeFactory
*/
public static EJBHomeFactory getFactory()
{
try
{
if(EJBHomeFactory.aFactorySingleton == null)
{
EJBHomeFactory.aFactorySingleton = new EJBHomeFactory();
}
}catch(Exception e)
{
System.out.println("exception in static ejb home factory "+e);
}
return EJBHomeFactory.aFactorySingleton;
}
/**
* Look up and cache an EJBHome object using a home class.
*/
public EJBHome lookUpHome(Class homeClass)
{
EJBHome anEJBHome;
anEJBHome = (EJBHome)this.ejbHomes.get(homeClass);
try
{
if(anEJBHome == null)
{
anEJBHome = (EJBHome)PortableRemoteObject.narrow(ctx.lookup(homeClass.getName()), homeClass);
this.ejbHomes.put(homeClass, anEJBHome);
}
}catch(Exception e)
{
System.out.println("exception in look up home "+e);
}
return anEJBHome;
}
}
I am not sure whether the object of this class will get garbage collected if no other class in the system is referring to it.
Can you help me out with this?
guddu guddu
Greenhorn

Joined: May 20, 2004
Posts: 1
I come across an example on the, but is really good and clear.

Can check this out

http://www.ideas2work.com/share-knowledge-details.php/username/Amit/title/Service%20locator%20is%20here%20to%20solve%20problem%20such%20as%20slow%20response,%20over%20head%20of%20re%20creation%20of%20Home%20ref/technology/Java-Design-Pattern/

Thanks

Guddu

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Service Locator Pattern