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-2003 (C) Exoffice Technologies Inc. All Rights Reserved.
42   *
43   * $Id: DurableConsumerEndpoint.java,v 1.22 2003/09/25 11:24:16 tanderson Exp $
44   *
45   * Date         Author  Changes
46   * 3/1/2001     jima    Created
47   */
48  package org.exolab.jms.messagemgr;
49  
50  import java.sql.Connection;
51  import java.util.Enumeration;
52  import java.util.Vector;
53  
54  import javax.jms.JMSException;
55  import javax.jms.Session;
56  import javax.transaction.TransactionManager;
57  
58  import org.apache.commons.logging.Log;
59  import org.apache.commons.logging.LogFactory;
60  
61  import org.exolab.jms.client.JmsTopic;
62  import org.exolab.jms.message.MessageHandle;
63  import org.exolab.jms.message.MessageImpl;
64  import org.exolab.jms.persistence.DatabaseService;
65  import org.exolab.jms.persistence.PersistenceException;
66  import org.exolab.jms.scheduler.Scheduler;
67  import org.exolab.jms.server.JmsServerSession;
68  
69  
70  /***
71   * A durable subscriber extends {@link ConsumerEndpoint} and is only associated
72   * with a Topic destination. The state of the durable subscriber is
73   * maintained across server invocations by the persistent layer
74   *
75   * @version     $Revision: 1.22 $ $Date: 2003/09/25 11:24:16 $
76   * @author      <a href="mailto:jima@exoffice.com">Jim Alateras</a>
77   */
78  public class DurableConsumerEndpoint
79      extends TopicConsumerEndpoint {
80  
81      /***
82       * The well-known name of the durable subscriber
83       */
84      private String _name = null;
85  
86      /***
87       * The logger
88       */
89      private static final Log _log =
90          LogFactory.getLog(DurableConsumerEndpoint.class);
91  
92  
93      /***
94       * Create an durable consumer endpoint. This is a well-known
95       * endpoint which can only be activated by one client at any one time.
96       * <p>
97       * DurableConsumerEndpoints are always loaded in memory, whether they
98       * are active or inactive. When they are inactive they simply process
99       * persistent messages. Non-persistent message are ignored when the
100      * durable consumer is inactive.
101      *
102      * @param session - the creating session
103      * @param clientId - uniquely identifies the remote client within session
104      * @param topic - destination it is subscribing too
105      * @param name - the well known name of the durable subscriber
106      * @param selector - the selector attached to the consumer, if any.
107      * @param scheduler - entity that will delivery messages asynch.
108      * @throws JMSException
109      */
110     DurableConsumerEndpoint(JmsServerSession session, long clientId,
111                             JmsTopic topic, String name, String selector,
112                             Scheduler scheduler) throws JMSException {
113         super(session, clientId, topic, selector, scheduler);
114         _name = name;
115 
116         // call the persistence adapter to retrieve the state of  this
117         // durable consumer. The adaptor will return a linked list of
118         // PersistentHandle objects which represent unsent and sent
119         // messages.
120         Connection connection = null;
121         try {
122             connection = DatabaseService.getConnection();
123 
124             // remove expired messages
125             DatabaseService.getAdapter().removeExpiredMessageHandles(
126                 connection, _name);
127             Vector handles = DatabaseService.getAdapter().getMessageHandles(
128                 connection, getDestination(), _name);
129             connection.commit();
130             // iterate over each handle and add them to the list of messages
131             // for the durable consumer
132             Enumeration iter = handles.elements();
133             while (iter.hasMoreElements()) {
134                 addMessage((MessageHandle) iter.nextElement());
135             }
136         } catch (PersistenceException exception) {
137             if (connection != null) {
138                 try {
139                     connection.rollback();
140                 } catch (Exception nested) {
141                     // ignore
142                 }
143             }
144             throw new JMSException("DurableConusmerEndpoint not activated " +
145                 exception);
146         } catch (Exception exception) {
147             // rethrow as a JMSException
148             throw new JMSException("DurableConusmerEndpoint not activated " +
149                 exception);
150         } finally {
151             if (connection != null) {
152                 try {
153                     connection.close();
154                 } catch (Exception nested) {
155                     // ignore
156                 }
157             }
158         }
159     }
160 
161     /***
162      * Returns the persistent identifier for this consumer.<p/>
163      * This is the identity of the consumer which is persistent across
164      * subscriptions and server restarts. <p/>
165      *
166      * This implementation returns the consumer name.
167      *
168      * @return the persistent identifier for this consumer
169      */
170     public String getPersistentId() {
171         return getName();
172     }
173 
174     /***
175      * Returns the durable subscription name
176      *
177      * @return the durable subscription name
178      */
179     public String getName() {
180         return _name;
181     }
182 
183 } //-- DurableConsumerEndpoint