001    package com.mockrunner.mock.jms;
002    
003    import javax.jms.JMSException;
004    import javax.jms.Message;
005    import javax.jms.Queue;
006    import javax.jms.QueueReceiver;
007    
008    /**
009     * Mock implementation of JMS <code>QueueReceiver</code>.
010     */
011    public class MockQueueReceiver extends MockMessageConsumer implements QueueReceiver
012    {
013        private MockSession session;
014        private MockQueue queue;
015        
016        public MockQueueReceiver(MockConnection connection, MockQueueSession session, MockQueue queue)
017        {
018            this(connection, session, queue, null); 
019        }
020    
021        public MockQueueReceiver(MockConnection connection, MockSession session, MockQueue queue, String messageSelector)
022        {
023            super(connection, messageSelector);
024            this.session = session;
025            this.queue = queue;
026        }
027    
028        public Queue getQueue() throws JMSException
029        {
030            getConnection().throwJMSException();
031            return queue;
032        }
033        
034        public Message receive() throws JMSException
035        {
036            getConnection().throwJMSException();
037            if(isClosed())
038            {
039                throw new JMSException("Receiver is closed");
040            }
041            if(queue.isEmpty()) return null;
042            Message message;
043            if((!getConnection().getConfigurationManager().getUseMessageSelectors()) || (null == getMessageFilter()))
044            {
045                message = queue.getMessage();
046            }
047            else
048            {
049                message = queue.getMatchingMessage(getMessageFilter());
050            }
051            if(null == message) return null;
052            if(session.isAutoAcknowledge()) message.acknowledge();
053            return message;
054        }
055    }