KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > axis > transport > jms > QueueConnector


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  *
5  * Copyright (c) 2001, 2002 The Apache Software Foundation. All rights
6  * reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  *
20  * 3. The end-user documentation included with the redistribution,
21  * if any, must include the following acknowledgment:
22  * "This product includes software developed by the
23  * Apache Software Foundation (http://www.apache.org/)."
24  * Alternately, this acknowledgment may appear in the software itself,
25  * if and wherever such third-party acknowledgments normally appear.
26  *
27  * 4. The names "Axis" and "Apache Software Foundation" must
28  * not be used to endorse or promote products derived from this
29  * software without prior written permission. For written
30  * permission, please contact apache@apache.org.
31  *
32  * 5. Products derived from this software may not be called "Apache",
33  * nor may "Apache" appear in their name, without prior written
34  * permission of the Apache Software Foundation.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
40  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47  * SUCH DAMAGE.
48  * ====================================================================
49  *
50  * This software consists of voluntary contributions made by many
51  * individuals on behalf of the Apache Software Foundation. For more
52  * information on the Apache Software Foundation, please see
53  * <http://www.apache.org/>.
54  */

55
56 package org.jboss.axis.transport.jms;
57
58 import org.jboss.axis.components.jms.JMSVendorAdapter;
59
60 import javax.jms.Connection JavaDoc;
61 import javax.jms.ConnectionFactory JavaDoc;
62 import javax.jms.Destination JavaDoc;
63 import javax.jms.JMSException JavaDoc;
64 import javax.jms.Message JavaDoc;
65 import javax.jms.MessageConsumer JavaDoc;
66 import javax.jms.Queue JavaDoc;
67 import javax.jms.QueueConnection JavaDoc;
68 import javax.jms.QueueConnectionFactory JavaDoc;
69 import javax.jms.QueueReceiver JavaDoc;
70 import javax.jms.QueueSender JavaDoc;
71 import javax.jms.QueueSession JavaDoc;
72 import javax.jms.Session JavaDoc;
73 import javax.jms.TemporaryQueue JavaDoc;
74
75 /**
76  * QueueConnector is a concrete JMSConnector subclass that specifically handles
77  * connections to queues (ptp domain).
78  *
79  * @author Jaime Meritt (jmeritt@sonicsoftware.com)
80  * @author Richard Chung (rchung@sonicsoftware.com)
81  * @author Dave Chappell (chappell@sonicsoftware.com)
82  */

83 public class QueueConnector extends JMSConnector
84 {
85
86    public QueueConnector(ConnectionFactory JavaDoc factory,
87                          int numRetries,
88                          int numSessions,
89                          long connectRetryInterval,
90                          long interactRetryInterval,
91                          long timeoutTime,
92                          boolean allowReceive,
93                          String JavaDoc clientID,
94                          String JavaDoc username,
95                          String JavaDoc password,
96                          JMSVendorAdapter adapter)
97            throws JMSException JavaDoc
98    {
99       super(factory, numRetries, numSessions, connectRetryInterval,
100               interactRetryInterval, timeoutTime, allowReceive, clientID,
101               username, password, adapter);
102    }
103
104    public JMSEndpoint createEndpoint(String JavaDoc destination)
105    {
106       return new QueueEndpoint(destination);
107    }
108
109    /**
110     * Create an endpoint for a queue destination.
111     *
112     * @param destination
113     * @return
114     * @throws JMSException
115     */

116    public JMSEndpoint createEndpoint(Destination JavaDoc destination)
117            throws JMSException JavaDoc
118    {
119       if (!(destination instanceof Queue JavaDoc))
120          throw new IllegalArgumentException JavaDoc("The input must be a queue for this connector");
121       return new QueueDestinationEndpoint((Queue JavaDoc)destination);
122    }
123
124    protected Connection JavaDoc internalConnect(ConnectionFactory JavaDoc connectionFactory,
125                                         String JavaDoc username,
126                                         String JavaDoc password)
127            throws JMSException JavaDoc
128    {
129       QueueConnectionFactory JavaDoc qcf = (QueueConnectionFactory JavaDoc)connectionFactory;
130       if (username == null)
131          return qcf.createQueueConnection();
132
133       return qcf.createQueueConnection(username, password);
134    }
135
136
137    protected SyncConnection createSyncConnection(ConnectionFactory JavaDoc factory,
138                                                  Connection JavaDoc connection,
139                                                  int numSessions,
140                                                  String JavaDoc threadName,
141                                                  String JavaDoc clientID,
142                                                  String JavaDoc username,
143                                                  String JavaDoc password)
144
145            throws JMSException JavaDoc
146    {
147       return new QueueSyncConnection((QueueConnectionFactory JavaDoc)factory,
148               (QueueConnection JavaDoc)connection, numSessions,
149               threadName, clientID, username, password);
150    }
151
152    private QueueSession JavaDoc createQueueSession(QueueConnection JavaDoc connection, int ackMode)
153            throws JMSException JavaDoc
154    {
155       return connection.createQueueSession(false, ackMode);
156    }
157
158    private Queue JavaDoc createQueue(QueueSession JavaDoc session, String JavaDoc subject)
159            throws Exception JavaDoc
160    {
161       return m_adapter.getQueue(session, subject);
162    }
163
164    private QueueReceiver JavaDoc createReceiver(QueueSession JavaDoc session,
165                                         Queue JavaDoc queue,
166                                         String JavaDoc messageSelector)
167            throws JMSException JavaDoc
168    {
169       return session.createReceiver(queue, messageSelector);
170    }
171
172    private final class QueueSyncConnection extends SyncConnection
173    {
174       QueueSyncConnection(QueueConnectionFactory JavaDoc connectionFactory,
175                           QueueConnection JavaDoc connection,
176                           int numSessions,
177                           String JavaDoc threadName,
178                           String JavaDoc clientID,
179                           String JavaDoc username,
180                           String JavaDoc password)
181               throws JMSException JavaDoc
182       {
183          super(connectionFactory, connection, numSessions, threadName,
184                  clientID, username, password);
185       }
186
187       protected SendSession createSendSession(javax.jms.Connection JavaDoc connection)
188               throws JMSException JavaDoc
189       {
190          QueueSession JavaDoc session = createQueueSession((QueueConnection JavaDoc)connection,
191                  JMSConstants.DEFAULT_ACKNOWLEDGE_MODE);
192          QueueSender JavaDoc sender = session.createSender(null);
193          return new QueueSendSession(session, sender);
194       }
195
196       private final class QueueSendSession extends SendSession
197       {
198          QueueSendSession(QueueSession JavaDoc session,
199                           QueueSender JavaDoc sender)
200                  throws JMSException JavaDoc
201          {
202             super(session, sender);
203          }
204
205          protected MessageConsumer JavaDoc createConsumer(Destination JavaDoc destination)
206                  throws JMSException JavaDoc
207          {
208             return createReceiver((QueueSession JavaDoc)m_session, (Queue JavaDoc)destination, null);
209          }
210
211
212          protected Destination JavaDoc createTemporaryDestination()
213                  throws JMSException JavaDoc
214          {
215             return ((QueueSession JavaDoc)m_session).createTemporaryQueue();
216          }
217
218          protected void deleteTemporaryDestination(Destination JavaDoc destination)
219                  throws JMSException JavaDoc
220          {
221             ((TemporaryQueue JavaDoc)destination).delete();
222          }
223
224          protected void send(Destination JavaDoc destination, Message JavaDoc message,
225                              int deliveryMode, int priority, long timeToLive)
226                  throws JMSException JavaDoc
227          {
228             ((QueueSender JavaDoc)m_producer).send((Queue JavaDoc)destination, message,
229                     deliveryMode, priority, timeToLive);
230          }
231
232       }
233    }
234
235    private class QueueEndpoint
236            extends JMSEndpoint
237    {
238       String JavaDoc m_queueName;
239
240       QueueEndpoint(String JavaDoc queueName)
241       {
242          super(QueueConnector.this);
243          m_queueName = queueName;
244       }
245
246       Destination JavaDoc getDestination(Session JavaDoc session)
247               throws Exception JavaDoc
248       {
249          return createQueue((QueueSession JavaDoc)session, m_queueName);
250       }
251
252       public String JavaDoc toString()
253       {
254          StringBuffer JavaDoc buffer = new StringBuffer JavaDoc("QueueEndpoint:");
255          buffer.append(m_queueName);
256          return buffer.toString();
257       }
258
259       public boolean equals(Object JavaDoc object)
260       {
261          if (!super.equals(object))
262             return false;
263
264          if (!(object instanceof QueueEndpoint))
265             return false;
266
267          return m_queueName.equals(((QueueEndpoint)object).m_queueName);
268       }
269    }
270
271
272    private final class QueueDestinationEndpoint
273            extends QueueEndpoint
274    {
275       Queue JavaDoc m_queue;
276
277       QueueDestinationEndpoint(Queue JavaDoc queue)
278               throws JMSException JavaDoc
279       {
280          super(queue.getQueueName());
281          m_queue = queue;
282       }
283
284       Destination JavaDoc getDestination(Session JavaDoc session)
285       {
286          return m_queue;
287       }
288
289    }
290
291    protected AsyncConnection createAsyncConnection(ConnectionFactory JavaDoc factory,
292                                                    Connection JavaDoc connection,
293                                                    String JavaDoc threadName,
294                                                    String JavaDoc clientID,
295                                                    String JavaDoc username,
296                                                    String JavaDoc password)
297            throws JMSException JavaDoc
298    {
299       return new QueueAsyncConnection((QueueConnectionFactory JavaDoc)factory,
300               (QueueConnection JavaDoc)connection, threadName,
301               clientID, username, password);
302    }
303
304    private final class QueueAsyncConnection extends AsyncConnection
305    {
306
307       QueueAsyncConnection(QueueConnectionFactory JavaDoc connectionFactory,
308                            QueueConnection JavaDoc connection,
309                            String JavaDoc threadName,
310                            String JavaDoc clientID,
311                            String JavaDoc username,
312                            String JavaDoc password)
313               throws JMSException JavaDoc
314       {
315          super(connectionFactory, connection, threadName, clientID, username, password);
316       }
317
318       protected ListenerSession createListenerSession(javax.jms.Connection JavaDoc connection,
319                                                       Subscription subscription)
320               throws Exception JavaDoc
321       {
322          QueueSession JavaDoc session = createQueueSession((QueueConnection JavaDoc)connection,
323                  subscription.m_ackMode);
324          QueueReceiver JavaDoc receiver = createReceiver(session,
325                  (Queue JavaDoc)subscription.m_endpoint.getDestination(session),
326                  subscription.m_messageSelector);
327          return new ListenerSession(session, receiver, subscription);
328       }
329
330    }
331
332 }
Popular Tags