0

I am tring to connect to IBM websphere Client with a Java Programme the Following are the code:=

import com.ibm.mq.MQException;
import com.ibm.mq.MQGetMessageOptions;
import com.ibm.mq.MQMessage;
import com.ibm.mq.MQPutMessageOptions;
import com.ibm.mq.MQQueue;
import com.ibm.mq.MQQueueManager;


/**
 * Simple example program
 */
public class MQSample {

  // code identifier
  static final String sccsid = "@(#) MQMBID sn=p750-002-131001_DE su=_FswqMCqGEeOZ3ui-rZDONA pn=MQJavaSamples/wmqjava/MQSample.java";

  // define the name of the QueueManager
  private static final String qManager = "QM_ORANGE";
  // and define the name of the Queue
  private static final String qName = "SYSTEM.DEFAULT.LOCAL.QUEUE";
 // private static final String qName = "QM_APPLE";


  public static void main(String args[]) {
    try {

      System.out.println("Connecting to queue manager: " + qManager);
      MQQueueManager qMgr = new MQQueueManager(qManager);
      int openOptions =1| 16;
      System.out.println("Accessing queue: " + qName);
      MQQueue queue = qMgr.accessQueue(qName, openOptions);

      MQMessage msg = new MQMessage();
      msg.writeUTF("Hello, World!");
      MQPutMessageOptions pmo = new MQPutMessageOptions();
      System.out.println("Sending a message...");
      queue.put(msg, pmo);

      // Now get the message back again. First define a WebSphere MQ
      // message
      // to receive the data
    //  MQMessage rcvMessage = new MQMessage();

      // Specify default get message options
    //  MQGetMessageOptions gmo = new MQGetMessageOptions();

      // Get the message off the queue.
     // System.out.println("...and getting the message back again");
     // queue.get(rcvMessage, gmo);

      // And display the message text...
      //String msgText = rcvMessage.readUTF();
     // System.out.println("The message is: " + msgText);

      // Close the queue
      System.out.println("Closing the queue");
      queue.close();

      // Disconnect from the QueueManager
      System.out.println("Disconnecting from the Queue Manager");
      qMgr.disconnect();
      System.out.println("Done!");
    }
    catch (MQException ex) {
      System.out.println("A WebSphere MQ Error occured : Completion Code " + ex.completionCode
          + " Reason Code " + ex.reasonCode);
      ex.printStackTrace();
      for (Throwable t = ex.getCause(); t != null; t = t.getCause()) {
        System.out.println("... Caused by ");
        t.printStackTrace();
      }

    }
    catch (java.io.IOException ex) {
      System.out.println("An IOException occured whilst writing to the message buffer: " + ex);
    }
    return;
  }
}

but right now i am getting following error

Exception in thread "main" java.lang.UnsatisfiedLinkError: no mqjbnd05 in java.library.path
    at java.lang.ClassLoader.loadLibrary(Unknown Source)
    at java.lang.Runtime.loadLibrary0(Unknown Source)
    at java.lang.System.loadLibrary(Unknown Source)
    at com.ibm.mq.MQSESSION.loadLib(MQSESSION.java:872)
    at com.ibm.mq.server.MQSESSION$1.run(MQSESSION.java:228)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.ibm.mq.server.MQSESSION.<clinit>(MQSESSION.java:222)
    at com.ibm.mq.MQSESSIONServer.getMQSESSION(MQSESSIONServer.java:70)
    at com.ibm.mq.MQSESSION.getSession(MQSESSION.java:492)
    at com.ibm.mq.MQManagedConnectionJ11.<init>(MQManagedConnectionJ11.java:168)
    at com.ibm.mq.MQBindingsManagedConnectionFactoryJ11._createManagedConnection(MQBindingsManagedConnectionFactoryJ11.java:179)
    at com.ibm.mq.MQBindingsManagedConnectionFactoryJ11.createManagedConnection(MQBindingsManagedConnectionFactoryJ11.java:215)
    at com.ibm.mq.StoredManagedConnection.<init>(StoredManagedConnection.java:84)
    at com.ibm.mq.MQSimpleConnectionManager.allocateConnection(MQSimpleConnectionManager.java:168)
    at com.ibm.mq.MQQueueManagerFactory.obtainBaseMQQueueManager(MQQueueManagerFactory.java:772)
    at com.ibm.mq.MQQueueManagerFactory.procure(MQQueueManagerFactory.java:697)
    at com.ibm.mq.MQQueueManagerFactory.constructQueueManager(MQQueueManagerFactory.java:657)
    at com.ibm.mq.MQQueueManagerFactory.createQueueManager(MQQueueManagerFactory.java:153)
    at com.ibm.mq.MQQueueManager.<init>(MQQueueManager.java:451)
    at MQSample.main(MQSample.java:30)

when i refered about this everyone saying to put mqjbnd.dll in java.library i put that file in that path also still not working

JoshMc
  • 10,239
  • 2
  • 19
  • 38
Akshay Prabhu
  • 175
  • 1
  • 4
  • 18
  • Suggest using the 100% Java client using JMS, if possible. Much easier to code and deploy b/c there is no native library required. – Matt Pavlovich Oct 04 '16 at 13:55
  • @Matt. Bad answer. (1) Maybe them don't want to use JMS & (2) even if they used JMS, they would still need the vendor specific JMS JAR files. – Roger Oct 04 '16 at 16:03
  • @Roger7 1) it wasn't an answer, it was a comment. 2) They are having a problem with the native c-library which uses the non-JMS IBM java client lib. Using JMS+IBMMQ-jar is 100% java and simpler to code and deploy. Seen this 100 times... 99 times users move to the JMS libs. The JMS libs also provide backward compat to non-JMS WMQ clients on the other end using targetClient=0. – Matt Pavlovich Oct 04 '16 at 16:06
  • @Matt, his/her problem is not native vs pure JAR files. 99% sure they have incorrect code and connecting in 'bindings mode' when they should be connecting in 'client mode'. Just to blow your mind: if you use "JMS+IBMMQ-jar is 100% java" and connect in 'bindings mode' then it will ALWAYS use the mqjbnd05 DLL/shared library!!!!! – Roger Oct 04 '16 at 19:12
  • @Roger. Yep, I'm aware. Unless there is extreme performance use case (rarely the case), the WMQ-native API and binding-mode approach should be avoided as much as possible for code-reuse and standards conformance reasons. – Matt Pavlovich Oct 04 '16 at 19:34
  • Based on comments to my answer this appears to be a full install of MQ Advanced for Devs 7.5 in which case a local QMgr is likely. I'm proceeding with Q&A on that assumption. Hoping to get clarification shortly. – T.Rob Oct 05 '16 at 05:51
  • @Matt, if the queue manager & application are running on the same server then you should ALWAYS use bindings mode. There is a HUGE performance difference. You should go to MQ Technical Conference because there are lots of sessions on MQ internals. – Roger Oct 05 '16 at 16:53

2 Answers2

5

IBM MQ CLIENT java.lang.UnsatisfiedLinkError: no mqjbnd05 in java.library.path exception

Your title pretty much says it all. 'MQ Client' generally means that the queue manager is REMOTE to where you are running your application. But 'no mqjbnd05' means that you are attempting to connect to the queue manager in bindings mode, queue manager is running on the same server as your queue manager.

99% of the time an application gets that errors is because the application and queue manager are running on separate servers and the application is not specifying: channel name, hostname/IP address & port #.

Note: An application can connect in 2 ways to a queue manager: (1) client mode - meaning the application and queue manager are running on separate servers and the application is not specifying: channel name, hostname/IP address & port #.

(2) bindings mode - meaning the application and queue manager are running on the same servers (no networking information is specified).

Note: Don't use MQEnvironment class but rather put the connection information in a Hashtable and pass it to the MQQueueManager class. MQEnvironment is NOT thread safe.

Here's a working sample MQ application that will connect (client mode) to a remote queue manager:

import java.io.IOException;
import java.util.Hashtable;

import com.ibm.mq.*;
import com.ibm.mq.constants.CMQC;

/**
 * Java class to connect to MQ. Post and Retrieve messages.
 * 
 * Sample Command Line Parameters 
 *  -h 127.0.0.1 -p 1414 -c TEST.CHL -m MQA1 -q TEST.Q1 -u userid -x password
 */
public class MQClientTest
{
   private Hashtable<String, String> params = null;
   private Hashtable<String, Object> mqht = null;
   private String qManager;
   private String inputQName;

   /**
    * The constructor
    */
   public MQClientTest()
   {
      super();
   }

   /**
    * Make sure the required parameters are present.
    * 
    * @return true/false
    */
   private boolean allParamsPresent()
   {
      boolean b = params.containsKey("-h") && params.containsKey("-p") &&
                  params.containsKey("-c") && params.containsKey("-m") &&
                  params.containsKey("-u") && params.containsKey("-x") &&
                  params.containsKey("-q");
      if (b)
      {
         try
         {
            Integer.parseInt((String) params.get("-p"));
         }
         catch (NumberFormatException e)
         {
            b = false;
         }
      }

      return b;
   }

   /**
    * Extract the command-line parameters and initialize the MQ variables.
    * 
    * @param args
    * @throws IllegalArgumentException
    */
   private void init(String[] args) throws IllegalArgumentException
   {
      params = new Hashtable<String, String>();
      if (args.length > 0 && (args.length % 2) == 0)
      {
         for (int i = 0; i < args.length; i += 2)
         {
            params.put(args[i], args[i + 1]);
         }
      }
      else
      {
         throw new IllegalArgumentException();
      }

      if (allParamsPresent())
      {
         qManager = (String) params.get("-m");
         inputQName = (String) params.get("-q");

         mqht = new Hashtable<String, Object>();

         mqht.put(CMQC.CHANNEL_PROPERTY, params.get("-c"));
         mqht.put(CMQC.HOST_NAME_PROPERTY, params.get("-h"));

         try
         {
            mqht.put(CMQC.PORT_PROPERTY, new Integer(params.get("-p")));
         }
         catch (NumberFormatException e)
         {
            mqht.put(CMQC.PORT_PROPERTY, new Integer(1414));
         }

         mqht.put(CMQC.USER_ID_PROPERTY, params.get("-u"));
         mqht.put(CMQC.PASSWORD_PROPERTY, params.get("-x"));

         // I don't want to see MQ exceptions at the console.
         MQException.log = null;
      }
      else
      {
         throw new IllegalArgumentException();
      }
   }

   /**
    * Method to put then get a message to/from a queue.
    */
   public void putAndGetMessage()
   {
      MQQueueManager qMgr = null;
      MQQueue        queue = null;
      MQMessage      putMessage = null;
      MQMessage      getMessage = null;

      int openOptions = CMQC.MQOO_INPUT_AS_Q_DEF | CMQC.MQOO_OUTPUT + CMQC.MQOO_FAIL_IF_QUIESCING;

      MQGetMessageOptions gmo = new MQGetMessageOptions();
      gmo.options = CMQC.MQGMO_NO_WAIT + CMQC.MQGMO_FAIL_IF_QUIESCING;

      MQPutMessageOptions pmo = new MQPutMessageOptions();
      pmo.options = CMQC.MQPMO_FAIL_IF_QUIESCING;

      String msg = "Hello World, WelCome to MQ.";

      try
      {
         qMgr = new MQQueueManager(qManager, mqht);
         queue = qMgr.accessQueue(inputQName, openOptions);

         putMessage = new MQMessage();
         putMessage.writeUTF(msg);

         // put the message on the queue
         queue.put(putMessage, pmo);

         System.out.println("Message is put on MQ.");

         // get message from MQ.
         getMessage = new MQMessage();
         // assign message id to get message.
         getMessage.messageId = putMessage.messageId;

         /*
          * Tell the queue manager that we want a message with a specific MsgID.
          */
         gmo.matchOptions = CMQC.MQMO_MATCH_MSG_ID;

         // get message options.
         queue.get(getMessage, gmo);

         String retreivedMsg = getMessage.readUTF();
         System.out.println("Message got from MQ: " + retreivedMsg);
      }
      catch (MQException e)
      {
         System.err.println("CC=" + e.completionCode + " : RC=" + e.reasonCode);
      }
      catch (IOException e)
      {
         e.printStackTrace();
      }
      finally
      {
         try
         {
            if (queue != null)
               queue.close();
         }
         catch (MQException e)
         {
            System.err.println("MQCLOSE CC=" + e.completionCode + " : RC="
                  + e.reasonCode);
         }

         try
         {
            if (qMgr != null)
               qMgr.disconnect();
         }
         catch (MQException e2)
         {
            System.err.println("MQDISC CC=" + e2.completionCode + " : RC="
                  + e2.reasonCode);
         }
      }
   }

   public static void main(String[] args)
   {
      System.out.println("Processing Main...");
      MQClientTest clientTest = new MQClientTest();

      try
      {
         // initialize MQ.
         clientTest.init(args);
         // put and retrieve message from MQ.
         clientTest.putAndGetMessage();
      }
      catch (IllegalArgumentException e)
      {
         System.out.println("Usage: java MQClientTest -h host -p port -c channel -m QueueManagerName -q QueueName -u userid -x password");
         System.exit(1);
      }

      System.out.println("Done!");
   }
}
Roger
  • 7,062
  • 13
  • 20
1

There is nothing in the question to indicate which version of MQ client is being used, how it was installed, whether the IBM-provided code works, or whether the environment was set correctly. That makes this more of a "how do I configure and test MQ client?" kind of question so I'll answer it that way.

Back-level MQ clients were always installed using IBM's full-client install media. Newer clients can be installed using IBM's Java-only install media. It is always recommended to use one of these methods.

The one thing to not do (which coincidentally is the thing done most often) is to simply grab the jar files from the MQ Server installation. The reasons for this include:

  • The IBM installer lays down a known-good set of files.
  • Maintenance can be applied to an installation made using IBM's installer.
  • When using IBM's install methods, things like trace directories and the location of the MQClient.ini file are predictable.

So first thing is to make sure you are running from the latest IBM-provided full client or Java-only install media. Alternatively, install IBM MQ Advanced for Developers which delivers a full MQ install to the desktop, including all the client support. MQ Advanced for Developers is free for individual use.

Prior to launching code, set the environment. See:
Environment variables relevant to IBM MQ classes for Java
Environment variables used by IBM MQ classes for JMS

Per the docs:

On Windows, all the environment variables are set automatically during installation. On any other platform, you must set them yourself. On a UNIX system, you can use the script setjmsenv (if you are using a 32-bit JVM) or setjmsenv64 (if you are using a 64-bit JVM) to set the environment variables. On AIX, HP-UX, Linux, and Solaris, these scripts are in the MQ_INSTALLATION_PATH/java/bin directory.

IBM provides lots of sample code. On *nix systems this is in /opt/mqm/samp/. On windows it's in the [MQ install directory]\tools. If the full client is installed try the compiled C code first, like amqsgetc. This establishes whether basic connectivity is in place. Once you know the channel connectivity works, try the Java or JMS samples.

Getting back to the original post, before we can help we'd need to know which of the above steps had already been completed and something about the configuration. Otherwise what you get back generally leads you down a path to a setup in which configuration is almost guaranteed to be hosed: "Try adding this library here," or "try mucking about with your CLASSPATH like this..." Such trial-and-error approaches often work but are unsupportable and lead to problems over time.

T.Rob
  • 31,522
  • 9
  • 59
  • 103
  • i am using WebSphere Explorer 7.5 – Akshay Prabhu Oct 05 '16 at 04:45
  • i am using mqadv_dev75_windows. – Akshay Prabhu Oct 05 '16 at 05:00
  • i had done all the steps mentioned by you in this answer complained c code also that works ok – Akshay Prabhu Oct 05 '16 at 05:09
  • Awesome! The original post shows us the code but the library problem has to do with install config and how the code is launched. If the install config stuff is sorted, you might want to update the question with the launcher script code. Then we can see where it runs setjmsenv and the output of that prior to attempting to execute the classes. Also, since this is MQADV4DEV, is the QMgr local? – T.Rob Oct 05 '16 at 05:48
  • So you are Saying it is not code error its configuration error .If it is configuration error then how can i configure client using websphere explorer 7.5 which connects to a queue manger in sever? – Akshay Prabhu Oct 05 '16 at 08:55
  • i followed steps on this link for configureing Client http://www.ibm.com/support/knowledgecenter/en/SSFKSJ_7.5.0/com.ibm.mq.ins.doc/q009350_.htm – Akshay Prabhu Oct 05 '16 at 08:58
  • MQ Explorer comes with its own embedded MQ client which has nothing to do with the client used to connect with stand-alone Java or C code. If you are having problems with Explorer too, you probably need to completely uninstall and reinstall. In which case, switch to MQ 8 or MQ 9 while you are at it. End of Service is announced on MQ v7.5. – T.Rob Oct 05 '16 at 13:34