KickJava   Java API By Example, From Geeks To Geeks.

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


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 import org.apache.activemq.ActiveMQConnectionFactory;
33
34 import javax.jms.*;
35 import javax.naming.Context JavaDoc;
36 import javax.naming.InitialContext JavaDoc;
37 import javax.naming.NamingException JavaDoc;
38
39 /**
40  * A simple queue sender which does not use JNDI
41  *
42  * @version $Revision: 1.1 $
43  */

44 public class DefaultQueueSender {
45
46     private static final org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory
47             .getLog(DefaultQueueSender.class);
48
49     public static void main(String JavaDoc[] args) {
50
51         String JavaDoc uri = "tcp://localhost:61616";
52         String JavaDoc text = "Hello World!";
53
54         Connection connection = null;
55         QueueSession queueSession = null;
56
57         if ((args.length < 1)) {
58             printUsage();
59             System.exit(1);
60         }
61
62         int idx = 0;
63         String JavaDoc arg = args[0];
64         if (arg.equals("-uri")) {
65             if (args.length == 1) {
66                 printUsage();
67                 System.exit(1);
68             }
69             uri = args[1];
70             idx += 2;
71         }
72         String JavaDoc queueName = args[idx];
73         log.info("Connecting to: " + uri);
74         log.info("Queue name is " + queueName);
75
76         if (++idx < args.length) {
77             text = args[idx];
78         }
79
80         try {
81             ConnectionFactory factory = new ActiveMQConnectionFactory(uri);
82             connection = factory.createConnection();
83             Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
84             Destination destination = session.createQueue(queueName);
85             MessageProducer producer = session.createProducer(destination);
86
87             Message message = session.createTextMessage(text);
88             producer.send(message);
89         }
90         catch (JMSException e) {
91             log.info("Exception occurred: " + e.toString());
92         }
93         finally {
94             if (connection != null) {
95                 try {
96                     connection.close();
97                 }
98                 catch (JMSException e) {
99                 }
100             }
101         }
102     }
103
104     protected static void printUsage() {
105         System.out.println("Usage: java DefaultQueueSender [-uri <connection-uri>] " + "<queue-name> [<message-body>]");
106     }
107 }
108
109 // END SNIPPET: demo
110
Popular Tags