Service Locator implementation

Discussions

EJB programming & troubleshooting: Service Locator implementation

  1. Service Locator implementation (1 messages)

    Hi, there

    I tried to implement the ServiceLocator design pattern. I have a stateless session bean A, which tries to get a reference to stateless session bean B. But I met a problem when I implements the ServiceLocator class.

    Originaly I have a HashMap instance variable in the ServiceLocator, thus I can cache the EJBHome objects. The code will be something like

    getEJBHome(String jndiName, Class class) {
    if (hashMap.containsKey(jndiName)) {
       // get from the hashMap cache
    }
    else {
       // use JNDI looup and at the end, store the EJBHome to the hashMap using jndiName as the key
    }

    Suddenly I realized that I can not just use HashMap, because HashMap is not thread safe. Since it is a singleton pattern, there might be multiple EJB A using this ServiceLocator object. I should use Hashtable or Collections.synchronizedMap(new HashMap()) instead

    However, I hesitated. The EJB spec prohibits us from using thread in our code and using Hashtable or Collections.synchronizedMap() seems voilating this rule.

    What am I going to do? Any suggestion?

    Thanks.

    Lan
  2. Service Locator implementation[ Go to top ]

    You might find some of this thread helpful.