KickJava   Java API By Example, From Geeks To Geeks.

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


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: DurableConsumerEndpoint.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 java.util.Iterator JavaDoc;
49 import java.util.List JavaDoc;
50 import javax.jms.InvalidSelectorException JavaDoc;
51 import javax.jms.JMSException JavaDoc;
52
53 import org.apache.commons.logging.Log;
54 import org.apache.commons.logging.LogFactory;
55
56 import org.exolab.jms.client.JmsTopic;
57 import org.exolab.jms.persistence.DatabaseService;
58 import org.exolab.jms.persistence.SQLHelper;
59 import org.exolab.jms.scheduler.Scheduler;
60 import org.exolab.jms.server.JmsServerSession;
61 import org.exolab.jms.server.JmsServerSession;
62
63
64 /**
65  * A {@link ConsumerEndpoint} for durable topic consumers.
66  * The state of durable topic consumers is maintained across server
67  * invocations by the persistent layer.
68  *
69  * @author <a HREF="mailto:jima@comware.com.au">Jim Alateras</a>
70  * @author <a HREF="mailto:tma@netspace.net.au">Tim Anderson</a>
71  * @version $Revision: 1.2 $ $Date: 2005/03/18 03:58:39 $
72  */

73 public class DurableConsumerEndpoint
74         extends AbstractTopicConsumerEndpoint {
75
76     /**
77      * The persistent name of the durable subscriber
78      */

79     private final String JavaDoc _name;
80
81     /**
82      * The logger
83      */

84     private static final Log _log
85             = LogFactory.getLog(DurableConsumerEndpoint.class);
86
87     /**
88      * Create an durable consumer endpoint. This is a well-known endpoint which
89      * can only be activated by one client at any one time.
90      * <p/>
91      * DurableConsumerEndpoints are always loaded in memory, whether they are
92      * active or inactive. When they are inactive they simply process persistent
93      * messages. Non-persistent message are ignored when the durable consumer is
94      * inactive.
95      *
96      * @param consumerId the identity of this consumer
97      * @param session the owning session
98      * @param topic the topic to access
99      * @param name the well known name of the durable subscriber
100      * @param selector the message selector. May be <code>null</code>
101      * @param noLocal if true, inhibits the delivery of messages published by
102      * its own connection.
103      * @param scheduler used to schedule asynchronous message delivery.
104      * @throws InvalidSelectorException if the selector is invalid
105      * @throws JMSException if the destination caches can't be
106      * constructed
107      */

108     public DurableConsumerEndpoint(long consumerId, JmsServerSession session,
109                                    JmsTopic topic, String JavaDoc name, String JavaDoc selector,
110                                    boolean noLocal, Scheduler scheduler)
111             throws InvalidSelectorException JavaDoc, JMSException JavaDoc {
112         super(consumerId, session, topic, selector, noLocal, scheduler);
113         _name = name;
114
115         // call the persistence adapter to retrieve the state of this consumer
116
Connection JavaDoc connection = null;
117         try {
118             connection = DatabaseService.getConnection();
119
120             // remove expired messages
121
DatabaseService.getAdapter().removeExpiredMessageHandles(
122                     connection, _name);
123
124             TopicDestinationCache cache = (TopicDestinationCache)
125                     DestinationManager.instance().getDestinationCache(topic);
126             // @todo - broken for wildcard subscriptions
127
// getMessageHandles() needs to return all handles for a given
128
// subscription name
129
List JavaDoc handles = cache.getDurableMessageHandles(_name, connection);
130
131             connection.commit();
132
133             // iterate over each handle and add them to the list of messages
134
// for the durable consumer
135
Iterator JavaDoc iterator = handles.iterator();
136             while (iterator.hasNext()) {
137                 MessageHandle handle = (MessageHandle) iterator.next();
138                 TopicConsumerMessageHandle consumer =
139                         new TopicConsumerMessageHandle(handle, this);
140                 addMessage(consumer);
141             }
142         } catch (Exception JavaDoc exception) {
143             SQLHelper.rollback(connection);
144             String JavaDoc msg = "Failed to create durable consumer, name=" + name
145                     + ", for topic=" + topic.getName();
146             _log.error(msg, exception);
147             throw new JMSException JavaDoc(msg + ": " + exception.getMessage());
148         } finally {
149             SQLHelper.close(connection);
150         }
151
152         init();
153     }
154
155     /**
156      * Determines if this is a persistent or non-persistent consumer.
157      * <p/>
158      * If persistent, then the consumer is persistent accross subscriptions and
159      * server restarts, and {@link #getPersistentId} returns a non-null value
160      *
161      * @return <code>true</code>
162      */

163     public boolean isPersistent() {
164         return true;
165     }
166
167     /**
168      * Returns the persistent identifier for this consumer.
169      * <p/>
170      * This is the identity of the consumer which is persistent across
171      * subscriptions and server restarts.
172      * <p/>
173      * This implementation returns the consumer name.
174      *
175      * @return the persistent identifier for this consumer
176      */

177     public String JavaDoc getPersistentId() {
178         return _name;
179     }
180
181
182 }
183
Popular Tags