1 22 package org.jboss.resource.adapter.jms; 23 24 import javax.jms.BytesMessage ; 25 import javax.jms.JMSException ; 26 import javax.jms.MapMessage ; 27 import javax.jms.Message ; 28 import javax.jms.MessageConsumer ; 29 import javax.jms.MessageListener ; 30 import javax.jms.ObjectMessage ; 31 import javax.jms.StreamMessage ; 32 import javax.jms.TextMessage ; 33 34 import org.jboss.logging.Logger; 35 36 42 public class JmsMessageConsumer implements MessageConsumer 43 { 44 private static final Logger log = Logger.getLogger(JmsMessageConsumer.class); 45 46 47 MessageConsumer consumer; 48 49 50 JmsSession session; 51 52 53 private boolean trace = log.isTraceEnabled(); 54 55 61 public JmsMessageConsumer(MessageConsumer consumer, JmsSession session) 62 { 63 this.consumer = consumer; 64 this.session = session; 65 66 if (trace) 67 log.trace("new JmsMessageConsumer " + this + " consumer=" + consumer + " session=" + session); 68 } 69 70 public void close() throws JMSException 71 { 72 if (trace) 73 log.trace("close " + this); 74 try 75 { 76 closeConsumer(); 77 } 78 finally 79 { 80 session.removeConsumer(this); 81 } 82 } 83 84 public MessageListener getMessageListener() throws JMSException 85 { 86 session.checkStrict(); 87 return consumer.getMessageListener(); 88 } 89 90 public String getMessageSelector() throws JMSException 91 { 92 return consumer.getMessageSelector(); 93 } 94 95 public Message receive() throws JMSException 96 { 97 if (trace) 98 log.trace("receive " + this); 99 Message message = consumer.receive(); 100 if (trace) 101 log.trace("received " + this + " result=" + message); 102 if (message == null) 103 return null; 104 else 105 return wrapMessage(message); 106 } 107 108 public Message receive(long timeout) throws JMSException 109 { 110 if (trace) 111 log.trace("receive " + this + " timeout=" + timeout); 112 Message message = consumer.receive(timeout); 113 if (trace) 114 log.trace("received " + this + " result=" + message); 115 if (message == null) 116 return null; 117 else 118 return wrapMessage(message); 119 } 120 121 public Message receiveNoWait() throws JMSException 122 { 123 if (trace) 124 log.trace("receiveNoWait " + this); 125 Message message = consumer.receiveNoWait(); 126 if (trace) 127 log.trace("received " + this + " result=" + message); 128 if (message == null) 129 return null; 130 else 131 return wrapMessage(message); 132 } 133 134 public void setMessageListener(MessageListener listener) throws JMSException 135 { 136 session.checkStrict(); 137 if (listener == null) 138 consumer.setMessageListener(null); 139 else 140 consumer.setMessageListener(wrapMessageListener(listener)); 141 } 142 143 void closeConsumer() throws JMSException 144 { 145 consumer.close(); 146 } 147 148 Message wrapMessage(Message message) 149 { 150 if (message instanceof BytesMessage ) 151 return new JmsBytesMessage((BytesMessage ) message, session); 152 else if (message instanceof MapMessage ) 153 return new JmsMapMessage((MapMessage ) message, session); 154 else if (message instanceof ObjectMessage ) 155 return new JmsObjectMessage((ObjectMessage ) message, session); 156 else if (message instanceof StreamMessage ) 157 return new JmsStreamMessage((StreamMessage ) message, session); 158 else if (message instanceof TextMessage ) 159 return new JmsTextMessage((TextMessage ) message, session); 160 return new JmsMessage(message, session); 161 } 162 163 MessageListener wrapMessageListener(MessageListener listener) 164 { 165 return new JmsMessageListener(listener, this); 166 } 167 } 168 | Popular Tags |