Java 为什么在 JNDI 中查找 JMS ConnectionFactory 时出现 ClassCastException?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2819612/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 13:14:58  来源:igfitidea点击:

Why ClassCastException on JMS ConnectionFactory lookup in JNDI?

javaexceptionclientjms

提问by Derek Mahar

What might be the cause of the following ClassCastExceptionin a standalone JMS client application when it attempts to retrieve a connection factory from the JNDI provider?

ClassCastException尝试从 JNDI 提供程序检索连接工厂时,独立 JMS 客户端应用程序中出现以下情况的原因可能是什么?

Exception in thread "main" java.lang.ClassCastException: javax.naming.Reference cannot be cast to javax.jms.ConnectionFactory

Here is an abbreviated version of the JMS client that includes only its start()and stop()methods. The exception occurs on the first line in method start()which attempts to retrieve the connection factory from the JNDI provider, a remote LDAP server. The JMS connection factory and destination objects are on a remote JMS server.

这是 JMS 客户端的缩写版本,仅包含其start()stop()方法。异常发生在方法的第一行,该方法start()尝试从远程 LDAP 服务器 JNDI 提供程序检索连接工厂。JMS 连接工厂和目标对象位于远程 JMS 服务器上。

class JmsClient {
    private ConnectionFactory connectionFactory;
    private Connection connection;
    private Session session;
    private MessageConsumer consumer;
    private Topic topic;

    public void stop() throws JMSException {
        consumer.close();
        session.close();
        connection.close();
    }

    public void start(Context context, String connectionFactoryName, String topicName) throws NamingException, JMSException {
        // ClassCastException occurs when retrieving connection factory.
        connectionFactory = (ConnectionFactory) context.lookup(connectionFactoryName);
        connection = connectionFactory.createConnection("username","password");
        session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        topic = (Topic) context.lookup(topicName);
        consumer = session.createConsumer(topic);
        connection.start();
    }

    private static Context getInitialContext() throws NamingException, IOException {
        String filename = "context.properties";
        Properties props = new Properties();
        props.load(new FileInputStream(filename));
        return new InitialContext(props);
    }
}

Though I prefer not to disclose the specific contents of context.properties, it contains the following general entries:

虽然我不想透露 的具体内容context.properties,但它包含以下一般条目:

java.naming.factory.initial=...
java.naming.provider.url=...
java.naming.security.principal=...
java.naming.security.credentials=...

采纳答案by Derek Mahar

Turns out that the problem was due to a Tibco JMS jarfile, tibjms.jar, being absent from the JVM classpath. This jarfile implements the Tibco JMS protocol and so because it was missing, the JMS client could not retrieve the JMS connection factory from the LDAP JNDI service provider.

事实证明,问题是由于tibjms.jarJVM 类路径中缺少Tibco JMS jarfile 。此 jarfile 实现了 Tibco JMS 协议,因此由于它丢失,JMS 客户端无法从 LDAP JNDI 服务提供者检索 JMS 连接工厂。