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 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.

About these ads

29 thoughts on “Writing a standalone JMS Java Client to connect to Websphere MQ 6.0 via Websphere Application Server 6.1

  1. 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..

  2. 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! :-)

  3. 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

  4. 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.

  5. 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.

  6. Hi,
    I would like to say thank you for your posting, which finally helped me to establish connection from Cast Iron to Websphere App Server (7.0) – Message Queue. (in order for Cast Iron to post or pull messages from the Message Queue)
    I needed to include the following jars:
    com.ibm.mq.jar
    com.ibm.mqjms.jar
    dhbcore.jar
    com.ibm.ws.webservices.thinclient_7.0.0.jar
    com.ibm.ws.runtime.jar
    com.ibm.ws.sib.client.thin.jms_7.0.0.jar

    Katrin

  7. Hi , I have a question on including the JMS provider specific jars included in the code. I just want to develop a generic Java agent to talk to a JMS provider

    New to JMS and thought I could code with just standard Java SE (for JNDI) and javax,jms (not included in SE library), I could connect to ANY JMS provider.
    I have not tried IBM MQ, but I tried on OPEN JMS. The code compiles OK with just the jms1-1.jar (containing javax.jms). I use the following to pass the connection properties

    Hashtable properties = new Hashtable();
    properties.put(Context.INITIAL_CONTEXT_FACTORY,
    “org.exolab.jms.jndi.InitialContextFactory”);
    properties.put(Context.PROVIDER_URL, “tcp://localhost:3035/”);

    Looks pretty typical. Until I try to send a message to the OPEN JMS, I got javax.naming.NoInitialContextException: Cannot instantiate class: org.exolab.jms.jndi.InitialContextFactory. It means the JMS provider cannot find the class. The problem get resolved after I add ALL the jar files from the LIB folder. It make no difference in compiling, but now I can access the JMS provider.
    After seen Katrin’s response, It seems that a Java client DO need to include vendor specific jars to be able to connect to that provider, even though including those jars are the only thing a Java developer needs to do.

    Anyone can confirm on with that?

    It seems there is NO way to avoid it. I initial try to stay vendor neutral, but now I have to consider the specific jars.

  8. I loved as much as you will receive carried out right here.
    The sketch is tasteful, your authored material stylish.

    nonetheless, you command get got an edginess over that you
    wish be delivering the following. unwell unquestionably
    come further formerly again as exactly the same nearly very often inside case you shield
    this increase.

  9. My spouse and I stumbled over here from a different web address and thought I may as well check
    things out. I like what I see so now i’m following you. Look forward to looking into your web page repeatedly.

  10. Greetings from California! I’m bored at work so I decided to check out
    your site on my iphone during lunch break. I enjoy the information you present here and can’t
    wait to take a look when I get home. I’m amazed at how quick
    your blog loaded on my cell phone .. I’m not even using WIFI, just
    3G .. Anyways, amazing blog!

  11. I seriously love your site.. Excellent colors & theme.
    Did you create this amazing site yourself? Please reply back as I’m looking to
    create my own personal website and want to find out where you got this from or just what the theme is
    named. Kudos!Meez cheats november 2013

  12. Use a gutter guard to prevent ice from building up inside, as the access weight could break it.

    A good place to start, before ever climbing a ladder (which, by the way, make sure to always
    use precautions when climbing a ladder and have a second person spot you if possible), you can
    give your roof a once over from the ground. Of course, we have
    all heard the stories of people who try to do their own work and
    end up spending more money in the long run.

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 )

Google+ photo

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

Connecting to %s