为什么在调用getEnvironment()时InitialContext会抛出命名异常?

时间:2014-01-04 17:45:30

标签: java jndi solace

异常堆栈跟踪没有提供太多信息,只是

  

javax.naming.OperationNoSupportedException

public static Connection getConnection(Context ctx, String factoryName) throws JMSException, NamingException {
    Object objfac = null;
    try {
        objfac = ctx.lookup(factoryName);
    } catch (NoClassDefFoundError e) {
        throw new NamingException("Lookup failed: "+e.toString());
    }
    if (objfac instanceof javax.jms.ConnectionFactory) {
        @SuppressWarnings("unchecked") // The environment is supposed to use String keys only
        Map<String, Object> env = (Map<String, Object>)ctx.getEnvironment();
        if(env.containsKey(Context.SECURITY_PRINCIPAL)) {
            String username = (String)env.get(Context.SECURITY_PRINCIPAL);
            String password = (String)env.get(Context.SECURITY_CREDENTIALS);
            return ((javax.jms.ConnectionFactory) objfac).createConnection(username, password);                
        }
        else {
            return ((javax.jms.ConnectionFactory) objfac).createConnection();
        }
    }
    throw new NamingException("Expected javax.jms.ConnectionFactory, found "+(objfac != null ? objfac.getClass().getName(): "null"));
}

堆栈跟踪是:

  

javax.naming.OperationNotSupportedException       at com.solacesystems.jndi.SolJNDIInitialContextFactory $ SolJNDIInitialContextImpl.getEnvironment(SolJNDIInitialContextFactory.java:230)       在javax.naming.InitialContext.getEnvironment(InitialContext.java:544)       在javax.naming.InitialContext.getEnvironment(InitialContext.java:544)       at org.apache.jmeter.protocol.jms.Utils.getConnection(Utils.java:155)       在org.apache.jmeter.protocol.jms.client.Publisher。(Publisher.java:130)       在org.apache.jmeter.protocol.jms.sampler.PublisherSampler.initClient(PublisherSampler.java:135)       在org.apache.jmeter.protocol.jms.sampler.PublisherSampler.sample(PublisherSampler.java:154)       在org.apache.jmeter.protocol.jms.sampler.BaseJMSSampler.sample(BaseJMSSampler.java:80)       在org.apache.jmeter.threads.JMeterThread.process_sampler(JMeterThread.java:429)       在org.apache.jmeter.threads.JMeterThread.run(JMeterThread.java:257)       在java.lang.Thread.run(Thread.java:722)

1 个答案:

答案 0 :(得分:4)

您正在使用的JNDI实现不支持getEnvironment方法,这意味着该代码无法访问用于访问JMS服务器的任何凭据。这是我尝试修复的问题(如果不需要凭据,这可能仅适用于您的情况,我不确定):

if (objfac instanceof javax.jms.ConnectionFactory) {
    @SuppressWarnings("unchecked") // The environment is supposed to use String keys only
    Map<String, Object> env = null;
    try {
       env = (Map<String, Object>)ctx.getEnvironment();
    } catch (javax.naming.OperationNotSupportedException ex) {}
    if(env != null && env.containsKey(Context.SECURITY_PRINCIPAL)) {
        String username = (String)env.get(Context.SECURITY_PRINCIPAL);
        String password = (String)env.get(Context.SECURITY_CREDENTIALS);
        return ((javax.jms.ConnectionFactory) objfac).createConnection(username, password);                
    }
    else {
        return ((javax.jms.ConnectionFactory) objfac).createConnection();
    }
}