FreeKB - PUT a JMS message in a queue in Java Eclipse - JNDI lookup
PUT 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 jmsPUT.

 

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.QueueSender;
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 jmsPUT {

  // 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 the sendMessage() method
  private void sendMessage() {
    try {
      Properties env = new Properties ();
      env.put ( Context.INITIAL_CONTEXT_FACTORY, initialContextFactoryName );
      env.put ( Context.PROVIDER_URL, providerURL );
      InitialContext jndi = new InitialContext (env);

      // connect to the queue
      Queue queue = (Queue)jndi.lookup(queueBindingName);
      QueueConnection queueConnection = qcf.createQueueConnection();
      QueueSession queueSession = queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
      queueConnection.start();

      QueueConnectionFactory qcf = (QueueConnectionFactory) jndi.lookup(queuecfBindingName);
      QueueSender queueSender = queueSession.createSender ( queue );
      TextMessage message = queueSession.createTextMessage( );
      message.setText( "Hello World" );
      queueSender.send ( message );
      queueSender.close();
      queueSession.close();
      queueConn.close();
      jndi.close();
    }
    catch (NamingException e ) {
      e.printStackTrace();
    }
    catch (JMSException e) {
      e.printStackTrace();
    }
  }

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

}

 

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

 

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

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

  // method
  jms.sendMessage();
%>
foo.sendMessage 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.sendMessage method has been called" should be displayed.

 

This should PUT the message Hello World into the queue. To verify the message was placed in the queue, check the application server log. For example, if using a WebSphere application server, the HPEL or SystemOut.log should have the following.

[mm/dd/yy hh:mm:ss:sss CDT] 000004f8 SystemOut   O received: Hello World

 

Then check the messaging server to verify the message is in the queue. For example, if using an IBM MQ server, you could use the amqsbcg command to verify the message is in the queue.

Now, you can write a Java application to GET the message from 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 e667a in the box below so that we can be sure you are a human.




Comments