Bootstrap FreeKB - IBM MQ - Connect to IBM MQ in Java Eclipse using JMS and WMQConstants
IBM MQ - Connect to IBM MQ in Java Eclipse using JMS and WMQConstants

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 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, issue the following commands to determine the hostname, channel, queue manager and port.

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

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 javax.jms.Connection;
import javax.jms.JMSException;
import com.ibm.msg.client.jms.JmsConnectionFactory;
import com.ibm.msg.client.jms.JmsFactoryFactory;
import com.ibm.msg.client.wmq.WMQConstants;

public class Connect {
	
  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 JMSException {

    JmsFactoryFactory ff    = JmsFactoryFactory.getInstance(WMQConstants.WMQ_PROVIDER);
    JmsConnectionFactory cf = ff.createConnectionFactory();
  
    cf.setStringProperty (WMQConstants.WMQ_HOST_NAME, HOST);
    cf.setIntProperty    (WMQConstants.WMQ_PORT, PORT);
    cf.setStringProperty (WMQConstants.WMQ_CHANNEL, CHANNEL);
    cf.setIntProperty    (WMQConstants.WMQ_CONNECTION_MODE, WMQConstants.WMQ_CM_CLIENT);
    cf.setStringProperty (WMQConstants.WMQ_QUEUE_MANAGER, QMGR);
    cf.setStringProperty (WMQConstants.USERID, USERNAME);
    cf.setStringProperty (WMQConstants.PASSWORD, myPASSWORD);
    cf.setStringProperty (WMQConstants.WMQ_APPLICATIONNAME, "foo app");

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

    // 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");
    // cf.setStringProperty (WMQConstants.WMQ_SSL_CIPHER_SUITE, "TLS_RSA_WITH_AES_256_CBC_SHA256");
    cf.setStringProperty (WMQConstants.WMQ_SSL_CIPHER_SUITE, "SSL_RSA_WITH_AES_256_CBC_SHA256");

    try {
      Connection conn = cf.createConnection(USERNAME, myPASSWORD);
      conn.start();
    } 
    catch (JMSException e) {
      e.printStackTrace();
    }
    finally {
      if (cf == null) {
        System.out.println("Failed to create the connection factory");	
      }
      else if (conn == null) {
        System.out.println("Failed to create the connection");	
      }
      else {
        conn = cf.createConnection(USERNAME, myPASSWORD);
        if (conn == null) {
          System.out.println("Failed to create the connection object");					  
        }
        else {
          conn.start();
          System.out.println("Successfully started connection to channel " + CHANNEL);
          conn.close();
        }
      }
    }
  }
}

 

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

Successfully connected to IBM MQ channel channel1

 

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.

AMQ8276I: Display Connection details.
  CONN(0938739232847)
  EXTCONN(418D84A8B837E3938A983ABC9)
  TYPE(CONN)
  PID(12345)                TID(10)
  APPLDESC(IBM MQ Channel)  APPLTAG(bootstrap.WSPreLauncher)
  APPLTYPE(USER)            ASTATE(NONE)
  CHANNEL(CHANNEL1)         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 2e4a41 in the box below so that we can be sure you are a human.