Bootstrap FreeKB - IBM MQ - Connect to IBM MQ in Java Eclipse using MQQueueManager and MQConstants
IBM MQ - Connect to IBM MQ in Java Eclipse using MQQueueManager and MQConstants

Updated:   |  IBM MQ articles

There are different ways to connect to IBM MQ in a Java application.

Download the latest com.ibm.mq.allclient.jar or com.ibm.mq.jmqi.jar (Java Message Queueing Interface) and javax.jms-api.jar or javax.servlet-api.jar files from https://mvnrepository.com and add the JARs to your Java Build Path

If you have access to the IBM MQ system, the following commands can be used.

  • Use the hostname command to determine the hostname of the IBM MQ system
  • Use the dspmq command to determine the name of the queue manager
  • Use the display lsstatus command to determine the port
  • Use the display channel command to determine the name of the channel and the SSL cipher

Add the following markup to the class. TLS CipherSpecs and CipherSuites in IBM MQ classes for JMS lists the ciphers supported by IBM MQ

import java.util.Hashtable;
import com.ibm.mq.MQException;
import com.ibm.mq.MQQueueManager;
import com.ibm.mq.constants.MQConstants;
public class Main {

  private static final String HOST       = "your ibm mq server or cluster hostname";
  private static final String QMGR       = "your ibm mq queue_manager";
  private static final String CHANNEL    = "your ibm mq channel";
  private static final String USERNAME   = "your ibm mq username";
  private static final String myPASSWORD = "your ibm mq password";
  private static final int PORT          = your ibm mq queue manager port;

  public static void main(String[] args) throws MQException {

    Hashtable<String, Object> properties = new Hashtable<String, Object>();
    properties.put(MQConstants.HOST_NAME_PROPERTY, HOST);
    properties.put(MQConstants.PORT_PROPERTY, PORT); 
    properties.put(MQConstants.CHANNEL_PROPERTY, CHANNEL); 
    properties.put(MQConstants.USER_ID_PROPERTY, USERNAME);
    properties.put(MQConstants.PASSWORD_PROPERTY, myPASSWORD);

    // This can only be used if you are using the allclient JAR (com.ibm.mq.allclient.jar)
    // This cannot be used if you are using the jmqi JAR (com.ibm.mq.jmqi.jar)
    properties.put(MQConstants.APPNAME_PROPERTY, "my app");

    // If USE_MQCSP_AUTHENTICATION_PROPERTY is set to true, then the username and password in USER_ID_PROPERTY and PASSWORD_PROPERTY will be used.
    // MQCSP stands for MQ Connection Security Parameters.
    // With false, something along the lines of a "UserID flow" and "UID header" are used (I'm not exactly sure what these are)
    // USE_MQCSP_AUTHENTICATION_PROPERTY can be used if you are using the allclient JAR (com.ibm.mq.allclient.jar)
    // I don't think that USE_MQCSP_AUTHENTICATION_PROPERTY can be used if you are using the jmqi JAR (com.ibm.mq.jmqi.jar)
    properties.put(MQConstants.USE_MQCSP_AUTHENTICATION_PROPERTY, true);

    // truststore is only needed when running the application in Eclipse
    // the truststore file will need the public certificate being used by IBM MQ for SSL
    // truststore should not be needed when running the application on an application server
    // as long as the truststore being used by the application server has the public certificate being used by IBM MQ for SSL
    // If running the application on a WebSphere application server
    // The <base websphere install directory>/java/jre/lib/security/cacerts will need the public certificate being used by IBM MQ for SSL
    // System.clearProperty("javax.net.ssl.trustStore");
    // System.clearProperty("javax.net.ssl.trustStoreType");
    // System.clearProperty("javax.net.ssl.trustStorePassword");
    // System.setProperty("javax.net.ssl.trustStore", "C:\\Users\\john.doe\\cacerts");
    // System.setProperty("javax.net.ssl.trustStoreType", "pkcs12");
    // System.setProperty("javax.net.ssl.trustStorePassword", "itsasecret");

    // If com.ibm.mq.cfg.useIBMCipherMappings is not being used or if com.ibm.mq.cfg.useIBMCipherMappings is set to true, then the IBM cipher must be used (the ciphers beginning with SSL)
    // If com.ibm.mq.cfg.useIBMCipherMappings is set to false, then the Oracle cipher must be used (the ciphers beginning with TLS)
    // System.setProperty("com.ibm.mq.cfg.useIBMCipherMappings", "false");
    // properties.put(MQConstants.SSL_CIPHER_SUITE_PROPERTY, "TLS_RSA_WITH_AES_256_CBC_SHA256");
    properties.put(MQConstants.SSL_CIPHER_SUITE_PROPERTY, "SSL_RSA_WITH_AES_256_CBC_SHA256");

    try {
      MQQueueManager queueManagerConnection = new MQQueueManager("MANAGER01", properties);
      if (queueManagerConnection.isConnected()) {
        System.out.println("Successfully connected to CHANNEL01");
      }
      else {
        System.out.println("Failed to connect to CHANNEL01");
      }
      System.out.println(queueManagerConnection);
      queueManagerConnection.disconnect();
      System.out.println("Successfully disconnected from queue manager MANAGER01");
    } catch (MQException e) {
      e.printStackTrace();
    }
  }
}

 

Run the application in Eclipse and if the connection to IBM MQ is successful, something like this should be displayed.

{
 "ConnectionId":"414D51434D5148554243413120202020CBE2016317410C23",
 "ObjectId":"com.ibm.mq.MQQueueManager@6ec8211c",
 "Port":8879,
 "Channel":"CHANNEL01",
 "ConnectionMode":"MQSeries Client",
 "ResolvedQueueManager":"MANAGER01",
 "Host":"mq1.example.com",
 "QueueManager":"MANAGER01"
}

 

Likewise, on the MQ server, the display conn command can be used to determine if there is an active connection to the channel. In this example, john.doe has an active connection. The connection remains active for 10 seconds and is then closed.

~]# echo "display conn (*) where (CHANNEL eq CHANNEL01) all" | runmqsc MANAGER01
AMQ8276I: Display Connection details.
  CONN(0938739232847)
  EXTCONN(418D84A8B837E3938A983ABC9)
  TYPE(CONN)
  PID(12345)                TID(10)
  APPLDESC(IBM MQ Channel)  APPLTAG(my app)
  APPLTYPE(USER)            ASTATE(NONE)
  CHANNEL(CHANNEL01)        CLIENTID()
  CONNNAME(10.1.2.3)
  CONNOPTS(MQCNO_HANDLE_SHARE_BLOCK,MQCNO_SHARED_BINDING)
  USERID()                  UOWLOG()
  UOWSTDA()                 UOWSTTI()
  UOWLOGDA()                UOWLOGTI()
  URTYPE(QMGR)
  EXTURID(XA_FORMATID[] XA_GTRID[] XA_BQUAL[])
  QMURID(0.0)               UOWSTATE(NONE)

 

You should now be able to build in the logic to PUT and GET messages.




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter 0f98e7 in the box below so that we can be sure you are a human.