KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright 2001, 2002,2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.axis.transport.jms;
18
19 import org.apache.axis.components.jms.JMSVendorAdapter;
20
21 import javax.jms.Connection JavaDoc;
22 import javax.jms.ConnectionFactory JavaDoc;
23 import javax.jms.Destination JavaDoc;
24 import javax.jms.JMSException JavaDoc;
25 import javax.jms.Message JavaDoc;
26 import javax.jms.MessageConsumer JavaDoc;
27 import javax.jms.Queue JavaDoc;
28 import javax.jms.QueueConnection JavaDoc;
29 import javax.jms.QueueConnectionFactory JavaDoc;
30 import javax.jms.QueueReceiver JavaDoc;
31 import javax.jms.QueueSender JavaDoc;
32 import javax.jms.QueueSession JavaDoc;
33 import javax.jms.Session JavaDoc;
34 import javax.jms.TemporaryQueue JavaDoc;
35
36 /**
37  * QueueConnector is a concrete JMSConnector subclass that specifically handles
38  * connections to queues (ptp domain).
39  *
40  * @author Jaime Meritt (jmeritt@sonicsoftware.com)
41  * @author Richard Chung (rchung@sonicsoftware.com)
42  * @author Dave Chappell (chappell@sonicsoftware.com)
43  */

44 public class QueueConnector extends JMSConnector
45 {
46
47     public QueueConnector(ConnectionFactory JavaDoc factory,
48                           int numRetries,
49                           int numSessions,
50                           long connectRetryInterval,
51                           long interactRetryInterval,
52                           long timeoutTime,
53                           boolean allowReceive,
54                           String JavaDoc clientID,
55                           String JavaDoc username,
56                           String JavaDoc password,
57                           JMSVendorAdapter adapter,
58                           JMSURLHelper jmsurl)
59         throws JMSException JavaDoc
60     {
61         super(factory, numRetries, numSessions, connectRetryInterval,
62               interactRetryInterval, timeoutTime, allowReceive, clientID,
63               username, password, adapter, jmsurl);
64     }
65
66     public JMSEndpoint createEndpoint(String JavaDoc destination)
67     {
68         return new QueueEndpoint(destination);
69     }
70
71     /**
72      * Create an endpoint for a queue destination.
73      *
74      * @param destination
75      * @return
76      * @throws JMSException
77      */

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