
There are different ways to connect to IBM MQ in a Java application.
- Connect to IBM MQ in Java using JMS
- Connect to IBM MQ in Java using MQQueueManager
- Connect to IBM MQ in Java using WebSphere JNDI lookup (this article)
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. You will also need the com.ibm.ws.ejb.thinclient_<version>.jar which you can get from your WebSphere system, for example at /opt/WebSphere/AppServer/runtimes/com.ibm.ws.ejb.thinclient_9.0.jar on a Linux system.
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 {
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:9809");
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("my/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("my/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 {
Properties props = new Properties();
props.put ( Context.INITIAL_CONTEXT_FACTORY, "com.ibm.websphere.naming.WsnInitialContextFactory" );
props.put ( Context.PROVIDER_URL, "corbaloc:iiop:localhost:9809");
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>
Here is a bit of a more practical example with try / catch error handling.
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 Connect {
public String main() throws NamingException, JMSException {
Properties props = new Properties();
props.put ( Context.INITIAL_CONTEXT_FACTORY, "com.ibm.websphere.naming.WsnInitialContextFactory" );
props.put ( Context.PROVIDER_URL, "corbaloc:iiop:localhost:9809");
String message = null;
InitialContext jndi = null;
try {
jndi = new InitialContext(props);
} catch (NamingException e) {
message = "Got the following NamingException when trying new InitialContext(props) :"+e;
System.out.println(message);
return message;
}
System.out.println("All good - new InitialContext(props) did not catch NamingException");
System.out.println("jndi = "+jndi);
ConnectionFactory cf = null;
try {
cf = (ConnectionFactory) jndi.lookup("java:comp/env/jms/my/jndi");
} catch (NamingException e) {
message = "Got the following NamingException when trying jndi.lookup :"+e;
System.out.println(message);
return message;
}
System.out.println("All good - jndi.lookup did not catch NamingException");
System.out.println("cf = "+cf);
Connection conn = null;
try {
conn = cf.createConnection();
} catch (JMSException e) {
message = "Got the following JMSException when trying cf.createConnection() :"+e;
System.out.println(message);
return message;
}
System.out.println("All good - cf.createConnection() did not catch JMSException");
System.out.println("conn = "+conn);
try {
conn.start();
} catch (JMSException e) {
message = "Got the following JMSException when trying conn.start() :"+e;
System.out.println(message);
return message;
}
System.out.println("All good - conn.start() did not catch JMSException");
try {
conn.close();
} catch (JMSException e) {
message = "Got the following JMSException when trying conn.close() :"+e;
System.out.println(message);
return message;
}
System.out.println("All good - conn.close() did not catch JMSException");
return "Made it to the bottom of the main method in the Connect class";
}
}
Did you find this article helpful?
If so, consider buying me a coffee over at