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 2000-2004 (C) Exoffice Technologies Inc. All Rights Reserved.
42   */
43  package org.exolab.jms.client;
44  
45  import javax.jms.InvalidDestinationException;
46  import javax.jms.JMSException;
47  import javax.jms.Queue;
48  import javax.jms.QueueSession;
49  import javax.jms.QueueReceiver;
50  import javax.jms.QueueSender;
51  import javax.jms.QueueBrowser;
52  import javax.jms.TemporaryQueue;
53  
54  
55  /***
56   * Client implementation of the <code>javax.jms.QueueSession</code> interface
57   *
58   * @version     $Revision: 1.22 $ $Date: 2004/01/20 14:14:21 $
59   * @author      <a href="mailto:jima@exoffice.com">Jim Alateras</a>
60   */
61  class JmsQueueSession
62      extends JmsSession
63      implements QueueSession {
64  
65      /***
66       * Construct a new <code>JmsQueueSession</code>
67       *
68       * @param connection the owner of the session
69       * @param transacted if <code>true</code>, the session is transacted.
70       * @param ackMode indicates whether the consumer or the client will
71       * acknowledge any messages it receives. This parameter will be ignored if
72       * the session is transacted. Legal values are
73       * <code>Session.AUTO_ACKNOWLEDGE</code>,
74       * <code>Session.CLIENT_ACKNOWLEDGE</code> and
75       * <code>Session.DUPS_OK_ACKNOWLEDGE</code>.
76       * @throws JMSException if the session cannot be created
77       */
78      public JmsQueueSession(JmsQueueConnection connection, boolean transacted,
79                             int ackMode) throws JMSException {
80          super(connection, transacted, ackMode);
81      }
82  
83      /***
84       * Create a queue identity given its name
85       *
86       * @param queueName the queue name
87       * @return a queue with the given name.
88       * @throws JMSException if the queue can't be created
89       */
90      public synchronized Queue createQueue(String queueName)
91          throws JMSException {
92          ensureOpen();
93  
94          JmsQueue queue = null;
95  
96          if (queueName != null && queueName.length() > 0) {
97              queue = new JmsQueue(queueName);
98          } else {
99              throw new JMSException(
100                 "Cannot create a queue with null or empty name");
101         }
102 
103         return queue;
104     }
105 
106     /***
107      * Create a receiver to receive messages from the specified queue
108      *
109      * @param queue the queue to access
110      * @return the new receiver
111      * @throws JMSException if the receiver cannot be created
112      */
113     public QueueReceiver createReceiver(Queue queue) throws JMSException {
114         return createReceiver(queue, null);
115     }
116 
117     /***
118      * Create a receiver to receive messages from the specified queue
119      *
120      * @param queue the queue to access
121      * @param selector the message selector to filter messages.
122      * May be <code>null</code>
123      * @return the new receiver
124      * @throws JMSException if the receiver cannot be created
125      */
126     public synchronized QueueReceiver createReceiver(Queue queue,
127                                                      String selector)
128         throws JMSException {
129 
130         JmsQueueReceiver receiver = null;
131 
132         ensureOpen();
133 
134         if (queue == null) {
135             throw new InvalidDestinationException(
136                 "Cannot create receiver: argument 'queue' is null");
137         }
138 
139         // check to see if the queue is temporary. A temporary queue
140         // can only be used within the context of the owning connection
141         if (!checkForValidTemporaryDestination((JmsDestination) queue)) {
142             throw new InvalidDestinationException(
143                 "Cannot create a receiver for a temp queue that is not "
144                 + "bound to this connection");
145         }
146 
147         receiver = new JmsQueueReceiver(this, getNextConsumerId(),
148                                         (JmsQueue) queue, selector);
149         addReceiver(receiver);
150 
151         return receiver;
152     }
153 
154     /***
155      * Create a sender to send messages to the specified queue.
156      *
157      * @param queue the queue to access, or <code>null</code> if this is an
158      * unidentified producer
159      * @return the new sender
160      * @throws JMSException if the sender can't be created
161      */
162     public synchronized QueueSender createSender(Queue queue)
163         throws JMSException {
164         ensureOpen();
165         JmsQueueSender sender = new JmsQueueSender(this, (JmsQueue) queue);
166         addSender(sender);
167         return sender;
168     }
169 
170     /***
171      * Create a new queue browser
172      *
173      * @param queue the queue to access
174      * @return the new queue browser
175      * @throws JMSException if the browser can't be created
176      */
177     public QueueBrowser createBrowser(Queue queue) throws JMSException {
178         return createBrowser(queue, null);
179     }
180 
181     /***
182      * Create a new queue browser
183      *
184      * @param queue the queue to access
185      * @param selector the message selector to filter messages.
186      * May be <code>null</code>
187      * @return the new queue browser
188      * @throws JMSException if the browser can't be created
189      */
190     public synchronized QueueBrowser createBrowser(Queue queue,
191                                                    String selector)
192         throws JMSException {
193 
194         ensureOpen();
195 
196         if (queue == null) {
197             throw new InvalidDestinationException(
198                 "Cannot create browser: argument 'queue' is null");
199         }
200 
201         JmsQueueBrowser browser = null;
202 
203         // check to see if the queue is temporary. A temporary queue
204         // can only be used within the context of the owning connection
205         if (!checkForValidTemporaryDestination((JmsDestination) queue)) {
206             throw new InvalidDestinationException(
207                 "Cannot create a queue browser for a temp queue "
208                 + "that is not bound to this connection");
209         }
210 
211         browser = new JmsQueueBrowser(
212             this, getNextConsumerId(), (JmsQueue) queue, selector);
213         addBrowser(browser);
214 
215         return browser;
216     }
217 
218     /***
219      * Create a temporary queue. It's lifetime is that of the QueueConnection,
220      * unless deleted earlier.
221      *
222      * @return a new temporary queue
223      * @throws JMSException if the queue cannot be created
224      */
225     public synchronized TemporaryQueue createTemporaryQueue()
226         throws JMSException {
227         ensureOpen();
228 
229         JmsTemporaryQueue queue = new JmsTemporaryQueue();
230         queue.setOwningConnection(getConnection());
231 
232         return queue;
233     }
234 
235     /***
236      * Register a receiver
237      *
238      * @param receiver the receiver to register
239      * @throws JMSException if the receiver cannot be registered with the
240      * server
241      */
242     protected void addReceiver(JmsQueueReceiver receiver) throws JMSException {
243         // create it on the server
244         getJmsSessionStub().createReceiver(
245             (JmsQueue) receiver.getQueue(), receiver.getClientId(),
246              receiver.getMessageSelector());
247 
248         // register locally
249         addConsumer(receiver);
250     }
251 
252     /***
253      * Register a sender
254      *
255      * @param sender the sender to register
256      */
257     protected void addSender(JmsQueueSender sender) {
258         // getJmsSessionStub().createSender((JmsQueue)sender.getQueue());
259         // -- no longer used.
260 
261         // register locally
262         addProducer(sender);
263     }
264 
265     /***
266      * Register a browser
267      *
268      * @param browser the browser to register
269      * @throws JMSException if the browser cannot be registered with the
270      * server
271      */
272     protected void addBrowser(JmsQueueBrowser browser) throws JMSException {
273         // create it on the server
274         getJmsSessionStub().createBrowser((JmsQueue) browser.getQueue(),
275             browser.getClientId(), browser.getMessageSelector());
276 
277         // register locally
278         addConsumer(browser);
279     }
280 
281     /***
282      * Deregister a receiver
283      *
284      * @param receiver the receiver to deregister
285      * @throws JMSException if the receiver cannot be deregistered from the
286      * server
287      */
288     protected synchronized void removeReceiver(JmsQueueReceiver receiver)
289         throws JMSException {
290         // unregister the message listener. This must be called before
291         // deleting the receiver for correct clean up
292         if (!isClosed()) {
293             removeMessageListener(receiver);
294             getJmsSessionStub().deleteReceiver(receiver.getClientId());
295         }
296 
297         // local clean up
298         removeConsumer(receiver);
299     }
300 
301     /***
302      * Deregister a sender
303      *
304      * @param sender the sender to deregister
305      */
306     protected synchronized void removeSender(JmsQueueSender sender) {
307         // local clean up
308         removeProducer(sender);
309     }
310 
311     /***
312      * Deregister a queue browser
313      *
314      * @param browser the browser to deregister
315      * @throws JMSException if the browser cannot be deregistered from the
316      * server
317      */
318     protected synchronized void removeBrowser(JmsQueueBrowser browser)
319         throws JMSException {
320         // deregister from the server
321         if (!isClosed()) {
322             getJmsSessionStub().deleteBrowser(browser.getClientId());
323         }
324 
325         // local clean up
326         removeConsumer(browser);
327     }
328 }