FreeKB - Connect to IBM MQ in Java Eclipse - JNDI lookup
Connect to IBM MQ in Java Eclipse - JNDI lookup

Home > Search


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 = "com.ibm.websphere.naming.WsnInitialContextFactory";
    private final String providerURL = "corbaloc:iiop:example.com:9809";
    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
            queueConn.start();
            System.out.println("queueConn.start() issued " + queueBindingName + " " + queuecfBindingName);
            System.out.println("sleeping for 10 seconds");
            Thread.sleep(10000); 
    	}
        catch (NamingException e ) {
        	e.printStackTrace();
        }
        catch (JMSException e) {
        	e.printStackTrace();
        	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);
    	      queueConn.close(); 		
            }
            catch (JMSException e) {
    	      System.out.println("Failed to close connection to queue");    	
    	      e.printStackTrace();			
            }
          }
        }
    }
}

 

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"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
  // create object
  jmsConnect conn = new jmsConnect();

  // call the method
  conn.jmsConnect();
%>
The conn.jmsConnect method has been called
</body>
</html>

 

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.
  CONN(0938739232847)
  EXTCONN(418D84A8B837E3938A983ABC9)
  TYPE(CONN)
  PID(12345)                TID(10)
  APPLDESC(IBM MQ Channel)  APPLTAG(bootstrap.WSPreLauncher)
  APPLTYPE(USER)            ASTATE(NONE)
  CHANNEL(CHANNEL01)        CLIENTID()
  CONNNAME(10.1.2.3)
  CONNOPTS(MQCNO_HANDLE_SHARE_BLOCK,MQCNO_SHARED_BINDING)
  USERID(john.doe)          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.



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 cdedd in the box below so that we can be sure you are a human.




Comments