10

目前我正在使用以下代码来查找普通 POJO 类的 EJB3 无状态会话 bean。(我们在 JEE5 中,所以我们不能在普通 POJO 类中注入无状态会话 Bean,我必须使用查找)

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

import org.apache.log4j.Logger;  

public Object getEJB(String jndiName) {  

                logger.debug("WEBSPHERE EJB Lookup : " + jndiName);  
                String modifiedJndiName = "";  
                Hashtable<Object, Object> properties = new Hashtable<Object, Object>();  
                properties.put(Context.INITIAL_CONTEXT_FACTORY, "com.ibm.websphere.naming.WsnInitialContextFactory");  
                properties.put(Context.PROVIDER_URL, "iiop://localhost:2809");  
                try {  
                    Context context = new InitialContext(properties);  
                    logger.debug("WEBSPHERE EJB Lookup Modified JNDI Name: " + modifiedJndiName);  
                    return context.lookup("ejblocal:"+modifiedJndiName);  
                }catch (NamingException ne) {  
                    logger.debug("Naming Exception occurred :"+jndiName +">>>"+ne.getMessage());  
                    logger.error(ne.getMessage(), ne);  
                }  

                return null;  
            }  

那么 Context 对象是 ThredSafe 吗?我应该为每个调用创建 Context 对象[如此代码片段所示],还是可以为所有线程重用 Context?

4

2 回答 2

15

javadoc中通常已经提到了有关线程安全的答案,只要相关。事实上,InitialContextjavadoc提到了以下内容:

实例InitialContext不与多个线程的并发访问同步。每个操作不同实例的多个线程InitialContext不需要同步。需要同时访问单个InitialContext实例的线程应该在它们之间同步并提供必要的锁定。

最后一句话证实了这一点:它不是线程安全的,并且每个线程同步是必要的。但是,在您的特定代码示例中,不需要同步,因为它是在方法本地范围内创建的(即它绝对不是线程之间共享的)。如果InitialContext在您的特定代码示例中是一个实例变量,那么您必须将synchronized关键字添加到getEJB()方法中。

于 2012-11-10T00:14:15.033 回答
0

但是如果我把这个方法放在一个单例类中,那么我可以使用 Context 作为类变量吗?就像下面的代码方法在一个单例ServiceLocator类中一样。

import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;
import java.util.Properties;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

public class ServiceLocator {
    static volatile ServiceLocator serviceLocator = null;

    Map supportedAppServerMap = null;

    @Override
    public Object getEJB(String jndiName) {

        logger.debug("WEBSPHERE EJB Lookup : " + jndiName);
        String modifiedJndiName = "";
        Hashtable<Object, Object> properties = new Hashtable<Object, Object>();
        properties.put(Context.INITIAL_CONTEXT_FACTORY, "com.ibm.websphere.naming.WsnInitialContextFactory");
        properties.put(Context.PROVIDER_URL, AMPPropertyUtil.getProperty("PROVIDER_URL"));
        try {
            Context context = new InitialContext(properties);
            if (null != jndiName && jndiName.indexOf(".") != -1)
                modifiedJndiName = jndiName.substring(jndiName.lastIndexOf(".") + 1);
            else
                modifiedJndiName = jndiName;
            logger.debug("WEBSPHERE EJB Lookup Modified JNDI Name: " + modifiedJndiName);
            return context.lookup("ejblocal:" + modifiedJndiName);
        } catch (NamingException ne) {
            logger.debug("Naming Exception occurred :" + jndiName + ">>>" + ne.getMessage());
            logger.error(ne.getMessage(), ne);
        }

        return null;
    }

    @Override
    public DataSource getDataSource() {
        Context context = null;
        final String dsName = AMPPropertyUtil.getProperty("dsName");
        Hashtable<Object, Object> properties = new Hashtable<Object, Object>();
        properties.put(Context.INITIAL_CONTEXT_FACTORY, "com.ibm.websphere.naming.WsnInitialContextFactory");
        properties.put(Context.PROVIDER_URL, AMPPropertyUtil.getProperty("PROVIDER_URL"));
        try {

            context = new InitialContext();
            return (DataSource) context.lookup("java:comp/env/" + dsName);

        } catch (NamingException e) {
            logger.error(e.getMessage(), e);
        }
        return null;
    }

    @Override
    public Object getResources(final String jndiName) {
        Context context = null;
        Hashtable<Object, Object> properties = new Hashtable<Object, Object>();
        properties.put(Context.INITIAL_CONTEXT_FACTORY, "com.ibm.websphere.naming.WsnInitialContextFactory");
        properties.put(Context.PROVIDER_URL, AMPPropertyUtil.getProperty("PROVIDER_URL"));
        try {
            context = new InitialContext();
            return context.lookup(jndiName);

        } catch (NamingException e) {
            logger.error(e.getMessage(), e);
        }
        return null;
    }

    public static ServiceLocator getInstance() {
        if (serviceLocator == null) {
            synchronized (ServiceLocator.class) {
                if (null == serviceLocator)
                    serviceLocator = new ServiceLocator();
            }
        }
        return serviceLocator;
    }

}
于 2012-11-10T01:10:30.007 回答