All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.mockrunner.mock.jms.MockQueueReceiver Maven / Gradle / Ivy

There is a newer version: 2.0.7
Show newest version
package com.mockrunner.mock.jms;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Queue;
import javax.jms.QueueReceiver;

/**
 * Mock implementation of JMS QueueReceiver.
 */
public class MockQueueReceiver extends MockMessageConsumer implements QueueReceiver
{
    private MockSession session;
    private MockQueue queue;
    
    public MockQueueReceiver(MockConnection connection, MockQueueSession session, MockQueue queue)
    {
        this(connection, session, queue, null); 
    }

    public MockQueueReceiver(MockConnection connection, MockSession session, MockQueue queue, String messageSelector)
    {
        super(connection, messageSelector);
        this.session = session;
        this.queue = queue;
    }

    public Queue getQueue() throws JMSException
    {
        getConnection().throwJMSException();
        return queue;
    }
 
    public Message receiveNoWait() throws JMSException
    {
        getConnection().throwJMSException();
        if(isClosed())
        {
            throw new JMSException("Receiver is closed");
        }
        if(queue.isEmpty()) return null;
        Message message;
        if((!getConnection().getConfigurationManager().getUseMessageSelectors()) || (null == getMessageFilter()))
        {
            message = queue.getMessage();
        }
        else
        {
            message = queue.getMatchingMessage(getMessageFilter());
        }
        if(null == message) return null;
        if(session.isAutoAcknowledge()) message.acknowledge();
        return message;
    }
    
    protected void waitOnMessage(long timeout)
    {
    	try {
    		//todo, notify on topic. 
    		synchronized(queue) {
    			queue.wait(timeout);
    		}
		} catch (InterruptedException e) {
		}
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy