SimpleQueueSender xref

    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 SimpleQueueSender class consists only of a main method,
    42   * which sends several messages to a queue.
    43   *
    44   * Run this program in conjunction with SimpleQueueReceiver.
    45   * Specify a queue name on the command line when you run the
    46   * program.  By default, the program sends one message.  Specify
    47   * a number after the queue name to send that number of messages.
    48   */
    49  package org.codehaus.activemq.demo;
    50
    51  // START SNIPPET: demo
    52
    53
    54  import javax.jms.JMSException;
    55  import javax.jms.Queue;
    56  import javax.jms.QueueConnection;
    57  import javax.jms.QueueConnectionFactory;
    58  import javax.jms.QueueSender;
    59  import javax.jms.QueueSession;
    60  import javax.jms.Session;
    61  import javax.jms.TextMessage;
    62  import javax.naming.Context;
    63  import javax.naming.InitialContext;
    64  import javax.naming.NamingException;
    65
    66  public class SimpleQueueSender {
    67
    68      /***
    69       * Main method.
    70       *
    71       * @param args the queue used by the example and,
    72       *             optionally, the number of messages to send
    73       */
    74      public static void main(String[] args) {
    75          String queueName = null;
    76          Context jndiContext = null;
    77          QueueConnectionFactory queueConnectionFactory = null;
    78          QueueConnection queueConnection = null;
    79          QueueSession queueSession = null;
    80          Queue queue = null;
    81          QueueSender queueSender = null;
    82          TextMessage message = null;
    83          final int NUM_MSGS;
    84
    85          if ((args.length < 1) || (args.length > 2)) {
    86              System.out.println("Usage: java SimpleQueueSender " +
    87                      "<queue-name> [<number-of-messages>]");
    88              System.exit(1);
    89          }
    90          queueName = new String(args[0]);
    91          System.out.println("Queue name is " + queueName);
    92          if (args.length == 2) {
    93              NUM_MSGS = (new Integer(args[1])).intValue();
    94          }
    95          else {
    96              NUM_MSGS = 1;
    97          }
    98
    99          /*
    100          * Create a JNDI API InitialContext object if none exists
    101          * yet.
    102          */
    103         try {
    104             jndiContext = new InitialContext();
    105         }
    106         catch (NamingException e) {
    107             System.out.println("Could not create JNDI API context: " + e.toString());
    108             System.exit(1);
    109         }
    110
    111         /*
    112          * Look up connection factory and queue.  If either does
    113          * not exist, exit.
    114          */
    115         try {
    116             queueConnectionFactory = (QueueConnectionFactory) jndiContext.lookup("QueueConnectionFactory");
    117             queue = (Queue) jndiContext.lookup(queueName);
    118         }
    119         catch (NamingException e) {
    120             System.out.println("JNDI API lookup failed: " + e);
    121             System.exit(1);
    122         }
    123
    124         /*
    125          * Create connection.
    126          * Create session from connection; false means session is
    127          * not transacted.
    128          * Create sender and text message.
    129          * Send messages, varying text slightly.
    130          * Send end-of-messages message.
    131          * Finally, close connection.
    132          */
    133         try {
    134             queueConnection = queueConnectionFactory.createQueueConnection();
    135             queueSession = queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
    136             queueSender = queueSession.createSender(queue);
    137             message = queueSession.createTextMessage();
    138             for (int i = 0; i < NUM_MSGS; i++) {
    139                 message.setText("This is message " + (i + 1));
    140                 System.out.println("Sending message: " +
    141                         message.getText());
    142                 queueSender.send(message);
    143             }
    144
    145             /*
    146              * Send a non-text control message indicating end of
    147              * messages.
    148              */
    149             queueSender.send(queueSession.createMessage());
    150         }
    151         catch (JMSException e) {
    152             System.out.println("Exception occurred: " +
    153                     e.toString());
    154         }
    155         finally {
    156             if (queueConnection != null) {
    157                 try {
    158                     queueConnection.close();
    159                 }
    160                 catch (JMSException e) {
    161                 }
    162             }
    163         }
    164     }
    165 }
    166
    167 // END SNIPPET: demo