What could be the reason for the following ClassCastExceptionin a standalone JMS client application when it tries to get a factory connection with a JNDI provider?
Exception in thread "main" java.lang.ClassCastException: javax.naming.Reference cannot be cast to javax.jms.ConnectionFactory
The following is an abbreviated version of the JMS client, which includes only its methods start()and stop(). An exception occurs on the first line in a method start()that tries to get a factory connection to the JNDI provider, the remote LDAP server. The JMS factory connection and targets are located on the remote JMS server.
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 {
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);
}
}
Although I do not want to disclose specific content context.properties, it contains the following general entries:
java.naming.factory.initial=...
java.naming.provider.url=...
java.naming.security.principal=...
java.naming.security.credentials=...