在尝试从JNDI提供程序检索连接工厂时,独立JMS客户端应用程序中以下ClassCastException
可能是什么原因?
Exception in thread "main" java.lang.ClassCastException: javax.naming.Reference cannot be cast to javax.jms.ConnectionFactory
以下是JMS客户端的缩写版本,仅包含其start()
和stop()
方法。方法start()
中的第一行发生异常,该行尝试从JNDI提供程序(远程LDAP服务器)检索连接工厂。 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);
}
}
虽然我不想透露context.properties
的具体内容,但它包含以下一般条目:
java.naming.factory.initial=...
java.naming.provider.url=...
java.naming.security.principal=...
java.naming.security.credentials=...
答案 0 :(得分:9)
事实证明问题是由于缺少JVM类路径的Tibco JMS jar文件tibjms.jar
。此jarfile实现了Tibco JMS协议,因为它缺少,JMS客户端无法从LDAP JNDI服务提供程序检索JMS连接工厂。