KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > walend > jmsbridge > AbstractBridge


1 package net.walend.jmsbridge;
2
3 import java.util.Enumeration JavaDoc;
4
5 import javax.jms.MessageProducer JavaDoc;
6 import javax.jms.MessageConsumer JavaDoc;
7 import javax.jms.Session JavaDoc;
8 import javax.jms.JMSException JavaDoc;
9 import javax.jms.MessageListener JavaDoc;
10 import javax.jms.ExceptionListener JavaDoc;
11 import javax.jms.Message JavaDoc;
12 import javax.jms.ObjectMessage JavaDoc;
13
14 /**
15 AbstractBridge is a simple bridge from the MessageConsumer of one JMS implementation to the MessageProducer of another.
16
17 @author <a HREF="http://walend.net">David Walend</a> <a HREF="mailto:david@walend.net">david@walend.net</a>
18  */

19
20 public abstract class AbstractBridge
21 {
22     protected AbstractBridge(MessageConsumer JavaDoc source,MessageProducer JavaDoc sink,Session JavaDoc sinkSession,ExceptionListener JavaDoc exceptionListener)
23         throws JMSException JavaDoc
24     {
25         source.setMessageListener(createBridgeListener(sink,sinkSession,exceptionListener));
26     }
27
28     protected abstract MessageListener JavaDoc createBridgeListener(MessageProducer JavaDoc sink,Session JavaDoc sinkSession,ExceptionListener JavaDoc exceptionListener);
29
30
31     protected abstract class BridgeListener
32         implements MessageListener JavaDoc
33     {
34         private Session JavaDoc session;
35
36         private ExceptionListener JavaDoc exceptionListener;
37
38         protected BridgeListener(Session JavaDoc sinkSession,ExceptionListener JavaDoc exceptionListener)
39         {
40             this.session = sinkSession;
41             this.exceptionListener = exceptionListener;
42         }
43
44         /**
45 Translates message from the source JMS to the sink JMS.
46          */

47         protected Message JavaDoc translateMessage(Message JavaDoc message)
48             throws JMSException JavaDoc
49         {
50             Message JavaDoc result;
51
52             if(message instanceof ObjectMessage JavaDoc)
53                 {
54                     result = session.createObjectMessage(((ObjectMessage JavaDoc)message).getObject());
55                 }
56             //todo other message types
57
else
58                 {
59                     result = session.createMessage();
60                 }
61             //todo copy some header info
62

63             //Copy propreties
64
Enumeration JavaDoc enumeration = message.getPropertyNames();
65             while(enumeration.hasMoreElements())
66                 {
67                     String JavaDoc propName = (String JavaDoc)enumeration.nextElement();
68                     result.setObjectProperty(propName,message.getObjectProperty(propName));
69                 }
70
71             return result;
72         }
73
74         /**
75 Send the message to the sink. This method should send if the sink is a QueueSender, or publish if this is a TopicPublisher.
76          */

77         protected abstract void send(Message JavaDoc message)
78             throws JMSException JavaDoc;
79
80         /**
81 Translate the message from the source JMS to the sink JMS, and send it to the sink.
82          */

83         public void onMessage(Message JavaDoc message)
84         {
85             try
86                 {
87                     //translate the message to the sink JMS using the sinkSession
88
Message JavaDoc translated = translateMessage(message);
89                     //send the new message
90
send(translated);
91                 }
92             catch(JMSException JavaDoc jmse)
93                 {
94                     exceptionListener.onException(jmse);
95                 }
96         }
97
98     }
99
100
101 }
102
103 /* Copyright (c) 2001, 2002, 2003, 2004, 2005, 2006 David Walend
104 All rights reserved.
105
106 Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
107
108 Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
109
110 Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
111
112 Neither the name of the SomnifugiJMS Project, walend.net, nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission from David Walend.
113
114 Credits in redistributions in source or binary forms must include a link to http://somnifugi.sourceforge.net .
115
116 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
117 The net.walend.somnifugi.sql92 package is modified code from the openmq project, https://mq.dev.java.net/ , Copyright (c) of Sun, and carries the CDDL license, repeated here: You can obtain a copy of the license at https://glassfish.dev.java.net/public/CDDLv1.0.html. See the License for the specific language governing permissions and limitations under the License.
118
119 =================================================================================
120
121 For more information and the latest version of this software, please see http://somnifugi.sourceforge.net and http://walend.net or email <a HREF="mailto:david@walend.net">david@walend.net</a>.
122  */

123
Popular Tags