View Javadoc

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.4 2005/10/20 14:20:27 tanderson Exp $
44   */
45  package org.exolab.jms.messagemgr;
46  
47  import org.apache.commons.logging.Log;
48  import org.apache.commons.logging.LogFactory;
49  import org.exolab.jms.client.JmsDestination;
50  import org.exolab.jms.message.MessageImpl;
51  import org.exolab.jms.persistence.DatabaseService;
52  import org.exolab.jms.persistence.PersistenceException;
53  
54  import javax.jms.JMSException;
55  import java.sql.Connection;
56  
57  
58  /***
59   * A persistent message handle extends {@link MessageHandle} and references a
60   * persistent message. These messages can be discarded from the cache and later
61   * faulted in.
62   *
63   * @author <a href="mailto:jima@comware.com.au">Jim Alateras</a>
64   * @author <a href="mailto:tma@netspace.net.au">Tim Anderson</a>
65   * @version $Revision: 1.4 $ $Date: 2005/10/20 14:20:27 $
66   */
67  public class PersistentMessageHandle extends AbstractMessageHandle {
68  
69      /***
70       * The persistent identity of the message consumer.
71       */
72      private final String _persistentId;
73  
74      /***
75       * The logger.
76       */
77      private static final Log _log
78              = LogFactory.getLog(PersistentMessageHandle.class);
79  
80  
81      /***
82       * Construct a new <code>PersistentMessageHandle</code>, for a particular
83       * consumer.
84       *
85       * @param message      the message to construct the handle for
86       * @param persistentId the persistent identity of the consumer.
87       *                     May be <code>null</code>.
88       * @throws JMSException for any error
89       */
90      public PersistentMessageHandle(MessageImpl message, String persistentId)
91              throws JMSException {
92          this(message.getJMSMessageID(), message.getJMSPriority(),
93               message.getAcceptedTime(), message.getSequenceNumber(),
94               message.getJMSExpiration(),
95               (JmsDestination) message.getJMSDestination(), persistentId);
96      }
97  
98      /***
99       * Construct a new <code>PersistentMessageHandle</code>.
100      *
101      * @param messageId      the message identifier
102      * @param priority       the message priority
103      * @param acceptedTime   the time the message was accepted by the server
104      * @param sequenceNumber the message sequence number
105      * @param expiryTime     the time that the message will expire
106      */
107     public PersistentMessageHandle(String messageId, int priority,
108                                    long acceptedTime, long sequenceNumber,
109                                    long expiryTime,
110                                    JmsDestination destination) {
111         this(messageId, priority, acceptedTime, sequenceNumber, expiryTime,
112              destination, null);
113     }
114 
115     /***
116      * Construct a new <code>PersistentMessageHandle</code>, for a particular
117      * consumer.
118      *
119      * @param messageId      the message identifier
120      * @param priority       the message priority
121      * @param acceptedTime   the time the message was accepted by the server
122      * @param sequenceNumber the message sequence number
123      * @param expiryTime     the time that the message will expire
124      * @param persistentId   the persistent identity of the consumer. May be
125      *                       <code>null</code>.
126      */
127     public PersistentMessageHandle(String messageId, int priority,
128                                    long acceptedTime, long sequenceNumber,
129                                    long expiryTime,
130                                    JmsDestination destination,
131                                    String persistentId) {
132         super(messageId, priority, acceptedTime, sequenceNumber, expiryTime,
133               destination);
134         _persistentId = persistentId;
135     }
136 
137     /***
138      * Determines if the handle is persistent.
139      *
140      * @return <code>true</code>
141      */
142     public boolean isPersistent() {
143         return true;
144     }
145 
146     /***
147      * Make the handle persistent.
148      *
149      * @throws JMSException for any persistence error
150      */
151     public void add() throws JMSException {
152         try {
153             DatabaseService service = DatabaseService.getInstance();
154             Connection connection = service.getConnection();
155             service.getAdapter().addMessageHandle(connection, this);
156         } catch (PersistenceException exception) {
157             final String msg = "Failed to make handle persistent";
158             _log.error(msg, exception);
159             throw new JMSException(msg + ": " + exception.getMessage());
160         }
161     }
162 
163     /***
164      * Update this handle.
165      *
166      * @throws JMSException for any persistence error
167      */
168     public void update() throws JMSException {
169         try {
170             DatabaseService service = DatabaseService.getInstance();
171             Connection connection = service.getConnection();
172             service.getAdapter().updateMessageHandle(connection, this);
173         } catch (PersistenceException exception) {
174             final String msg = "Failed to update persistent handle";
175             _log.error(msg, exception);
176             throw new JMSException(msg + ": " + exception.getMessage());
177         }
178     }
179 
180     /***
181      * Reference a message.
182      *
183      * @throws JMSException for any error
184      */
185     public void reference(MessageRef reference) throws JMSException {
186         reference.reference();
187         setMessageRef(reference);
188     }
189 
190     /***
191      * Returns the persistent identity of the the consumer endpoint that owns
192      * this handle. If it is set, then a consumer owns it exclusively, otherwise
193      * the handle may be shared across a number of consumers.
194      *
195      * @return the consumer's persistent identity, or <code>null</code>
196      */
197     public String getConsumerPersistentId() {
198         return _persistentId;
199     }
200 
201     /***
202      * Destroy this handle. If this is the last handle to reference the message,
203      * also destroys the message.
204      *
205      * @throws JMSException for any error
206      */
207     public void destroy() throws JMSException {
208         try {
209             DatabaseService service = DatabaseService.getInstance();
210             Connection connection = service.getConnection();
211             service.getAdapter().removeMessageHandle(connection, this);
212         } catch (PersistenceException exception) {
213             final String msg = "Failed to destroy persistent handle";
214             _log.error(msg, exception);
215             throw new JMSException(msg + ": " + exception.getMessage());
216         }
217         super.destroy();
218     }
219 
220 }
221