FileDocCategorySizeDatePackage
StandaloneListener.javaAPI DocExample2460Mon Apr 25 15:15:50 BST 2005com.springbook

StandaloneListener

public class StandaloneListener extends Object

Fields Summary
Constructors Summary
Methods Summary
public static voidmain(java.lang.String[] args)

      String queueName = "billingQueue";
      Context jndiContext = null;
      QueueConnectionFactory queueConnectionFactory = null;
      QueueConnection queueConnection = null;
      QueueSession queueSession = null;
      Queue queue = null;
      QueueReceiver queueReceiver = null;
      TextMessage message = null;
      
      try {
         jndiContext = new InitialContext();
      }
      catch (NamingException e) {
         System.out.println("Could not create JNDI API " +
               "context: " + e.toString());
         System.exit(1);
      }
      try {
         queueConnectionFactory = (QueueConnectionFactory)
               jndiContext.lookup("QueueConnectionFactory");
         queue = (Queue) jndiContext.lookup(queueName);
      }
      catch (NamingException e) {
         System.out.println("JNDI API lookup failed: " +
               e.toString());
         System.exit(1);
      }
      try {
         queueConnection =
               queueConnectionFactory.createQueueConnection();
         queueSession =
               queueConnection.createQueueSession(false,
                     Session.AUTO_ACKNOWLEDGE);
         queueReceiver = queueSession.createReceiver(queue);
         queueConnection.start();
         while (true) {
            Message m = queueReceiver.receive(1);
            if (m != null) {
               if (m instanceof TextMessage) {
                  message = (TextMessage) m;
                  System.out.println("Reading message: " +
                        message.getText());
               }
               else {
                  break;
               }
            }
         }
      }
      catch (JMSException e) {
         System.out.println("Exception occurred: " +
               e.toString());
      }
      finally {
         if (queueConnection != null) {
            try {
               queueConnection.close();
            }
            catch (JMSException e) {
            }
         }
      }