KickJava   Java API By Example, From Geeks To Geeks.

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


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 javax.jms.Connection JavaDoc;
33 import javax.jms.ConnectionFactory JavaDoc;
34 import javax.jms.Destination JavaDoc;
35 import javax.jms.JMSException JavaDoc;
36 import javax.jms.MessageProducer JavaDoc;
37 import javax.jms.Session JavaDoc;
38 import javax.jms.TextMessage JavaDoc;
39 import javax.naming.Context JavaDoc;
40 import javax.naming.InitialContext JavaDoc;
41 import javax.naming.NamingException JavaDoc;
42
43 /**
44  * A simple polymorphic JMS producer which can work with Queues or Topics
45  * which uses JNDI to lookup the JMS connection factory and destination
46  *
47  * @version $Revision: 1.2 $
48  */

49 public class SimpleProducer {
50     
51     private static final org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory
52             .getLog(SimpleProducer.class);
53
54     /**
55      * @param args the destination name to send to and optionally, the number of messages to send
56      */

57     public static void main(String JavaDoc[] args) {
58         Context JavaDoc jndiContext = null;
59         ConnectionFactory JavaDoc connectionFactory = null;
60         Connection JavaDoc connection = null;
61         Session JavaDoc session = null;
62         Destination JavaDoc destination = null;
63         MessageProducer JavaDoc producer = null;
64         String JavaDoc destinationName = null;
65         final int NUM_MSGS;
66
67         if ((args.length < 1) || (args.length > 2)) {
68             log.info("Usage: java SimpleProducer <destination-name> [<number-of-messages>]");
69             System.exit(1);
70         }
71         destinationName = args[0];
72         log.info("Destination name is " + destinationName);
73         if (args.length == 2) {
74             NUM_MSGS = (new Integer JavaDoc(args[1])).intValue();
75         }
76         else {
77             NUM_MSGS = 1;
78         }
79
80         /*
81          * Create a JNDI API InitialContext object
82          */

83         try {
84             jndiContext = new InitialContext JavaDoc();
85         }
86         catch (NamingException JavaDoc e) {
87             log.info("Could not create JNDI API context: " + e.toString());
88             System.exit(1);
89         }
90
91         /*
92          * Look up connection factory and destination.
93          */

94         try {
95             connectionFactory = (ConnectionFactory JavaDoc) jndiContext.lookup("ConnectionFactory");
96             destination = (Destination JavaDoc) jndiContext.lookup(destinationName);
97         }
98         catch (NamingException JavaDoc e) {
99             log.info("JNDI API lookup failed: " + e);
100             System.exit(1);
101         }
102
103         /*
104          * Create connection.
105          * Create session from connection; false means session is not transacted.
106          * Create sender and text message.
107          * Send messages, varying text slightly.
108          * Send end-of-messages message.
109          * Finally, close connection.
110          */

111         try {
112             connection = connectionFactory.createConnection();
113             session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
114             producer = session.createProducer(destination);
115             TextMessage JavaDoc message = session.createTextMessage();
116             for (int i = 0; i < NUM_MSGS; i++) {
117                 message.setText("This is message " + (i + 1));
118                 log.info("Sending message: " + message.getText());
119                 producer.send(message);
120             }
121
122             /*
123              * Send a non-text control message indicating end of messages.
124              */

125             producer.send(session.createMessage());
126         }
127         catch (JMSException JavaDoc e) {
128             log.info("Exception occurred: " + e);
129         }
130         finally {
131             if (connection != null) {
132                 try {
133                     connection.close();
134                 }
135                 catch (JMSException JavaDoc e) {
136                 }
137             }
138         }
139     }
140 }
141
142 // END SNIPPET: demo
143
Popular Tags