1 /***
2 * Redistribution and use of this software and associated documentation
3 * ("Software"), with or without modification, are permitted provided
4 * that the following conditions are met:
5 *
6 * 1. Redistributions of source code must retain copyright
7 * statements and notices. Redistributions must also contain a
8 * copy of this document.
9 *
10 * 2. Redistributions in binary form must reproduce the
11 * above copyright notice, this list of conditions and the
12 * following disclaimer in the documentation and/or other
13 * materials provided with the distribution.
14 *
15 * 3. The name "Exolab" must not be used to endorse or promote
16 * products derived from this Software without prior written
17 * permission of Exoffice Technologies. For written permission,
18 * please contact tma@netspace.net.au.
19 *
20 * 4. Products derived from this Software may not be called "Exolab"
21 * nor may "Exolab" appear in their names without prior written
22 * permission of Exoffice Technologies. Exolab is a registered
23 * trademark of Exoffice Technologies.
24 *
25 * 5. Due credit should be given to the Exolab Project
26 * (http://www.exolab.org/).
27 *
28 * THIS SOFTWARE IS PROVIDED BY EXOFFICE TECHNOLOGIES AND CONTRIBUTORS
29 * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
30 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
31 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
32 * EXOFFICE TECHNOLOGIES OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
33 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
39 * OF THE POSSIBILITY OF SUCH DAMAGE.
40 *
41 * Copyright 2001-2004 (C) Exoffice Technologies Inc. All Rights Reserved.
42 *
43 * $Id: MessageBrowser.java,v 1.6 2004/01/31 13:44:24 tanderson Exp $
44 */
45 package org.exolab.jmscts.core;
46
47 import java.util.Enumeration;
48 import java.util.LinkedList;
49 import java.util.List;
50
51 import javax.jms.Destination;
52 import javax.jms.JMSException;
53 import javax.jms.Message;
54 import javax.jms.Queue;
55 import javax.jms.QueueBrowser;
56 import javax.jms.QueueSession;
57
58
59 /***
60 * Receives messages using a {@link javax.jms.QueueBrowser}
61 *
62 * @version $Revision: 1.6 $ $Date: 2004/01/31 13:44:24 $
63 * @author <a href="mailto:tma@netspace.net.au">Tim Anderson</a>
64 * @see MessageReceiver
65 * @see AsynchronousReceiver
66 * @see SynchronousReceiver
67 */
68 class MessageBrowser implements MessageReceiver {
69
70 /***
71 * The session that owns the browser
72 */
73 private QueueSession _session = null;
74
75 /***
76 * The queue to receive messages from
77 */
78 private Queue _queue = null;
79
80 /***
81 * The browser used to receive messages
82 */
83 private QueueBrowser _browser = null;
84
85 /***
86 * The message selector
87 */
88 private String _selector = null;
89
90 /***
91 * If true, a selector was specified at construction. The selector may be
92 * null - this is to ensure that the appropriate createBrowser method is
93 * invoked.
94 */
95 private boolean _hasSelector = false;
96
97
98 /***
99 * Construct an instance with the session and queue to receive messages
100 * from
101 *
102 * @param session the queue session
103 * @param queue the queue to receive messages from
104 * @throws JMSException if a <code>QueueBrowser</code> cannot be created
105 */
106 public MessageBrowser(QueueSession session, Queue queue)
107 throws JMSException {
108 if (session == null) {
109 throw new IllegalArgumentException("Argument session is null");
110 }
111 if (queue == null) {
112 throw new IllegalArgumentException("Argument queue is null");
113 }
114 _session = session;
115 _queue = queue;
116 getBrowser();
117 }
118
119 /***
120 * Construct an instance with the session and queue to receive messages
121 * from, using a message selector
122 *
123 * @param session the queue session
124 * @param queue the queue to receive messages from
125 * @param selector the message selector
126 * @throws JMSException if a <code>QueueBrowser</code> cannot be created
127 */
128 public MessageBrowser(QueueSession session, Queue queue, String selector)
129 throws JMSException {
130 if (session == null) {
131 throw new IllegalArgumentException("Argument session is null");
132 }
133 if (queue == null) {
134 throw new IllegalArgumentException("Argument queue is null");
135 }
136 _session = session;
137 _queue = queue;
138 _selector = selector;
139 _hasSelector = true;
140 getBrowser();
141 }
142
143 /***
144 * Return a list of {@link javax.jms.Message} instances from the underlying
145 * QueueBrowser.
146 *
147 * @param count the number of messages expected
148 * @param timeout the maximum time to wait for each message. This is ignore
149 * ignored by this implementation
150 * @return a list of messages, or null, if no messages were received
151 * @throws JMSException if any of the JMS operation fail
152 */
153 public List receive(int count, long timeout) throws JMSException {
154 List result = null;
155 QueueBrowser browser = getBrowser();
156 Enumeration iter = browser.getEnumeration();
157 if (count == 0) {
158 // don't expect any messages to be received. Attempt to receive
159 // one message.
160 ++count;
161 }
162
163 while (iter.hasMoreElements() && count > 0) {
164 Message message = (Message) iter.nextElement();
165 if (result == null) {
166 result = new LinkedList();
167 }
168 result.add(message);
169 --count;
170 }
171
172 return result;
173 }
174
175 /***
176 * Receive messages, delegating received messages to a
177 * <code>CountingListener</code>
178 *
179 * @param timeout the maximum time to wait for each message. If set to 0,
180 * then it waits until a message becomes available.
181 * @param listener the listener to delegate messages to
182 * @throws JMSException if the operation fails
183 */
184 public void receive(long timeout, CountingListener listener)
185 throws JMSException {
186
187 QueueBrowser browser = getBrowser();
188 Enumeration iter = browser.getEnumeration();
189
190 int count = listener.getExpected();
191 if (count == 0) {
192 // don't expect any messages to be received. Attempt to receive
193 // one message.
194 if (iter.hasMoreElements()) {
195 Message message = (Message) iter.nextElement();
196 listener.onMessage(message);
197 }
198 } else {
199 while (iter.hasMoreElements()) {
200 Message message = (Message) iter.nextElement();
201 listener.onMessage(message);
202 if (listener.getReceived() == count) {
203 break;
204 }
205 }
206 }
207 }
208
209 /***
210 * Return the destination associated with the QueueBrowser
211 *
212 * @return the destination to receive messages from
213 */
214 public Destination getDestination() {
215 return _queue;
216 }
217
218 /***
219 * Returns the message selector associated with the MessageConsumer
220 *
221 * @return the message selector
222 * @throws JMSException if the operation fails
223 */
224 public String getSelector() throws JMSException {
225 return _selector;
226 }
227
228 /***
229 * Returns the name of the subscriber, if the consumer is a durable topic
230 * subscriber
231 *
232 * @return <code>null</code>
233 */
234 public String getName() {
235 return null;
236 }
237
238 /***
239 * Returns the no-local value, if the consumer is a topic subscriber
240 *
241 * @return <code>false</code>
242 */
243 public boolean getNoLocal() {
244 return false;
245 }
246
247 /***
248 * Close the underlying MessageBrowser
249 *
250 * @throws JMSException if the operation fails
251 */
252 public void close() throws JMSException {
253 if (_browser != null) {
254 _browser.close();
255 _browser = null;
256 }
257 }
258
259 /***
260 * Close the underlying QueueBrowser
261 *
262 * @throws JMSException if the operation fails
263 */
264 public void remove() throws JMSException {
265 close();
266 }
267
268 /***
269 * Returns the QueueBrowser, constructing a new one if necessary
270 *
271 * @return the underlying queue browser
272 * @throws JMSException if a <code>QueueBrowser</code> cannot be created
273 */
274 private QueueBrowser getBrowser() throws JMSException {
275 if (_browser != null && !_browser.getEnumeration().hasMoreElements()) {
276 _browser.close();
277 _browser = null;
278 }
279 if (_browser == null) {
280 if (_hasSelector) {
281 _browser = _session.createBrowser(_queue, _selector);
282 } else {
283 _browser = _session.createBrowser(_queue);
284 }
285 }
286 return _browser;
287 }
288
289 }
This page was automatically generated by Maven