Show TOC Start of Content Area

Procedure documentation Using Queue Browsers  Locate the document in its SAP Library structure

Use

With a queue browser you can only look at the messages in a queue without removing them. This means that you can explore the messages while leaving them available for delivery to consumers. That is why queue browsers are often used to implement monitoring logic.

Note

Messages may be arriving and expiring while the scan is being done. The JMS API does not require the content of an enumeration to be a static snapshot of the queue content.

Procedure

...

       1.      To create a queue browser, use one of the two createBrowser() methods of the queue session, providing the queue and optionally a selector expression:

QueueBrowser browser = session.createBrowser(myQueue);

or

QueueBrowser browser = session.createBrowser(myQueue, selector);

       2.      To get an enumeration for browsing the current queue messages in the order they would be received and to process the messages, use the getEnumeration() method:

Enumeration en = browser.getEnumeration();

while (en.hasMoreElements()) {

   Message message = (Message) en.nextElement();

   // process the message

}

       3.      To close the queue browser, use the close() method:

browser.close();

Example

package com.sap.test.jms;

 

import java.io.IOException;

import java.io.PrintWriter;

import java.util.Enumeration;

import java.util.Properties;

 

import javax.annotation.Resource;

import javax.jms.JMSException;

import javax.jms.Queue;

import javax.jms.QueueBrowser;

import javax.jms.QueueConnection;

import javax.jms.QueueConnectionFactory;

import javax.jms.QueueSender;

import javax.jms.QueueSession;

import javax.jms.Session;

import javax.jms.TextMessage;

import javax.naming.Context;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

 

/**

 * Servlet implementation class for Servlet: SendingServlet

 *

 */

 

public class QueueBrowserServlet extends javax.servlet.http.HttpServlet

      implements javax.servlet.Servlet {

 

   private static final long serialVersionUID = 1L;

 

   private static final String USER = "user";

 

   private static final String PASSWORD = "password";

 

   private static final String SAP_NAMING_PROVIDER_URL = "localhost:56004";

 

   private static final String SAP_INITIAL_CONTEXT_FACTORY_IMPL = "com.sap.engine.services.jndi.InitialContextFactoryImpl";

 

   private PrintWriter printWriter;

 

   // inject QueueConnectionFactory to the field queueConnectionFactoryAnnot

   @Resource(mappedName = "MyServletQueueConnectionFactory")

   private QueueConnectionFactory queueConnectionFactoryAnnot;

 

   // inject Queue to the field queueAnnot

   @Resource(mappedName = "MyServletQueue")

   private Queue queueAnnot;

 

   public QueueBrowserServlet() {

      super();

   }

 

   protected void doGet(HttpServletRequest request,

         HttpServletResponse response) throws ServletException, IOException {

      doTest(request, response);

   }

 

   protected void doPost(HttpServletRequest request,

         HttpServletResponse response) throws ServletException, IOException {

      doTest(request, response);

   }

 

   private void doTest(HttpServletRequest request, HttpServletResponse response)

         throws ServletException {

      QueueConnection queueConnection = null;

      QueueSession queueSession = null;

      QueueSender queueSender = null;

      TextMessage message = null;

 

      try {

         printWriter = response.getWriter();

      } catch (IOException ioe) {

         ioe.printStackTrace();

         throw new ServletException("Exception during response.getWriter()");

      }

      printWriter.println("Start sending.");

 

      // set the properties of the connection

      Properties properties = System.getProperties();

      properties.put(Context.INITIAL_CONTEXT_FACTORY,

            SAP_INITIAL_CONTEXT_FACTORY_IMPL);

      properties.put(Context.PROVIDER_URL, SAP_NAMING_PROVIDER_URL);

      properties.put(Context.SECURITY_PRINCIPAL, USER);

      properties.put(Context.SECURITY_CREDENTIALS, PASSWORD);

 

      try {

         // queueConnectionFactoryAnnot is injected via annotation

         queueConnection = queueConnectionFactoryAnnot

               .createQueueConnection();

         queueSession = queueConnection.createQueueSession(false,

               Session.AUTO_ACKNOWLEDGE);

         // queueAnnot is injected via annotation

         queueSender = queueSession.createSender(queueAnnot);

         message = queueSession.createTextMessage();

         // sending messages

         for (int i = 1; i <= 3; i++) {

            message.setText("This is message " + i);

            printWriter.println("<br>" + "Sending message " + i);

            queueSender.send(message);

         }

         printWriter.println("<br>End sending.<br>");

 

         printWriter.println("<br>MyServletQueue contains:");

 

         queueConnection.start();

 

         // creating a queue browser

         QueueBrowser browser = queueSession.createBrowser(queueAnnot);

         Enumeration en = browser.getEnumeration();

         // monitoring the content of the queue using queue browser

         while (en.hasMoreElements()) {

            TextMessage queueBrMessage = (TextMessage) en.nextElement();

            printWriter.println("<br>" + queueBrMessage.getText());

         }

 

      } catch (JMSException e) {

         throw new ServletException("Exception occurred: " + e.toString());

 

      } finally {

         if (queueConnection != null) {

 

            try {

               queueConnection.close();

            } catch (JMSException e) {

               throw new ServletException(

                     "Exception during queueConnection.close()");

            }

         }

      }

   }

}

 

End of Content Area