Writing a standalone JMS Java Client to connect to Websphere MQ 6.0 via Websphere Application Server 6.1

Somehow, when I tried to write a standalone client I kept coming across the following exception
java.lang.ClassCastException: javax.naming.Reference incompatible with javax.jms.ConnectionFactory “.

I followed intructions from here to setup the Queue, after which I created a simple java client to post a message to the Queue:

Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, “com.ibm.websphere.naming.WsnInitialContextFactory”);
env.put(Context.PROVIDER_URL, “iiop://localhost:2809″);

Context jndiContext = null;
try {
jndiContext = new InitialContext(env);
} catch (NamingException e) {

System.out.println(“Could not create JNDI API context: ” + e.toString());
System.exit(1);
}

ConnectionFactory connectionFactory = (ConnectionFactory)  jndiContext.lookup(“jms/JMSExampleConnectionFactory”);
Connection qConn = connectionFactory.createConnection();
Session qSession = qConn.createSession(false, Session.AUTO_ACKNOWLEDGE);
Queue q = (Queue) jndiContext.lookup(“jms/JMSExampleQueue”);

MessageProducer producer = qSession.createProducer(q);
TextMessage message = qSession.createTextMessage();
message.setText(“test message”);
producer.send(message);

producer.close();
qSession.close();
qConn.close();

Even after switching to the IBM JRE and adding the relevant Jars (as suggested in most places.) the program kept throwing the ClassCast Exception. Just in case you are wondering, the relevant Jars ( found in <Websphere Installation Path>/runtimes/ and <Websphere Installation Path>/lib/WMQ/java/lib/ ) are:

com.ibm.mq.jar
com.ibm.mqjms.jar
com.ibm.ws.webservices.thinclient_6.1.0.jar
dhbcore.jar
ws_runtime.jar

After googling and reading through lot of forum threads I finally stumbled on the solution. To resolve this you also need to add added the <Websphere Installation Path>/plugins/com.ibm.ws.runtime_6.1.0.jar to the classpath.  Seems simple enough, but it took me ages to find. Hence the post.

8 comments so far

  1. Obaid Dehlavi on

    Thank you!

  2. Tyrael on

    Did you switch your JRE on the Server or on the Client?
    I tried everything on the client and finally gave up!!! I’m running things as a servlet and it looks to be working fine..

  3. Swen on

    I switched the JRE on the Client. The server is already a IBM AIX box.

  4. Scott on

    Thank you. JMS frustration gone.

  5. Leo on

    I had the same problem in MQ 7.0.1 and solved it by adding the following jars in the CLASSPATH of my Java client:

    com.ibm.mq.jar
    com.ibm.mqjms.jar
    com.ibm.mq.jmqi.jar
    dhbcore.jar
    fscontext.jar
    providerutil.jar

    Hope this helps someone someday! :-)

  6. anu on

    Hi,
    I am trying to use JNDI in my standalone java client to connect to the websphere MQ JMS provider.We don’t have any JNDI on JMS provider side.
    So I was wondering from where do we get this connectionfactory name.
    I am using Queue and Topic names directly and look these up successfully but not sure How I should lookup for connection factory.

    Thanks
    Anu

  7. uk directories on

    Great items from you, man. I’ve understand your stuff prior to and you’re just extremely great. I really like what you have received right here, certainly like what you are stating and the way wherein you assert it. You make it entertaining and you continue to take care of to stay it sensible. I can not wait to read far more from you. This is actually a tremendous web site.

  8. E.Bos on

    Thanks for this great post, it even helped me 3 years later to get this to work on WAS 7.0. The directory then is:
    /SDPShared\plugins\com.ibm.rational.ttt.common.models.core_8.1.50.v20110304_1001\lib\jmslibs

    This is for an Websphere BPM 7.5 installation.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.