KickJava   Java API By Example, From Geeks To Geeks.

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


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.Session JavaDoc;
27 import javax.jms.Topic JavaDoc;
28 import javax.jms.TopicConnection JavaDoc;
29 import javax.jms.TopicPublisher JavaDoc;
30 import javax.jms.TopicSession JavaDoc;
31 /**
32  * A Destination bridge is used to bridge between to different JMS systems
33  *
34  * @version $Revision: 1.1.1.1 $
35  */

36 class TopicBridge extends DestinationBridge{
37     protected Topic JavaDoc consumerTopic;
38     protected Topic JavaDoc producerTopic;
39     protected TopicSession JavaDoc consumerSession;
40     protected TopicSession JavaDoc producerSession;
41     protected String JavaDoc consumerName;
42     protected String JavaDoc selector;
43     protected TopicPublisher JavaDoc producer;
44     protected TopicConnection JavaDoc consumerConnection;
45     protected TopicConnection JavaDoc producerConnection;
46     
47
48     public void stop() throws Exception JavaDoc{
49         super.stop();
50         if(consumerSession!=null){
51             consumerSession.close();
52         }
53         if(producerSession!=null){
54             producerSession.close();
55         }
56     }
57     
58    
59
60     protected MessageConsumer JavaDoc createConsumer() throws JMSException JavaDoc{
61         // set up the consumer
62
consumerSession=consumerConnection.createTopicSession(false,Session.CLIENT_ACKNOWLEDGE);
63         MessageConsumer JavaDoc consumer=null;
64         if(consumerName!=null&&consumerName.length()>0){
65             if(selector!=null&&selector.length()>0){
66                 consumer=consumerSession.createDurableSubscriber(consumerTopic,consumerName,selector,false);
67             }else{
68                 consumer=consumerSession.createDurableSubscriber(consumerTopic,consumerName);
69             }
70         }else{
71             if(selector!=null&&selector.length()>0){
72                 consumer=consumerSession.createSubscriber(consumerTopic,selector,false);
73             }else{
74                 consumer=consumerSession.createSubscriber(consumerTopic);
75             }
76         }
77         return consumer;
78     }
79     
80     
81     
82     protected synchronized MessageProducer JavaDoc createProducer() throws JMSException JavaDoc{
83         producerSession=producerConnection.createTopicSession(false,Session.AUTO_ACKNOWLEDGE);
84         producer = producerSession.createPublisher(null);
85         return producer;
86     }
87     
88     protected synchronized void sendMessage(Message JavaDoc message) throws JMSException JavaDoc{
89         if (producer == null) {
90             createProducer();
91         }
92         producer.publish(producerTopic,message);
93     }
94
95     /**
96      * @return Returns the consumerConnection.
97      */

98     public TopicConnection JavaDoc getConsumerConnection(){
99         return consumerConnection;
100     }
101
102     /**
103      * @param consumerConnection
104      * The consumerConnection to set.
105      */

106     public void setConsumerConnection(TopicConnection JavaDoc consumerConnection){
107         this.consumerConnection=consumerConnection;
108     }
109
110     /**
111      * @return Returns the subscriptionName.
112      */

113     public String JavaDoc getConsumerName(){
114         return consumerName;
115     }
116
117     /**
118      * @param subscriptionName
119      * The subscriptionName to set.
120      */

121     public void setConsumerName(String JavaDoc consumerName){
122         this.consumerName=consumerName;
123     }
124
125     /**
126      * @return Returns the consumerTopic.
127      */

128     public Topic JavaDoc getConsumerTopic(){
129         return consumerTopic;
130     }
131
132     /**
133      * @param consumerTopic
134      * The consumerTopic to set.
135      */

136     public void setConsumerTopic(Topic JavaDoc consumerTopic){
137         this.consumerTopic=consumerTopic;
138     }
139
140     /**
141      * @return Returns the producerConnection.
142      */

143     public TopicConnection JavaDoc getProducerConnection(){
144         return producerConnection;
145     }
146
147     /**
148      * @param producerConnection
149      * The producerConnection to set.
150      */

151     public void setProducerConnection(TopicConnection JavaDoc producerConnection){
152         this.producerConnection=producerConnection;
153     }
154
155     /**
156      * @return Returns the producerTopic.
157      */

158     public Topic JavaDoc getProducerTopic(){
159         return producerTopic;
160     }
161
162     /**
163      * @param producerTopic
164      * The producerTopic to set.
165      */

166     public void setProducerTopic(Topic JavaDoc producerTopic){
167         this.producerTopic=producerTopic;
168     }
169
170     /**
171      * @return Returns the selector.
172      */

173     public String JavaDoc getSelector(){
174         return selector;
175     }
176
177     /**
178      * @param selector
179      * The selector to set.
180      */

181     public void setSelector(String JavaDoc selector){
182         this.selector=selector;
183     }
184     
185     protected Connection JavaDoc getConnnectionForConsumer(){
186         return getConsumerConnection();
187     }
188     
189     protected Connection JavaDoc getConnectionForProducer(){
190         return getProducerConnection();
191     }
192 }
193
Popular Tags