KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > presumo > jms > client > JmsQueueBrowser


1 /**
2  * This file is part of Presumo.
3  *
4  * Presumo is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * Presumo is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with Presumo; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  *
19  * Copyright 2001 Dan Greff
20  */

21 package com.presumo.jms.client;
22
23 import com.presumo.jms.message.JmsMessage;
24
25 import java.util.Enumeration JavaDoc;
26 import java.util.NoSuchElementException JavaDoc;
27
28 import javax.jms.JMSException JavaDoc;
29 import javax.jms.Queue JavaDoc;
30 import javax.jms.QueueBrowser JavaDoc;
31 import javax.jms.QueueReceiver JavaDoc;
32
33 /**
34  * <p>Implementation of the <code>javax.jms.QueueBrowser</code> interface.
35  *
36  * @see javax.jms.QueueBrowser
37  */

38 public final class JmsQueueBrowser implements QueueBrowser JavaDoc
39 {
40   /**
41    * The default time (in milliseconds) the Enumeration will wait for the
42    * next message before it decides that it has no more messages. The value
43    * is 10000 milliseconds, or 10 seconds.
44    *
45    * @see JmsQueueBrowser#setTimeout
46    */

47   public static final int DEFAULT_TIMEOUT = 10000;
48   public static final String JavaDoc END_OF_STREAM_PROP = "JMSX_END_OF_STREAM";
49   
50
51     /////////////////////////////////////////////////////////////////////////
52
// Private Instance Variables //
53
/////////////////////////////////////////////////////////////////////////
54
private QueueReceiver JavaDoc receiver;
55   private boolean browserCreated;
56   private int timeout = DEFAULT_TIMEOUT;
57   
58     /////////////////////////////////////////////////////////////////////////
59
// Constructors //
60
/////////////////////////////////////////////////////////////////////////
61

62   JmsQueueBrowser(JmsQueueReceiver receiver)
63   {
64     this.receiver = receiver;
65   }
66
67     //////////////////////////////////////////////////////////////////////////
68
// Public Methods //
69
//////////////////////////////////////////////////////////////////////////
70

71   /**
72    * Sets the amount of time the enumeration will wait for the next message
73    * before it determines that it has no more messages.
74    *
75    * @see JmsQueueBrowser#DEFAULT_TIMEOUT
76    */

77   public void setTimeout(int timeout)
78   {
79     this.timeout = timeout;
80   }
81   
82   public Queue JavaDoc getQueue() throws JMSException JavaDoc
83   {
84     return receiver.getQueue();
85   }
86
87   public String JavaDoc getMessageSelector() throws JMSException JavaDoc
88   {
89     return receiver.getMessageSelector();
90   }
91   
92   public void close() throws JMSException JavaDoc
93   {
94     receiver.close();
95   }
96   
97   public Enumeration JavaDoc getEnumeration() throws JMSException JavaDoc
98   {
99     if (browserCreated)
100       throw new JMSException JavaDoc("Only one Enumeration is allowed per QueueBrowser");
101     browserCreated = true;
102     return new BrowserEnumeration();
103   }
104
105
106     //////////////////////////////////////////////////////////////////////////
107
// Begin Inner Class BrowserEnumeration //
108
//////////////////////////////////////////////////////////////////////////
109

110   public class BrowserEnumeration implements Enumeration JavaDoc
111   {
112     private JmsMessage nextMessage;
113     private boolean endOfQueue;
114     
115     BrowserEnumeration() throws JMSException JavaDoc
116     {
117     }
118         
119     public boolean hasMoreElements()
120     {
121       if (nextMessage == null)
122         readNextMessage();
123       return (nextMessage != null);
124     }
125     
126     
127     public Object JavaDoc nextElement()
128     {
129       if (nextMessage == null) {
130         readNextMessage();
131         if (nextMessage == null)
132           throw new NoSuchElementException JavaDoc();
133       }
134       JmsMessage retval = nextMessage;
135       retval.acknowledge();
136       nextMessage = null;
137       return retval;
138     }
139     
140     
141     private void readNextMessage()
142     {
143       if (endOfQueue)
144         return;
145       try {
146         JmsMessage msg = (JmsMessage) receiver.receive(timeout);
147         if ( msg != null && !isEndOfQueueMsg(msg) )
148         {
149           nextMessage = msg;
150         }
151       } catch (JMSException JavaDoc jmsex) {}
152     }
153     
154     private boolean isEndOfQueueMsg(JmsMessage msg)
155     {
156       Boolean JavaDoc b = (Boolean JavaDoc) msg.getObjectProperty(END_OF_STREAM_PROP);
157       if (b != null) {
158         endOfQueue = b.booleanValue();
159       }
160       return endOfQueue;
161     }
162   }
163
164     //////////////////////////////////////////////////////////////////////////
165
// End Inner Class BrowserEnumeration //
166
//////////////////////////////////////////////////////////////////////////
167

168 }
169
Popular Tags