• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Service Locator Pattern

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How is your factory implemented? Often factories are realized as Singletons - the class will have a static reference to it self.
 
aparna chi
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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

 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic