    1   /*
    2    *
    3    * Copyright 2002 Sun Microsystems, Inc. All Rights Reserved.
    4    *
    5    * Redistribution and use in source and binary forms, with or
    6    * without modification, are permitted provided that the following
    7    * conditions are met:
    8    *
    9    * - Redistributions of source code must retain the above copyright
    10   *   notice, this list of conditions and the following disclaimer.
    11   *
    12   * - Redistribution in binary form must reproduce the above
    13   *   copyright notice, this list of conditions and the following
    14   *   disclaimer in the documentation and/or other materials
    15   *   provided with the distribution.
    16   *
    17   * Neither the name of Sun Microsystems, Inc. or the names of
    18   * contributors may be used to endorse or promote products derived
    19   * from this software without specific prior written permission.
    20   *
    21   * This software is provided "AS IS," without a warranty of any
    22   * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND
    23   * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,
    24   * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY
    25   * EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY
    26   * DAMAGES OR LIABILITIES SUFFERED BY LICENSEE AS A RESULT OF OR
    27   * RELATING TO USE, MODIFICATION OR DISTRIBUTION OF THIS SOFTWARE OR
    28   * ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE
    29   * FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT,
    30   * SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
    31   * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF
    32   * THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF SUN HAS
    33   * BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
    34   *
    35   * You acknowledge that this software is not designed, licensed or
    36   * intended for use in the design, construction, operation or
    37   * maintenance of any nuclear facility.
    38   *
    39   */
    40  /***
    41   * The SimpleQueueReceiver class consists only of a main method,
    42   * which fetches one or more messages from a queue using
    43   * synchronous message delivery.  Run this program in conjunction
    44   * with SimpleQueueSender.  Specify a queue name on the command
    45   * line when you run the program.
    46   */
    47  package org.codehaus.activemq.demo;
    48
    49  import javax.jms.Connection;
    50  import javax.jms.ConnectionFactory;
    51  import javax.jms.Destination;
    52  import javax.jms.JMSException;
    53  import javax.jms.Message;
    54  import javax.jms.MessageConsumer;
    55  import javax.jms.Session;
    56  import javax.jms.TextMessage;
    57  import javax.naming.Context;
    58  import javax.naming.InitialContext;
    59  import javax.naming.NamingException;
    60
    61  /***
    62   * A simple polymorphic JMS consumer which can work with Queues or Topics
    63   * which uses JNDI to lookup the JMS connection factory and destination
    64   *
    65   * @version $Revision: 1.1 $
    66   */
    67  public class SimpleConsumer {
    68
    69      /***
    70       * @param args the queue used by the example
    71       */
    72      public static void main(String[] args) {
    73          String destinationName = null;
    74          Context jndiContext = null;
    75          ConnectionFactory connectionFactory = null;
    76          Connection connection = null;
    77          Session session = null;
    78          Destination destination = null;
    79          MessageConsumer consumer = null;
    80
    81          /*
    82           * Read destination name from command line and display it.
    83           */
    84          if (args.length != 1) {
    85              System.out.println("Usage: java SimpleConsumer <destination-name>");
    86              System.exit(1);
    87          }
    88          destinationName = new String(args[0]);
    89          System.out.println("Destination name is " + destinationName);
    90
    91          /*
    92           * Create a JNDI API InitialContext object
    93           */
    94          try {
    95              jndiContext = new InitialContext();
    96          }
    97          catch (NamingException e) {
    98              System.out.println("Could not create JNDI API " +
    99                      "context: " + e.toString());
    100             System.exit(1);
    101         }
    102
    103         /*
    104          * Look up connection factory and destination.
    105          */
    106         try {
    107             connectionFactory = (ConnectionFactory)
    108                     jndiContext.lookup("ConnectionFactory");
    109             destination = (Destination) jndiContext.lookup(destinationName);
    110         }
    111         catch (NamingException e) {
    112             System.out.println("JNDI API lookup failed: " +
    113                     e.toString());
    114             System.exit(1);
    115         }
    116
    117         /*
    118          * Create connection.
    119          * Create session from connection; false means session is
    120          * not transacted.
    121          * Create receiver, then start message delivery.
    122          * Receive all text messages from destination until
    123          * a non-text message is received indicating end of
    124          * message stream.
    125          * Close connection.
    126          */
    127         try {
    128             connection = connectionFactory.createConnection();
    129             session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    130             consumer = session.createConsumer(destination);
    131             connection.start();
    132             while (true) {
    133                 Message m = consumer.receive(1);
    134                 if (m != null) {
    135                     if (m instanceof TextMessage) {
    136                         TextMessage message = (TextMessage) m;
    137                         System.out.println("Reading message: " + message.getText());
    138                     }
    139                     else {
    140                         break;
    141                     }
    142                 }
    143             }
    144         }
    145         catch (JMSException e) {
    146             System.out.println("Exception occurred: " + e);
    147         }
    148         finally {
    149             if (connection != null) {
    150                 try {
    151                     connection.close();
    152                 }
    153                 catch (JMSException e) {
    154                 }
    155             }
    156         }
    157     }
    158 }