1 package net.walend.jmsbridge; 2 3 import java.util.Enumeration ; 4 5 import javax.jms.MessageProducer ; 6 import javax.jms.MessageConsumer ; 7 import javax.jms.Session ; 8 import javax.jms.JMSException ; 9 import javax.jms.MessageListener ; 10 import javax.jms.ExceptionListener ; 11 import javax.jms.Message ; 12 import javax.jms.ObjectMessage ; 13 14 19 20 public abstract class AbstractBridge 21 { 22 protected AbstractBridge(MessageConsumer source,MessageProducer sink,Session sinkSession,ExceptionListener exceptionListener) 23 throws JMSException 24 { 25 source.setMessageListener(createBridgeListener(sink,sinkSession,exceptionListener)); 26 } 27 28 protected abstract MessageListener createBridgeListener(MessageProducer sink,Session sinkSession,ExceptionListener exceptionListener); 29 30 31 protected abstract class BridgeListener 32 implements MessageListener 33 { 34 private Session session; 35 36 private ExceptionListener exceptionListener; 37 38 protected BridgeListener(Session sinkSession,ExceptionListener exceptionListener) 39 { 40 this.session = sinkSession; 41 this.exceptionListener = exceptionListener; 42 } 43 44 47 protected Message translateMessage(Message message) 48 throws JMSException 49 { 50 Message result; 51 52 if(message instanceof ObjectMessage ) 53 { 54 result = session.createObjectMessage(((ObjectMessage )message).getObject()); 55 } 56 else 58 { 59 result = session.createMessage(); 60 } 61 63 Enumeration enumeration = message.getPropertyNames(); 65 while(enumeration.hasMoreElements()) 66 { 67 String propName = (String )enumeration.nextElement(); 68 result.setObjectProperty(propName,message.getObjectProperty(propName)); 69 } 70 71 return result; 72 } 73 74 77 protected abstract void send(Message message) 78 throws JMSException ; 79 80 83 public void onMessage(Message message) 84 { 85 try 86 { 87 Message translated = translateMessage(message); 89 send(translated); 91 } 92 catch(JMSException jmse) 93 { 94 exceptionListener.onException(jmse); 95 } 96 } 97 98 } 99 100 101 } 102 103 123 | Popular Tags |