FreeKB - IBM MQ GET a JMS message in a queue in Java Eclipse JNDI lookup
IBM MQ - GET a JMS message in a queue in Java Eclipse JNDI lookup

Before writing an application to GET/PUT messages on a queue, you probably want to write an application to connect to MQ.

There are different ways to GET/PUT messages on a queue in a Java application. 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. Refer to this article to make the connection in this way. 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 jmsGET.


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.jms.Queue;
import javax.jms.QueueReceive;
import javax.jms.QueueSession;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.naming.InitialContext;
import javax.naming.Context;
import javax.naming.NamingException;

// class
public class jmsGET {

  // variables
  private final String initialContextFactoryName = "";
  private final String providerURL = "corbaloc:iiop:<hostname of your dmgr>:<BOOTSTRAP_ADDRESS port of your dmgr>";
  private final String queueBindingName = "jms/queue01";
  private final String queuecfBindingName = "jms/queue01cf";
  // create getMessage() method
  private void getMessage() {
    try {
      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);
      Queue queue = (Queue)jndi.lookup(queueBindingName);
      QueueConnection queueConn = qcf.createQueueConnection();
      QueueSession queueSession = queueConn.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
      QueueReceiver queueReceiver = queueSession.createReceiver ( queue );
    catch (NamingException e ) {
    catch (JMSException e) {

  // invoke the getMessage() method
  public static void main(String[] args) {
    jmsGET jms = new jmsGET();



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


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

<%@page import="com.jms.demo.jmsGET"%>
<%@ 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
  jmsGET jms = new jmsGET();

  // call the method
foo.getMessage 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:

Once deployed, navigate to the index.jsp page, and "foo.getMessage method has been called" should be displayed.


This should GET the message Hello World into the queue. To verify the message was placed in the queue, check the messaging server to verify the message has been removed from the queue. For example, if using an IBM MQ server, you could use the amqsbcg command to verify the message is no longer in the queue.

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


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