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 tma@netspace.net.au.
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-2004 (C) Exoffice Technologies Inc. All Rights Reserved.
42 *
43 * $Id: AbstractMessageReceiver.java,v 1.4 2004/01/31 13:44:24 tanderson Exp $
44 */
45 package org.exolab.jmscts.core;
46
47 import javax.jms.Destination;
48 import javax.jms.JMSException;
49 import javax.jms.MessageConsumer;
50 import javax.jms.QueueReceiver;
51 import javax.jms.Session;
52 import javax.jms.TopicSession;
53 import javax.jms.TopicSubscriber;
54
55
56 /***
57 * Helper that implements behaviour common to both AsynchronousReceiver and
58 * SynchronousReceiver
59 *
60 * @version $Revision: 1.4 $ $Date: 2004/01/31 13:44:24 $
61 * @author <a href="mailto:tma@netspace.net.au">Tim Anderson</a>
62 * @see MessageReceiver
63 * @see AsynchronousReceiver
64 * @see SynchronousReceiver
65 * @see MessageBrowser
66 */
67 abstract class AbstractMessageReceiver implements MessageReceiver {
68
69 /***
70 * The session that owns the consumer
71 */
72 private Session _session = null;
73
74 /***
75 * The consumer used to receive messages
76 */
77 private MessageConsumer _consumer = null;
78
79 /***
80 * The name of the subscriber, if the consumer is a durable topic
81 * subscriber
82 */
83 private String _name = null;
84
85 /***
86 * Construct an instance with the consumer to receive messages with
87 *
88 * @param session the session that created the consumer
89 * @param consumer the consumer used to receive messages
90 * @param name the name of the subscriber, for durable topic subscribers.
91 * May be null.
92 */
93 public AbstractMessageReceiver(Session session, MessageConsumer consumer,
94 String name) {
95 if (session == null) {
96 throw new IllegalArgumentException("Argument session is null");
97 }
98 if (consumer == null) {
99 throw new IllegalArgumentException("Argument consumer is null");
100 }
101 _session = session;
102 _consumer = consumer;
103 _name = name;
104 }
105
106 /***
107 * Return the destination associated with the MessageConsumer
108 *
109 * @return the destination to receive messages from
110 * @throws JMSException if the operation fails
111 */
112 public Destination getDestination() throws JMSException {
113 Destination result = null;
114 if (_consumer instanceof QueueReceiver) {
115 result = ((QueueReceiver) _consumer).getQueue();
116 } else {
117 result = ((TopicSubscriber) _consumer).getTopic();
118 }
119 return result;
120 }
121
122 /***
123 * Returns the message selector associated with the MessageConsumer
124 *
125 * @return the message selector
126 * @throws JMSException if the operation fails
127 */
128 public String getSelector() throws JMSException {
129 return _consumer.getMessageSelector();
130 }
131
132 /***
133 * Returns the name of the subscriber, if the consumer is a durable topic
134 * subscriber
135 *
136 * @return the name of the subscriber, or <code>null</code> if the
137 * consumer is not a durable topic subscriber
138 */
139 public String getName() {
140 return _name;
141 }
142
143 /***
144 * Returns the no-local value, if the consumer is a topic subscriber
145 *
146 * @return the no-local value, if the consumer is a topic subscriber;
147 * otherwise <code>false</code>
148 * @throws JMSException if the operation fails
149 */
150 public boolean getNoLocal() throws JMSException {
151 boolean result = false;
152 if (_consumer instanceof TopicSubscriber) {
153 result = ((TopicSubscriber) _consumer).getNoLocal();
154 }
155 return result;
156 }
157
158 /***
159 * Close the underlying MessageConsumer
160 *
161 * @throws JMSException if the operation fails
162 */
163 public void close() throws JMSException {
164 if (_consumer != null) {
165 _consumer.close();
166 _consumer = null;
167 }
168 }
169
170 /***
171 * Close the underlying MessageConsumer and if necessary, unsubscribe it
172 * from the session.
173 *
174 * @throws JMSException if the operation fails
175 */
176 public void remove() throws JMSException {
177 close();
178 if (_name != null && _session instanceof TopicSession) {
179 ((TopicSession) _session).unsubscribe(_name);
180 }
181 }
182
183 /***
184 * Returns the underlying message consumer
185 *
186 * @return the underlying message consumer
187 */
188 protected MessageConsumer getConsumer() {
189 return _consumer;
190 }
191
192 }
This page was automatically generated by Maven