Bootstrap FreeKB - IBM MQ - Connect to IBM MQ in Java using WebSphere JNDI lookup
IBM MQ - Connect to IBM MQ in Java using WebSphere JNDI lookup

Updated:   |  IBM MQ articles

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

In WebSphere, create the connection factory or the queue connection factory and then restart the WebSphere deployment manager and also restart the WebSphere application servers the queue is scoped to. If you have access to the IBM MQ system, the following commands can be used to get the values needed for the connection factory.

  • 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

Download the latest javax.jms-api.jar or javax.servlet-api.jar files from https://mvnrepository.com and add the JARs to your Java Build Path

AVOID TROUBLE

There are two similar but unique classes that can be used. JNDI lookup can be done in JMS but cannot be done in MQQueueManager

  • com.ibm.mq.MQQueueManager (aka IBM MQ classes for Java) provided by com.ibm.mq.allclient-<version>.jar
  • javax.jms (aka IBM MQ classes for JMS) provided by javax.jms-api.jar

Add the following markup to the class. In this example, you would include the username and password being used to connect to IBM MQ in the Java app.

  • Replace 9809 with the bootstrap port being used by the WebSphere application server the application will be running on
  • Replace "my/jndi" with the JNDI that you created in WebSphere for the connection factory
package my.pkg;

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

// If you created a "Connection Factory" in WebSphere (not a Queue Connection Factory)
// then you will import Connection and ConnectionFactory
import javax.jms.Connection;
import javax.jms.ConnectionFactory;

// If you created a "Queue Connection Factory" in WebSphere (not a Connection Factory)
// then you will import QueueConnection and QueueConnectionFactory
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;

public class Main {
	public String main() throws InterruptedException, NamingException, JMSException {
            
        int bootstrap_port = 9809;
        String jndi = "my/jndi";
        String user = "john.doe";
        String password = "itsasecret";

        Properties props = new Properties();
        props.put ( Context.INITIAL_CONTEXT_FACTORY, "com.ibm.websphere.naming.WsnInitialContextFactory" );
        props.put ( Context.PROVIDER_URL, "corbaloc:iiop:localhost:"+bootstrap_port );
        InitialContext jndi = new InitialContext(props);
           
        // If you created a "Connection Factory" in WebSphere (not a Queue Connection Factory)
        // then you will use ConnectionFactory here 
        ConnectionFactory cf = (ConnectionFactory) jndi.lookup(jndi);
        Connection conn = cf.createConnection(username, password);

        // If you created a "Queue Connection Factory" in WebSphere (not a Connection Factory)
        // then you will use QueueConnectionFactory here 
        QueueConnectionFactory cf = (QueueConnectionFactory) jndi.lookup(jndi);
        QueueConnection conn = cf.createQueueConnection(username, password);
            
        conn.start();
        conn.close();

        return "Successfully connected to IBM MQ";
    }
}

 

Better yet, let's say you have a WebSphere connection factory that is using a J2C authentication alias (username / password). Here is how you can reference the connection factory and J2C authentication alias so that you do not need to include the username and password in your Java app.

package my.pkg;

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

// If you created a "Connection Factory" in WebSphere (not a Queue Connection Factory)
// then you will import Connection and ConnectionFactory
import javax.jms.Connection;
import javax.jms.ConnectionFactory;

// If you created a "Queue Connection Factory" in WebSphere (not a Connection Factory)
// then you will import QueueConnection and QueueConnectionFactory
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;

public class Main {
	public String main() throws NamingException, JMSException {
    	   
        int bootstrap_port = 9809;

        Properties props = new Properties();
        props.put ( Context.INITIAL_CONTEXT_FACTORY, "com.ibm.websphere.naming.WsnInitialContextFactory" );
        props.put ( Context.PROVIDER_URL, "corbaloc:iiop:localhost:"+bootstrap_port );
        InitialContext jndi = new InitialContext(props);
            
        ConnectionFactory cf = (ConnectionFactory) jndi.lookup("java:comp/env/my/jndi");
        Connection conn = cf.createConnection();
                           
        conn.start();
        conn.close();

    	return "Successfully connected to IBM MQ with connection factory my/jndi";
    }
}

 

In this scneario, you will need to include the following in your WEB-INF/web.xml file.

<resource-ref>
  <res-ref-name>my/jndi</res-ref-name>
  <res-type>javax.jms.ConnectionFactory</res-type>
  <res-auth>Container</res-auth>
</resource-ref>  

 

And the following in your WEB-INF/ibm-web-bnd.xml file.

<?xml version="1.0" encoding="UTF-8"?>
<web-bnd 
	xmlns="http://websphere.ibm.com/xml/ns/javaee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://websphere.ibm.com/xml/ns/javaee http://websphere.ibm.com/xml/ns/javaee/ibm-web-bnd_1_0.xsd"
	version="1.0">

    <resource-ref binding-name="my/jndi" name="<the name of the connection factory on WebSphere>">
      <authentication-alias name="<the name of the J2C authentication alias on WebSphere>"/>
    </resource-ref>

</web-bnd>

 




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