KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > resource > adapter > jms > JmsMessageConsumer


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.resource.adapter.jms;
23
24 import javax.jms.BytesMessage JavaDoc;
25 import javax.jms.JMSException JavaDoc;
26 import javax.jms.MapMessage JavaDoc;
27 import javax.jms.Message JavaDoc;
28 import javax.jms.MessageConsumer JavaDoc;
29 import javax.jms.MessageListener JavaDoc;
30 import javax.jms.ObjectMessage JavaDoc;
31 import javax.jms.StreamMessage JavaDoc;
32 import javax.jms.TextMessage JavaDoc;
33
34 import org.jboss.logging.Logger;
35
36 /**
37  * A wrapper for a message consumer
38  *
39  * @author <a HREF="mailto:adrian@jboss.com">Adrian Brock</a>
40  * @version $Revision: 38315 $
41  */

42 public class JmsMessageConsumer implements MessageConsumer JavaDoc
43 {
44    private static final Logger log = Logger.getLogger(JmsMessageConsumer.class);
45
46    /** The wrapped message consumer */
47    MessageConsumer JavaDoc consumer;
48    
49    /** The session for this consumer */
50    JmsSession session;
51    
52    /** Whether trace is enabled */
53    private boolean trace = log.isTraceEnabled();
54
55    /**
56     * Create a new wrapper
57     *
58     * @param consumer the consumer
59     * @param session the session
60     */

61    public JmsMessageConsumer(MessageConsumer JavaDoc 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 JavaDoc
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 JavaDoc getMessageListener() throws JMSException JavaDoc
85    {
86       session.checkStrict();
87       return consumer.getMessageListener();
88    }
89    
90    public String JavaDoc getMessageSelector() throws JMSException JavaDoc
91    {
92       return consumer.getMessageSelector();
93    }
94    
95    public Message JavaDoc receive() throws JMSException JavaDoc
96    {
97       if (trace)
98          log.trace("receive " + this);
99       Message JavaDoc 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 JavaDoc receive(long timeout) throws JMSException JavaDoc
109    {
110       if (trace)
111          log.trace("receive " + this + " timeout=" + timeout);
112       Message JavaDoc 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 JavaDoc receiveNoWait() throws JMSException JavaDoc
122    {
123       if (trace)
124          log.trace("receiveNoWait " + this);
125       Message JavaDoc 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 JavaDoc listener) throws JMSException JavaDoc
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 JavaDoc
144    {
145       consumer.close();
146    }
147    
148    Message JavaDoc wrapMessage(Message JavaDoc message)
149    {
150       if (message instanceof BytesMessage JavaDoc)
151          return new JmsBytesMessage((BytesMessage JavaDoc) message, session);
152       else if (message instanceof MapMessage JavaDoc)
153          return new JmsMapMessage((MapMessage JavaDoc) message, session);
154       else if (message instanceof ObjectMessage JavaDoc)
155          return new JmsObjectMessage((ObjectMessage JavaDoc) message, session);
156       else if (message instanceof StreamMessage JavaDoc)
157          return new JmsStreamMessage((StreamMessage JavaDoc) message, session);
158       else if (message instanceof TextMessage JavaDoc)
159          return new JmsTextMessage((TextMessage JavaDoc) message, session);
160       return new JmsMessage(message, session);
161    }
162    
163    MessageListener JavaDoc wrapMessageListener(MessageListener JavaDoc listener)
164    {
165       return new JmsMessageListener(listener, this);
166    }
167 }
168
Popular Tags