KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > demo > SimpleQueueSender


1 /**
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one or more
4  * contributor license agreements. See the NOTICE file distributed with
5  * this work for additional information regarding copyright ownership.
6  * The ASF licenses this file to You under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with
8  * the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19 /**
20  * The SimpleQueueSender class consists only of a main method,
21  * which sends several messages to a queue.
22  *
23  * Run this program in conjunction with SimpleQueueReceiver.
24  * Specify a queue name on the command line when you run the
25  * program. By default, the program sends one message. Specify
26  * a number after the queue name to send that number of messages.
27  */

28 package org.apache.activemq.demo;
29
30 // START SNIPPET: demo
31

32
33 import javax.jms.JMSException JavaDoc;
34 import javax.jms.Queue JavaDoc;
35 import javax.jms.QueueConnection JavaDoc;
36 import javax.jms.QueueConnectionFactory JavaDoc;
37 import javax.jms.QueueSender JavaDoc;
38 import javax.jms.QueueSession JavaDoc;
39 import javax.jms.Session JavaDoc;
40 import javax.jms.TextMessage JavaDoc;
41 import javax.naming.Context JavaDoc;
42 import javax.naming.InitialContext JavaDoc;
43 import javax.naming.NamingException JavaDoc;
44
45 public class SimpleQueueSender {
46     
47     private static final org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory
48             .getLog(SimpleQueueSender.class);
49
50     /**
51      * Main method.
52      *
53      * @param args the queue used by the example and,
54      * optionally, the number of messages to send
55      */

56     public static void main(String JavaDoc[] args) {
57         String JavaDoc queueName = null;
58         Context JavaDoc jndiContext = null;
59         QueueConnectionFactory JavaDoc queueConnectionFactory = null;
60         QueueConnection JavaDoc queueConnection = null;
61         QueueSession JavaDoc queueSession = null;
62         Queue JavaDoc queue = null;
63         QueueSender JavaDoc queueSender = null;
64         TextMessage JavaDoc message = null;
65         final int NUM_MSGS;
66
67         if ((args.length < 1) || (args.length > 2)) {
68             log.info("Usage: java SimpleQueueSender " +
69                     "<queue-name> [<number-of-messages>]");
70             System.exit(1);
71         }
72         queueName = args[0];
73         log.info("Queue name is " + queueName);
74         if (args.length == 2) {
75             NUM_MSGS = (new Integer JavaDoc(args[1])).intValue();
76         }
77         else {
78             NUM_MSGS = 1;
79         }
80
81         /*
82          * Create a JNDI API InitialContext object if none exists
83          * yet.
84          */

85         try {
86             jndiContext = new InitialContext JavaDoc();
87         }
88         catch (NamingException JavaDoc e) {
89             log.info("Could not create JNDI API context: " + e.toString());
90             System.exit(1);
91         }
92
93         /*
94          * Look up connection factory and queue. If either does
95          * not exist, exit.
96          */

97         try {
98             queueConnectionFactory = (QueueConnectionFactory JavaDoc) jndiContext.lookup("QueueConnectionFactory");
99             queue = (Queue JavaDoc) jndiContext.lookup(queueName);
100         }
101         catch (NamingException JavaDoc e) {
102             log.info("JNDI API lookup failed: " + e);
103             System.exit(1);
104         }
105
106         /*
107          * Create connection.
108          * Create session from connection; false means session is
109          * not transacted.
110          * Create sender and text message.
111          * Send messages, varying text slightly.
112          * Send end-of-messages message.
113          * Finally, close connection.
114          */

115         try {
116             queueConnection = queueConnectionFactory.createQueueConnection();
117             queueSession = queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
118             queueSender = queueSession.createSender(queue);
119             message = queueSession.createTextMessage();
120             for (int i = 0; i < NUM_MSGS; i++) {
121                 message.setText("This is message " + (i + 1));
122                 log.info("Sending message: " +
123                         message.getText());
124                 queueSender.send(message);
125             }
126
127             /*
128              * Send a non-text control message indicating end of
129              * messages.
130              */

131             queueSender.send(queueSession.createMessage());
132         }
133         catch (JMSException JavaDoc e) {
134             log.info("Exception occurred: " +
135                     e.toString());
136         }
137         finally {
138             if (queueConnection != null) {
139                 try {
140                     queueConnection.close();
141                 }
142                 catch (JMSException JavaDoc e) {
143                 }
144             }
145         }
146     }
147 }
148
149 // END SNIPPET: demo
150
Popular Tags