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 SimpleQueueReceiver class consists only of a main method,
42 * which fetches one or more messages from a queue using
43 * synchronous message delivery. Run this program in conjunction
44 * with SimpleQueueSender. Specify a queue name on the command
45 * line when you run the program.
46 */
47 package org.codehaus.activemq.demo;
48
49 import javax.jms.JMSException;
50 import javax.jms.Message;
51 import javax.jms.Queue;
52 import javax.jms.QueueConnection;
53 import javax.jms.QueueConnectionFactory;
54 import javax.jms.QueueReceiver;
55 import javax.jms.QueueSession;
56 import javax.jms.Session;
57 import javax.jms.TextMessage;
58 import javax.naming.Context;
59 import javax.naming.InitialContext;
60 import javax.naming.NamingException;
61
62 public class SimpleQueueReceiver {
63
64 /***
65 * Main method.
66 *
67 * @param args the queue used by the example
68 */
69 public static void main(String[] args) {
70 String queueName = null;
71 Context jndiContext = null;
72 QueueConnectionFactory queueConnectionFactory = null;
73 QueueConnection queueConnection = null;
74 QueueSession queueSession = null;
75 Queue queue = null;
76 QueueReceiver queueReceiver = null;
77 TextMessage message = null;
78
79 /*
80 * Read queue name from command line and display it.
81 */
82 if (args.length != 1) {
83 System.out.println("Usage: java " +
84 "SimpleQueueReceiver <queue-name>");
85 System.exit(1);
86 }
87 queueName = new String(args[0]);
88 System.out.println("Queue name is " + queueName);
89
90 /*
91 * Create a JNDI API InitialContext object if none exists
92 * yet.
93 */
94 try {
95 jndiContext = new InitialContext();
96 }
97 catch (NamingException e) {
98 System.out.println("Could not create JNDI API " +
99 "context: " + e.toString());
100 System.exit(1);
101 }
102
103 /*
104 * Look up connection factory and queue. If either does
105 * not exist, exit.
106 */
107 try {
108 queueConnectionFactory = (QueueConnectionFactory)
109 jndiContext.lookup("QueueConnectionFactory");
110 queue = (Queue) jndiContext.lookup(queueName);
111 }
112 catch (NamingException e) {
113 System.out.println("JNDI API lookup failed: " +
114 e.toString());
115 System.exit(1);
116 }
117
118 /*
119 * Create connection.
120 * Create session from connection; false means session is
121 * not transacted.
122 * Create receiver, then start message delivery.
123 * Receive all text messages from queue until
124 * a non-text message is received indicating end of
125 * message stream.
126 * Close connection.
127 */
128 try {
129 queueConnection =
130 queueConnectionFactory.createQueueConnection();
131 queueSession =
132 queueConnection.createQueueSession(false,
133 Session.AUTO_ACKNOWLEDGE);
134 queueReceiver = queueSession.createReceiver(queue);
135 queueConnection.start();
136 while (true) {
137 Message m = queueReceiver.receive(1);
138 if (m != null) {
139 if (m instanceof TextMessage) {
140 message = (TextMessage) m;
141 System.out.println("Reading message: " +
142 message.getText());
143 }
144 else {
145 break;
146 }
147 }
148 }
149 }
150 catch (JMSException e) {
151 System.out.println("Exception occurred: " +
152 e.toString());
153 }
154 finally {
155 if (queueConnection != null) {
156 try {
157 queueConnection.close();
158 }
159 catch (JMSException e) {
160 }
161 }
162 }
163 }
164 }