[jboss-user] [EJB] - caching the jndi call

redlight do-not-reply at jboss.com
Wed Nov 18 16:12:37 EST 2009


hi,

i got my ejb's up and running

in my web project i call them like that 


  | TraxManager TraxManager = (TraxManager)context.lookup("TraxService/remote");
  | 

in my web project i implement this call in each of managed beans, which no an optimal solution

so i try to do a generic method ==>



  | import java.util.Collections;
  | import java.util.HashMap;
  | import java.util.Map;
  | import java.util.Properties;
  | 
  | import javax.ejb.EJBHome;
  | import javax.naming.Context;
  | import javax.naming.InitialContext;
  | import javax.naming.NamingException;
  | 
  | public class EJBHomeFactory {
  | 	
  |  private static  Map<String, Object> cache=Collections.synchronizedMap(new HashMap());; //used to hold references to EJBHomes/JMS Resources for re-use
  | 
  | 	
  | 	 private EJBHomeFactory() throws ServiceLocatorException  {
  | 	      try {
  | 		//THIS DOESN'T GET EXECUTED :(	       
  | 	        cache = Collections.synchronizedMap(new HashMap());
  | 	      } catch (Exception e) {
  | 	            throw new ServiceLocatorException(e);
  | 	       }
  | 	    }
  | 
  | 	 
  | 	public static Object lookupObject(String jndiName) throws ServiceLocatorException {
  | 		 EJBHome home = null;
  | 		 Context context = null;
  |        
  | 		  try {   
  | 		        if (cache.containsKey((Object)jndiName)) {
  | 		            return cache.get(jndiName);
  | 		        } else {         
  | 		        	Properties props  = new Properties();
  | 			           props.setProperty("java.naming.factory.initial","org.jnp.interfaces.NamingContextFactory");
  | 			           props.setProperty("java.naming.provider.url", "jnp://192.168.1.201:1099");
  | 			           props.setProperty("java.naming.factory.url.pkgs", "org.jboss.naming");
  | 			           props.setProperty("jnp.socket.Factory", "org.jnp.interfaces.TimedSocketFactory");
  | 			             context = new InitialContext(props);
  | 		            //Object obj = context.lookup(jndiName);
  | 		            cache.put(jndiName, context.lookup(jndiName));
  | 		            		        }
  | 		       } catch (NamingException ne) {
  | 		            throw new ServiceLocatorException(ne);
  | 		       } catch (Exception e) {
  | 		            throw new ServiceLocatorException(e);
  | 		       }
  | 		       return cache.get(jndiName);
  | 
  |     }
  | 
  | 
  | 
  | 	public Map<String, Object> getCache() {
  | 		return cache;
  | 	}
  | 
  | 
  | 
  | 	public void setCache(Map<String, Object> cache) {
  | 		EJBHomeFactory.cache = cache;
  | 	}
  | 	
  | 
  | }
  | 
  | 
  | 

i test with 2 class, so that the first one , fill the map and the second should find a a map with the key of the jndi name , but it never found it,

  |    if (cache.containsKey((Object)jndiName)) {
  | 		            return cache.get(jndiName);
  | 		        } 
is never executed


and thanks 

View the original post : http://www.jboss.org/index.html?module=bb&op=viewtopic&p=4266404#4266404

Reply to the post : http://www.jboss.org/index.html?module=bb&op=posting&mode=reply&p=4266404



More information about the jboss-user mailing list