KickJava   Java API By Example, From Geeks To Geeks.

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


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

45 package org.exolab.jms.messagemgr;
46
47 import java.sql.Connection JavaDoc;
48 import javax.jms.JMSException JavaDoc;
49
50 import org.exolab.jms.client.JmsDestination;
51 import org.exolab.jms.persistence.DatabaseService;
52 import org.exolab.jms.persistence.PersistenceException;
53 import org.exolab.jms.persistence.SQLHelper;
54
55
56 /**
57  * A persistent message handle extends {@link MessageHandle} and references a
58  * persistent message. These messages can be discarded from the cache and later
59  * faulted in.
60  *
61  * @author <a HREF="mailto:jima@comware.com.au">Jim Alateras</a>
62  * @author <a HREF="mailto:tma@netspace.net.au">Tim Anderson</a>
63  * @version $Revision: 1.2 $ $Date: 2005/03/18 03:58:39 $
64  */

65 public class PersistentMessageHandle extends AbstractMessageHandle {
66
67     /**
68      * The persistent identity of the message consumer.
69      */

70     private final String JavaDoc _persistentId;
71
72
73     /**
74      * Construct a new <code>PersistentMessageHandle</code>.
75      *
76      * @param messageId the message identifier
77      * @param priority the message priority
78      * @param acceptedTime the time the message was accepted by the server
79      * @param sequenceNumber the message sequence number
80      * @param expiryTime the time that the message will expire
81      */

82     public PersistentMessageHandle(String JavaDoc messageId, int priority,
83                                    long acceptedTime, long sequenceNumber,
84                                    long expiryTime,
85                                    JmsDestination destination) {
86         this(messageId, priority, acceptedTime, sequenceNumber, expiryTime,
87               destination, null);
88     }
89
90     /**
91      * Construct a new <code>PersistentMessageHandle</code>, for a particular
92      * consumer.
93      *
94      * @param messageId the message identifier
95      * @param priority the message priority
96      * @param acceptedTime the time the message was accepted by the server
97      * @param sequenceNumber the message sequence number
98      * @param expiryTime the time that the message will expire
99      * @param persistentId the persistent identity of the consumer
100      */

101     public PersistentMessageHandle(String JavaDoc messageId, int priority,
102                                    long acceptedTime, long sequenceNumber,
103                                    long expiryTime,
104                                    JmsDestination destination,
105                                    String JavaDoc persistentId) {
106         super(messageId, priority, acceptedTime, sequenceNumber, expiryTime,
107               destination);
108         if (persistentId == null) {
109             throw new IllegalArgumentException JavaDoc(
110                     "Argument 'persistentId' is null");
111         }
112         _persistentId = persistentId;
113     }
114
115     /**
116      * Determines if the handle is persistent.
117      *
118      * @return <code>true</code>
119      */

120     public boolean isPersistent() {
121         return true;
122     }
123
124     /**
125      * Make the handle persistent.
126      *
127      * @param connection the connection to use
128      * @throws PersistenceException for any persistence error
129      */

130     public void add(Connection JavaDoc connection) throws PersistenceException {
131         DatabaseService.getAdapter().addMessageHandle(connection, this);
132     }
133
134     /**
135      * Update this handle.
136      *
137      * @param connection the connection to use
138      * @throws PersistenceException for any persistence error
139      */

140     public void update(Connection JavaDoc connection) throws PersistenceException {
141         DatabaseService.getAdapter().updateMessageHandle(connection, this);
142     }
143
144     /**
145      * Reference a message.
146      *
147      * @throws JMSException for any error
148      */

149     public void reference(MessageRef reference) throws JMSException JavaDoc {
150         reference.reference();
151         setMessageRef(reference);
152     }
153
154     /**
155      * Destroy this handle. If this is the last handle to reference the message,
156      * also destroys the message.
157      *
158      * @throws JMSException for any error
159      */

160     public void destroy() throws JMSException JavaDoc {
161         Connection JavaDoc connection = null;
162
163         try {
164             connection = DatabaseService.getConnection();
165             destroy(connection);
166             connection.commit();
167         } catch (Exception JavaDoc exception) {
168             SQLHelper.rollback(connection);
169             throw new JMSException JavaDoc(exception.getMessage());
170         } finally {
171             SQLHelper.close(connection);
172         }
173     }
174
175     /**
176      * Returns the persistent identity of the the consumer endpoint that owns
177      * this handle. If it is set, then a consumer owns it exclusively, otherwise
178      * the handle may be shared across a number of consumers.
179      *
180      * @return the consumer's persistent identity, or <code>null</code>
181      */

182     public String JavaDoc getConsumerPersistentId() {
183         return _persistentId;
184     }
185
186
187     /**
188      * Destroy this handle. If this is the last handle to reference the message,
189      * also destroys the message.
190      *
191      * @param connection the connection to use
192      * @throws JMSException for any error
193      * @throws PersistenceException for any persistence error
194      */

195     public void destroy(Connection JavaDoc connection)
196             throws JMSException JavaDoc, PersistenceException {
197         DatabaseService.getAdapter().removeMessageHandle(connection, this);
198         super.destroy(connection);
199     }
200 }
201
202
Popular Tags