KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exolab > jms > client > JmsQueueBrowser


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 info@exolab.org.
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 2000-2005 (C) Exoffice Technologies Inc. All Rights Reserved.
42  *
43  * $Id: JmsQueueBrowser.java,v 1.2 2005/03/18 03:36:37 tanderson Exp $
44  */

45 package org.exolab.jms.client;
46
47 import java.util.Enumeration JavaDoc;
48 import java.util.LinkedList JavaDoc;
49 import java.util.List JavaDoc;
50
51 import javax.jms.JMSException JavaDoc;
52 import javax.jms.Message JavaDoc;
53 import javax.jms.Queue JavaDoc;
54 import javax.jms.QueueBrowser JavaDoc;
55
56 import org.apache.commons.logging.Log;
57 import org.apache.commons.logging.LogFactory;
58
59
60 /**
61  * Client implementation of the <code>javax.jms.QueueBrowser</code> interface.
62  *
63  * @version $Revision: 1.2 $ $Date: 2005/03/18 03:36:37 $
64  * @author <a HREF="mailto:jima@comware.com.au">Jim Alateras</a>
65  * @author <a HREF="mailto:tma@netspace.net.au">Tim Anderson</a>
66  */

67 class JmsQueueBrowser
68     extends JmsMessageConsumer
69     implements QueueBrowser JavaDoc, Enumeration JavaDoc {
70
71     /**
72      * Caches a collection of messages, which are used during enumeration.
73      */

74     private LinkedList JavaDoc _messages = new LinkedList JavaDoc();
75
76     /**
77      * The logger.
78      */

79     private static final Log _log = LogFactory.getLog(JmsQueueBrowser.class);
80
81
82     /**
83      * Construct a new <code>QueueBrowser</code>.
84      *
85      * @param session the session that created this instance
86      * @param consumerId the identity of this consumer
87      * @param queue the queue to browse
88      * @param selector the message selector. May be <code>null</code>
89      */

90     public JmsQueueBrowser(JmsSession session, long consumerId,
91                            Queue JavaDoc queue, String JavaDoc selector) {
92         super(session, consumerId, queue, selector);
93     }
94
95     /**
96      * Returns the queue associated with this browser.
97      *
98      * @return the queue associated with this browser
99      */

100     public Queue JavaDoc getQueue() {
101         return (Queue JavaDoc) getDestination();
102     }
103
104     /**
105      * Returns an enumeration for browsing the current queue messages in the
106      * order they would be received.
107      *
108      * @return an enumeration for browsing the messages
109      */

110     public Enumeration JavaDoc getEnumeration() {
111         return this;
112     }
113
114     /**
115      * Close this browser.
116      *
117      * @throws JMSException if the browser can't be closed
118      */

119     public void close() throws JMSException JavaDoc {
120         super.close();
121         if (_messages != null) {
122             _messages.clear();
123             _messages = null;
124         }
125     }
126
127     /**
128      * Handle asynchronous messages. It is invalid to call this method -
129      * doing so results in a <code>RuntimeException</code>
130      *
131      * @param message the message received
132      */

133     public void onMessage(Message JavaDoc message) {
134         throw new RuntimeException JavaDoc(
135             "JmsQueueBrowsder.onMessage() has been called");
136     }
137
138     /**
139      * Determines if there are more messages to browse.
140      *
141      * @return <code>true</code> if there are more messages to browse
142      */

143     public boolean hasMoreElements() {
144         return !isEmpty();
145     }
146
147     /**
148      * Returns the next message.
149      *
150      * @return the next message
151      */

152     public synchronized Object JavaDoc nextElement() {
153         if (!isEmpty()) {
154             return _messages.removeFirst();
155         }
156
157         return null;
158     }
159
160     /**
161      * If there are no more messages on the server, bring across another
162      * batch of them. If there are no more then return false.
163      * <p>
164      * Return a max of 20 at a time..although we should make it configurable
165      *
166      * @return <code>true</code> is empty; <code>false</code> oherwise
167      */

168     private boolean isEmpty() {
169         final int count = 20;
170         // check that the local cache is not empty first
171
if (!_messages.isEmpty()) {
172             return false;
173         }
174
175         // now try and retrieve a batch of messages from the server. If there
176
// are no messages in place then return true otherwise retrieve the
177
// messages, place them in the local cache and return not empty.
178
List JavaDoc messages = null;
179         try {
180             messages = getSession().browse(getConsumerId(), count);
181         } catch (JMSException JavaDoc exception) {
182             _log.error("Error in JmsQueueBrowser.isEmpty", exception);
183         }
184
185         if (messages != null) {
186             _messages.addAll(messages);
187         }
188
189         return _messages.isEmpty();
190     }
191
192 }
193
Popular Tags