FreeKB - IBM MQ connect in Java Eclipse via JNDI lookup
IBM MQ - connect in Java Eclipse via JNDI lookup

There are different ways to connect to IBM MQ. One way is to define the hostname, port, username, password, queue manager, and queue in the Java application, so that the Java application makes a direct connection to MQ. Another option is to define the connection parameters in WebSphere, and to then get the connection parameters from WebSphere through a JNDI lookup. This article describes how to make the connection in this way.

In WebSphere, create the queue JNDI and the queue connection factory JNDI. Let's say the JNDI's are jms/queue01 and jms/queue01cf.

Create a Dynamic Web Project in Eclipse. Let's say the name of the project is jmsDemo. At jmsDemo > Java Resources > src, create a new package. For the sake of this tutorial, let's name the package com.jms.demo.


In the package, create a class called queueConnection.


Add the following markup to the class. In the variables section, enter your hostname, port, queue JNDI and queue connection factory JNDI.

package com.jms.demo;

import java.util.Properties;
import javax.jms.JMSException;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

public class queueConnection {
	private final String initialContextFactoryName = "";
    private final String providerURL = "";
    private final String queueBindingName = "jms/queue01";
    private final String queuecfBindingName = "jms/queue01cf";

    public void jmsConnect() {
        QueueConnection queueConn = qcf.createQueueConnection();
    	try {
            // variables
            Properties env = new Properties ();
            env.put ( Context.INITIAL_CONTEXT_FACTORY, initialContextFactoryName );
            env.put ( Context.PROVIDER_URL, providerURL );
            InitialContext jndi = new InitialContext (env);
            QueueConnectionFactory qcf = (QueueConnectionFactory) jndi.lookup(queuecfBindingName);            

            // create the connection
            System.out.println("queueConn.start() issued " + queueBindingName + " " + queuecfBindingName);
            System.out.println("sleeping for 10 seconds");
        catch (NamingException e ) {
        catch (JMSException e) {
        	System.out.println("Failed to connect to queue " + " " + queueBindingName + queuecfBindingName);
        finally {
          if (queueConn == null) {
            System.out.println("queueConn equals null" + queueConn);	
          if (queueConn != null) {
            System.out.println("queueConn does not equal null" + queueConn);
            try {
              System.out.println("queueConn.close() issued " + queueBindingName + " " + queuecfBindingName);
            catch (JMSException e) {
    	      System.out.println("Failed to close connection to queue");    	


Create index.JSP to call the jmsConnect() method.


Add the following markup to index.jsp, so that the jmsConnect() method is called when navigating to index.jsp.

<%@page import="com.jms.demo.queueConnection"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
  // create object
  jmsConnect conn = new jmsConnect();

  // call the method
The conn.jmsConnect method has been called


Export jmsDemo to a WAR and deploy the WAR to your WebSphere application server.

  • If there is a firewall between the application server and dmgr server, ensure the BOOTSTRAP_ADDRESS and CSIV2_SSL_SERVERAUTH_LISTENER_ADDRESS ports are open in the firewall.
  • If you have access to the MQ server, use the dspmq command to ensure the status of the queue manager is "Running"
  • If you have access to the MQ server, use the display chstatus command to ensure the status of the channel is "RUNNING"

Once deployed, navigate to the index.jsp page, and "The conn.jmsConnect method has been called" should be displayed.


Since System.out.println was used in the class, the HPEL or SystemOut.log of the application server running the application should contain either a failed to connect or successfully connect message.

Failed to connect to queue jms/queue01 jms/queue01cf

Successfully connected to queue jms/queue01 jms/queue01cf


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.
  PID(12345)                TID(10)
  APPLDESC(IBM MQ Channel)  APPLTAG(bootstrap.WSPreLauncher)
  USERID(john.doe)          UOWLOG()
  UOWSTDA()                 UOWSTTI()
  UOWLOGDA()                UOWLOGTI()
  QMURID(0.0)               UOWSTATE(NONE)


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

Add a Comment

We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.

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


Web design by yours truely - me, myself, and I   |   |