KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > network > jms > QueueBridge


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

18 package org.apache.activemq.network.jms;
19
20 import javax.jms.Connection JavaDoc;
21 import javax.jms.Destination JavaDoc;
22 import javax.jms.JMSException JavaDoc;
23 import javax.jms.Message JavaDoc;
24 import javax.jms.MessageConsumer JavaDoc;
25 import javax.jms.MessageProducer JavaDoc;
26 import javax.jms.Queue JavaDoc;
27 import javax.jms.QueueConnection JavaDoc;
28 import javax.jms.QueueSender JavaDoc;
29 import javax.jms.QueueSession JavaDoc;
30 import javax.jms.Session JavaDoc;
31 import javax.jms.Topic JavaDoc;
32 /**
33  * A Destination bridge is used to bridge between to different JMS systems
34  *
35  * @version $Revision: 1.1.1.1 $
36  */

37 class QueueBridge extends DestinationBridge{
38     protected Queue JavaDoc consumerQueue;
39     protected Queue JavaDoc producerQueue;
40     protected QueueSession JavaDoc consumerSession;
41     protected QueueSession JavaDoc producerSession;
42    
43     protected String JavaDoc selector;
44     protected QueueSender JavaDoc producer;
45     protected QueueConnection JavaDoc consumerConnection;
46     protected QueueConnection JavaDoc producerConnection;
47     
48
49     public void stop() throws Exception JavaDoc{
50         super.stop();
51         if(consumerSession!=null){
52             consumerSession.close();
53         }
54         if(producerSession!=null){
55             producerSession.close();
56         }
57     }
58     
59
60     protected MessageConsumer JavaDoc createConsumer() throws JMSException JavaDoc{
61         // set up the consumer
62
consumerSession=consumerConnection.createQueueSession(false,Session.CLIENT_ACKNOWLEDGE);
63         MessageConsumer JavaDoc consumer=null;
64         
65             if(selector!=null&&selector.length()>0){
66                 consumer=consumerSession.createReceiver(consumerQueue,selector);
67             }else{
68                 consumer=consumerSession.createReceiver(consumerQueue);
69             }
70        
71         return consumer;
72     }
73     
74     protected synchronized MessageProducer JavaDoc createProducer() throws JMSException JavaDoc{
75         producerSession=producerConnection.createQueueSession(false,Session.AUTO_ACKNOWLEDGE);
76         producer = producerSession.createSender(null);
77         return producer;
78     }
79     
80         
81         
82     
83     protected synchronized void sendMessage(Message JavaDoc message) throws JMSException JavaDoc{
84         if (producer == null) {
85             createProducer();
86         }
87         producer.send(producerQueue,message);
88     }
89
90     /**
91      * @return Returns the consumerConnection.
92      */

93     public QueueConnection JavaDoc getConsumerConnection(){
94         return consumerConnection;
95     }
96
97     /**
98      * @param consumerConnection The consumerConnection to set.
99      */

100     public void setConsumerConnection(QueueConnection JavaDoc consumerConnection){
101         this.consumerConnection=consumerConnection;
102     }
103
104     /**
105      * @return Returns the consumerQueue.
106      */

107     public Queue JavaDoc getConsumerQueue(){
108         return consumerQueue;
109     }
110
111     /**
112      * @param consumerQueue The consumerQueue to set.
113      */

114     public void setConsumerQueue(Queue JavaDoc consumerQueue){
115         this.consumerQueue=consumerQueue;
116     }
117
118     /**
119      * @return Returns the producerConnection.
120      */

121     public QueueConnection JavaDoc getProducerConnection(){
122         return producerConnection;
123     }
124
125     /**
126      * @param producerConnection The producerConnection to set.
127      */

128     public void setProducerConnection(QueueConnection JavaDoc producerConnection){
129         this.producerConnection=producerConnection;
130     }
131
132     /**
133      * @return Returns the producerQueue.
134      */

135     public Queue JavaDoc getProducerQueue(){
136         return producerQueue;
137     }
138
139     /**
140      * @param producerQueue The producerQueue to set.
141      */

142     public void setProducerQueue(Queue JavaDoc producerQueue){
143         this.producerQueue=producerQueue;
144     }
145
146     /**
147      * @return Returns the selector.
148      */

149     public String JavaDoc getSelector(){
150         return selector;
151     }
152
153     /**
154      * @param selector The selector to set.
155      */

156     public void setSelector(String JavaDoc selector){
157         this.selector=selector;
158     }
159     
160     protected Connection JavaDoc getConnnectionForConsumer(){
161         return getConsumerConnection();
162     }
163     
164     protected Connection JavaDoc getConnectionForProducer(){
165         return getProducerConnection();
166     }
167     
168   
169 }
170
Popular Tags