KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exolab > jms > messagemgr > AbstractConsumerMessageHandle


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 2005 (C) Exoffice Technologies Inc. All Rights Reserved.
42  *
43  * $Id: AbstractConsumerMessageHandle.java,v 1.1 2005/03/18 03:58:38 tanderson Exp $
44  */

45
46 package org.exolab.jms.messagemgr;
47
48 import java.sql.Connection JavaDoc;
49 import javax.jms.JMSException JavaDoc;
50 import javax.jms.MessageConsumer JavaDoc;
51
52 import org.exolab.jms.client.JmsDestination;
53 import org.exolab.jms.message.MessageImpl;
54 import org.exolab.jms.persistence.DatabaseService;
55 import org.exolab.jms.persistence.PersistenceException;
56
57
58 /**
59  * A {@link MessageHandle} for a consumer.
60  *
61  * @author <a HREF="mailto:tma@netspace.net.au">Tim Anderson</a>
62  * @version $Revision: 1.1 $ $Date: 2005/03/18 03:58:38 $
63  */

64 abstract class AbstractConsumerMessageHandle implements MessageHandle {
65
66     /**
67      * The underlying handle.
68      */

69     private final MessageHandle _handle;
70
71     /**
72      * The consumer's identity.
73      */

74     private long _consumerId;
75
76     /**
77      * The consumer's persistent identity.
78      */

79     private final String JavaDoc _persistentId;
80
81     /**
82      * Detetmines if this handle is persistent.
83      */

84     private boolean _persistent;
85
86
87     /**
88      * Construct a new <code>AbstractConsumerMessageHandle</code>.
89      *
90      * @param handle the underlying handle
91      * @param consumer the consumer of the handle
92      * @throws JMSException if the underlying message can't be referenced
93      */

94     public AbstractConsumerMessageHandle(MessageHandle handle,
95                                          ConsumerEndpoint consumer)
96             throws JMSException JavaDoc {
97         this(handle, consumer.getId(), consumer.getPersistentId());
98     }
99
100     /**
101      * Construct a new <code>AbstractConsumerMessageHandle</code>.
102      *
103      * @param handle the underlying handle
104      * @param persistentId the persistent identity of the consumer. May be
105      * <code>null</code>
106      * @throws JMSException if the underlying message can't be referenced
107      */

108     public AbstractConsumerMessageHandle(MessageHandle handle,
109                                          String JavaDoc persistentId)
110             throws JMSException JavaDoc {
111         this(handle, -1L, persistentId);
112     }
113
114     /**
115      * Construct a new <code>AbstractConsumerMessageHandle</code>.
116      *
117      * @param handle the underlying handle
118      * @param consumerId the consumer identifier
119      * @param persistentId the persistent identity of the consumer. May be
120      * <code>null</code>
121      * @throws JMSException if the underlying message can't be referenced
122      */

123     protected AbstractConsumerMessageHandle(MessageHandle handle, long consumerId,
124                                             String JavaDoc persistentId)
125             throws JMSException JavaDoc {
126         if (handle == null) {
127             throw new IllegalArgumentException JavaDoc("Argument 'handle' is null");
128         }
129         _handle = handle;
130         _consumerId = consumerId;
131         _persistentId = persistentId;
132         _handle.getMessageRef().reference();
133     }
134
135     /**
136      * Returns the message identifier.
137      *
138      * @return the message identifier
139      */

140     public String JavaDoc getMessageId() {
141         return _handle.getMessageId();
142     }
143
144     /**
145      * Indicates if a message has been delivered to a {@link MessageConsumer},
146      * but not acknowledged.
147      *
148      * @param delivered if <code>true</code> indicates that an attempt has been
149      * made to deliver the message
150      */

151     public void setDelivered(boolean delivered) {
152         _handle.setDelivered(delivered);
153     }
154
155     /**
156      * Returns if an attempt has already been made to deliver the message.
157      *
158      * @return <code>true</code> if delivery has been attempted
159      */

160     public boolean getDelivered() {
161         return _handle.getDelivered();
162     }
163
164     /**
165      * Returns the priority of the message.
166      *
167      * @return the message priority
168      */

169     public int getPriority() {
170         return _handle.getPriority();
171     }
172
173     /**
174      * Returns the time that the corresponding message was accepted, in
175      * milliseconds.
176      *
177      * @return the time that the corresponding message was accepted
178      */

179     public long getAcceptedTime() {
180         return _handle.getAcceptedTime();
181     }
182
183     /**
184      * Returns the time that the message expires.
185      *
186      * @return the expiry time
187      */

188     public long getExpiryTime() {
189         return _handle.getExpiryTime();
190     }
191
192     /**
193      * Determines if the message has expired.
194      *
195      * @return <code>true</code> if the message has expired, otherwise
196      * <code>false</code>
197      */

198     public boolean hasExpired() {
199         return _handle.hasExpired();
200     }
201
202     /**
203      * Returns the handle's sequence number.
204      *
205      * @return the sequence number
206      */

207     public long getSequenceNumber() {
208         return _handle.getSequenceNumber();
209     }
210
211     /**
212      * Returns the message destination.
213      *
214      * @return the message destination
215      */

216     public JmsDestination getDestination() {
217         return _handle.getDestination();
218     }
219
220     /**
221      * Returns the consumer identity associated with the message.
222      *
223      * @return the consumer identity associated with the message, or *
224      * <code>-1</code> if the message isn't associated with a consumer
225      */

226     public long getConsumerId() {
227         return _consumerId;
228     }
229
230     /**
231      * Returns the name of the consumer endpoint that owns this handle. If it is
232      * set, then a consumer owns it exclusively, otherwise the handle may be
233      * shared across a number of consumers
234      *
235      * @return the consumer name, or <code>null</code>
236      */

237     public String JavaDoc getConsumerPersistentId() {
238         return _persistentId;
239     }
240
241     /**
242      * Returns the connection identity associated with the message.
243      *
244      * @return the connection identity associated with the message, or
245      * <code>-1</code> if the message isn't associated with a
246      * connection
247      */

248     public long getConnectionId() {
249         return _handle.getConnectionId();
250     }
251
252     /**
253      * Determines if the handle is persistent.
254      *
255      * @return <code>true</code> if the handle is persistent; otherwise
256      * <code>false</code>
257      */

258     public boolean isPersistent() {
259         return _persistent;
260     }
261
262     /**
263      * Returns the message associated with this handle.
264      *
265      * @return the associated message, or <code>null</code> if the handle is no
266      * longer valid
267      * @throws JMSException for any error
268      */

269     public MessageImpl getMessage() throws JMSException JavaDoc {
270         return _handle.getMessage();
271     }
272
273     /**
274      * Makes the handle persistent.
275      *
276      * @param connection the connection to use
277      * @throws PersistenceException for any persistence error
278      */

279     public void add(Connection JavaDoc connection) throws PersistenceException {
280         DatabaseService.getAdapter().addMessageHandle(connection, this);
281         _persistent = true;
282     }
283
284     /**
285      * Update the persistent handle.
286      *
287      * @param connection the connection to use
288      * @throws PersistenceException for any persistence error
289      */

290     public void update(Connection JavaDoc connection) throws PersistenceException {
291         DatabaseService.getAdapter().updateMessageHandle(connection, this);
292     }
293
294     /**
295      * Destroy this handle. If this is the last handle to reference the message,
296      * also destroys the message
297      *
298      * @throws JMSException for any error
299      */

300     public void destroy() throws JMSException JavaDoc {
301         _handle.destroy();
302     }
303
304     /**
305      * Destroy this handle. If this is the last handle to reference the message,
306      * also destroys the message
307      *
308      * @param connection the connection to use
309      * @throws JMSException for any error
310      * @throws PersistenceException for any persistence error
311      */

312     public void destroy(Connection JavaDoc connection) throws JMSException JavaDoc,
313             PersistenceException {
314         DatabaseService.getAdapter().removeMessageHandle(connection, this);
315         _handle.destroy(connection);
316         _persistent = false;
317     }
318
319     /**
320      * Returns the message reference.
321      *
322      * @return the message reference, or <code>null</code> if none has been set
323      */

324     public MessageRef getMessageRef() {
325         return _handle.getMessageRef();
326     }
327
328     /**
329      * Set the consumer identifier.
330      *
331      * @param consumerId the consumer identifier
332      */

333     protected void setConsumerId(long consumerId) {
334         _consumerId = consumerId;
335     }
336
337     /**
338      * Indicates if the handle is persistent.
339      *
340      * @param persistent if <code>true</code> indicates the handle is
341      * persistent
342      */

343     protected void setPersistent(boolean persistent) {
344         _persistent = persistent;
345     }
346
347 }
348
349
Popular Tags