KickJava   Java API By Example, From Geeks To Geeks.

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


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 SimpleQueueReceiver class consists only of a main method,
21  * which fetches one or more messages from a queue using
22  * synchronous message delivery. Run this program in conjunction
23  * with SimpleQueueSender. Specify a queue name on the command
24  * line when you run the program.
25  */

26 package org.apache.activemq.demo;
27
28 import javax.jms.JMSException JavaDoc;
29 import javax.jms.Message JavaDoc;
30 import javax.jms.Queue JavaDoc;
31 import javax.jms.QueueConnection JavaDoc;
32 import javax.jms.QueueConnectionFactory JavaDoc;
33 import javax.jms.QueueReceiver JavaDoc;
34 import javax.jms.QueueSession JavaDoc;
35 import javax.jms.Session JavaDoc;
36 import javax.jms.TextMessage JavaDoc;
37 import javax.naming.Context JavaDoc;
38 import javax.naming.InitialContext JavaDoc;
39 import javax.naming.NamingException JavaDoc;
40
41 public class SimpleQueueReceiver {
42     
43     private static final org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory
44             .getLog(SimpleQueueReceiver.class);
45
46     /**
47      * Main method.
48      *
49      * @param args the queue used by the example
50      */

51     public static void main(String JavaDoc[] args) {
52         String JavaDoc queueName = null;
53         Context JavaDoc jndiContext = null;
54         QueueConnectionFactory JavaDoc queueConnectionFactory = null;
55         QueueConnection JavaDoc queueConnection = null;
56         QueueSession JavaDoc queueSession = null;
57         Queue JavaDoc queue = null;
58         QueueReceiver JavaDoc queueReceiver = null;
59         TextMessage JavaDoc message = null;
60
61         /*
62          * Read queue name from command line and display it.
63          */

64         if (args.length != 1) {
65             log.info("Usage: java " +
66                     "SimpleQueueReceiver <queue-name>");
67             System.exit(1);
68         }
69         queueName = args[0];
70         log.info("Queue name is " + queueName);
71
72         /*
73          * Create a JNDI API InitialContext object if none exists
74          * yet.
75          */

76         try {
77             jndiContext = new InitialContext JavaDoc();
78         }
79         catch (NamingException JavaDoc e) {
80             log.info("Could not create JNDI API " +
81                     "context: " + e.toString());
82             System.exit(1);
83         }
84
85         /*
86          * Look up connection factory and queue. If either does
87          * not exist, exit.
88          */

89         try {
90             queueConnectionFactory = (QueueConnectionFactory JavaDoc)
91                     jndiContext.lookup("QueueConnectionFactory");
92             queue = (Queue JavaDoc) jndiContext.lookup(queueName);
93         }
94         catch (NamingException JavaDoc e) {
95             log.info("JNDI API lookup failed: " +
96                     e.toString());
97             System.exit(1);
98         }
99
100         /*
101          * Create connection.
102          * Create session from connection; false means session is
103          * not transacted.
104          * Create receiver, then start message delivery.
105          * Receive all text messages from queue until
106          * a non-text message is received indicating end of
107          * message stream.
108          * Close connection.
109          */

110         try {
111             queueConnection =
112                     queueConnectionFactory.createQueueConnection();
113             queueSession =
114                     queueConnection.createQueueSession(false,
115                             Session.AUTO_ACKNOWLEDGE);
116             queueReceiver = queueSession.createReceiver(queue);
117             queueConnection.start();
118             while (true) {
119                 Message JavaDoc m = queueReceiver.receive(1);
120                 if (m != null) {
121                     if (m instanceof TextMessage JavaDoc) {
122                         message = (TextMessage JavaDoc) m;
123                         log.info("Reading message: " +
124                                 message.getText());
125                     }
126                     else {
127                         break;
128                     }
129                 }
130             }
131         }
132         catch (JMSException JavaDoc e) {
133             log.info("Exception occurred: " +
134                     e.toString());
135         }
136         finally {
137             if (queueConnection != null) {
138                 try {
139                     queueConnection.close();
140                 }
141                 catch (JMSException JavaDoc e) {
142                 }
143             }
144         }
145     }
146 }
147
Popular Tags