    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  import javax.jms.Connection;
    54  import javax.jms.ConnectionFactory;
    55  import javax.jms.Destination;
    56  import javax.jms.JMSException;
    57  import javax.jms.MessageProducer;
    58  import javax.jms.Session;
    59  import javax.jms.TextMessage;
    60  import javax.naming.Context;
    61  import javax.naming.InitialContext;
    62  import javax.naming.NamingException;
    63
    64  /***
    65   * A simple polymorphic JMS producer which can work with Queues or Topics
    66   * which uses JNDI to lookup the JMS connection factory and destination
    67   *
    68   * @version $Revision: 1.2 $
    69   */
    70  public class SimpleProducer {
    71
    72      /***
    73       * @param args the destination name to send to and optionally, the number of messages to send
    74       */
    75      public static void main(String[] args) {
    76          Context jndiContext = null;
    77          ConnectionFactory connectionFactory = null;
    78          Connection connection = null;
    79          Session session = null;
    80          Destination destination = null;
    81          MessageProducer producer = null;
    82          String destinationName = null;
    83          final int NUM_MSGS;
    84
    85          if ((args.length < 1) || (args.length > 2)) {
    86              System.out.println("Usage: java SimpleProducer <destination-name> [<number-of-messages>]");
    87              System.exit(1);
    88          }
    89          destinationName = new String(args[0]);
    90          System.out.println("Destination name is " + destinationName);
    91          if (args.length == 2) {
    92              NUM_MSGS = (new Integer(args[1])).intValue();
    93          }
    94          else {
    95              NUM_MSGS = 1;
    96          }
    97
    98          /*
    99           * Create a JNDI API InitialContext object
    100          */
    101         try {
    102             jndiContext = new InitialContext();
    103         }
    104         catch (NamingException e) {
    105             System.out.println("Could not create JNDI API context: " + e.toString());
    106             System.exit(1);
    107         }
    108
    109         /*
    110          * Look up connection factory and destination.
    111          */
    112         try {
    113             connectionFactory = (ConnectionFactory) jndiContext.lookup("ConnectionFactory");
    114             destination = (Destination) jndiContext.lookup(destinationName);
    115         }
    116         catch (NamingException e) {
    117             System.out.println("JNDI API lookup failed: " + e);
    118             System.exit(1);
    119         }
    120
    121         /*
    122          * Create connection.
    123          * Create session from connection; false means session is not transacted.
    124          * Create sender and text message.
    125          * Send messages, varying text slightly.
    126          * Send end-of-messages message.
    127          * Finally, close connection.
    128          */
    129         try {
    130             connection = connectionFactory.createConnection();
    131             session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    132             producer = session.createProducer(destination);
    133             TextMessage message = session.createTextMessage();
    134             for (int i = 0; i < NUM_MSGS; i++) {
    135                 message.setText("This is message " + (i + 1));
    136                 System.out.println("Sending message: " + message.getText());
    137                 producer.send(message);
    138             }
    139
    140             /*
    141              * Send a non-text control message indicating end of messages.
    142              */
    143             producer.send(session.createMessage());
    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 }
    159
    160 // END SNIPPET: demo