001    /**
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.activemq.pool;
018    
019    import javax.jms.JMSException;
020    import javax.jms.Message;
021    import javax.jms.MessageConsumer;
022    import javax.jms.MessageListener;
023    
024    /**
025     * A {@link MessageConsumer} which was created by {@link PooledSession}.
026     */
027    public class PooledMessageConsumer implements MessageConsumer {
028    
029        private final PooledSession session;
030        private final MessageConsumer delegate;
031    
032        /**
033         * Wraps the message consumer.
034         *
035         * @param session  the pooled session
036         * @param delegate the created consumer to wrap
037         */
038        public PooledMessageConsumer(PooledSession session, MessageConsumer delegate) {
039            this.session = session;
040            this.delegate = delegate;
041        }
042    
043        @Override
044        public void close() throws JMSException {
045            // ensure session removes consumer as its closed now
046            session.onConsumerClose(delegate);
047            delegate.close();
048        }
049    
050        @Override
051        public MessageListener getMessageListener() throws JMSException {
052            return delegate.getMessageListener();
053        }
054    
055        @Override
056        public String getMessageSelector() throws JMSException {
057            return delegate.getMessageSelector();
058        }
059    
060        @Override
061        public Message receive() throws JMSException {
062            return delegate.receive();
063        }
064    
065        @Override
066        public Message receive(long timeout) throws JMSException {
067            return delegate.receive(timeout);
068        }
069    
070        @Override
071        public Message receiveNoWait() throws JMSException {
072            return delegate.receiveNoWait();
073        }
074    
075        @Override
076        public void setMessageListener(MessageListener listener) throws JMSException {
077            delegate.setMessageListener(listener);
078        }
079    
080        @Override
081        public String toString() {
082            return "PooledMessageConsumer { " + delegate + " }";
083        }
084    }