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

Home > Search


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 = "com.ibm.websphere.naming.WsnInitialContextFactory";
  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);
      queueConn.start();
      QueueReceiver queueReceiver = queueSession.createReceiver ( queue );
      queueReceiver.close();
      queueSession.close();
      queueConn.close();
      jndi.close();
    }
    catch (NamingException e ) {
      e.printStackTrace();
    }
    catch (JMSException e) {
      e.printStackTrace();
    }
  }

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

}

 

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"
    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
  jmsGET jms = new jmsGET();

  // call the method
  jms.getMessage();
%>
foo.getMessage 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"
  • If you have access to the MQ server, use the display queue command to ensure CURDEPTH has not reached MAXDEPTH.

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




Comments