I’m writing an application that performs some actions on WebSphere MQ queues. This method in particular moves messages from one queue to another. I encounter no problems with the following code:
Context ctx = new InitialContext();
ConnectionFactory retrieveConnectionFactory = (ConnectionFactory) ctx.lookup(jndiPrefix + "/" + qcfName);
retrieveConnection = retrieveConnectionFactory.createConnection();
Queue source = (Queue) ctx.lookup(jndiPrefix + "/" + sourceQueue);
retrieveConnection.start();
retrieveSession = retrieveConnection.createSession(true, Session.AUTO_ACKNOWLEDGE);
String queueSearchSpec = buildSearchSpec(searchSpec);
consumer = retrieveSession.createConsumer(source, queueSearchSpec);
do {
qMessage = consumer.receiveNoWait();
if (qMessage != null) {
messages.add(qMessage);
numberOfMessages++;
}
} while (qMessage != null);
But, later down, I’m doing something very similar, except I receive a casting exception:
ConnectionFactory putConnectionFactory = (ConnectionFactory) ctx.lookup(jndiPrefix + "/" + destinationQueue);
// this is where the exception is occurring-- Error: com.ibm.mq.jms.MQQueue incompatible with javax.jms.ConnectionFactory
putConnection = putConnectionFactory.createConnection();
Queue destination = (Queue) ctx.lookup(jndiPrefix + "/" + destinationQueue);
putConnection.start();
putSession = putConnection.createSession(true, Session.AUTO_ACKNOWLEDGE);
MessageProducer producer = putSession.createProducer(destination);
for(int i = 0; i < messages.size(); i++)
{
producer.send(messages.get(i));
}
Note the comment in the second line of the above excerpt: this is where I’m receiving the exception message “com.ibm.mq.jms.MQQueue incompatible with javax.jms.ConnectionFactory.” I don’t know why I would get it here, but not above. I even tried explicitly declaring all the objects with a ‘javax.jms.‘ prefix, but I still received the same error.
I am running my application on WebSphere Application Server v7.0. Any help would be appreciated.
Stupid mistake on my part. In this line:
The second variable in ctx.lookup should have been the Queue Connection Factory, qcfName, like in the above code. I had included destinationQueue, which was referring to the Queue the messages were going to be moved to.