com.mockrunner.jms.GenericTransmissionManager Maven / Gradle / Ivy
package com.mockrunner.jms;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import com.mockrunner.mock.jms.MockConnection;
import com.mockrunner.mock.jms.MockMessageProducer;
import com.mockrunner.mock.jms.MockQueueSender;
import com.mockrunner.mock.jms.MockSession;
import com.mockrunner.mock.jms.MockTopicPublisher;
/**
* This class is used to create generic producers that are not
* associated with a destination.
* If you create a MessageProducer
with a null
* destination, this class is used to create a MessageProducer
* which is not associated with any destination.
* If the session used to create the producer is a QueueSession
,
* you'll get a QueueSender
. For a TopicSession
,
* you'll get a TopicPublisher
. For a generic session,
* you'll get a generic instance of MessageProducer
.
*/
public class GenericTransmissionManager
{
private MockConnection connection;
private MockSession session;
private List messageProducerList;
public GenericTransmissionManager(MockConnection connection, MockSession session)
{
this.connection = connection;
this.session = session;
messageProducerList = new ArrayList();
}
/**
* Closes all producers.
*/
public void closeAll()
{
closeAllMessageProducers();
}
/**
* Closes all producers.
*/
public void closeAllMessageProducers()
{
for(int ii = 0; ii < messageProducerList.size(); ii++)
{
MessageProducer producer = (MessageProducer)messageProducerList.get(ii);
try
{
producer.close();
}
catch(JMSException exc)
{
}
}
}
/**
* Creates a new MessageProducer
.
* @return the created MessageProducer
*/
public MockMessageProducer createMessageProducer()
{
MockMessageProducer producer = new MockMessageProducer(connection, session, null);
messageProducerList.add(producer);
return producer;
}
/**
* Creates a new QueueSender
.
* @return the created QueueSender
*/
public MockQueueSender createQueueSender()
{
MockQueueSender producer = new MockQueueSender(connection, session, null);
messageProducerList.add(producer);
return producer;
}
/**
* Creates a new TopicPublisher
.
* @return the created TopicPublisher
*/
public MockTopicPublisher createTopicPublisher()
{
MockTopicPublisher producer = new MockTopicPublisher(connection, session, null);
messageProducerList.add(producer);
return producer;
}
/**
* Returns a MessageProducer
by its index or
* null
, if no such MessageProducer
is
* present.
* @param index the index of the MessageProducer
* @return the MessageProducer
*/
public MockMessageProducer getMessageProducer(int index)
{
if(messageProducerList.size() <= index || index < 0) return null;
return (MockMessageProducer)messageProducerList.get(index);
}
/**
* Returns the list of all MessageProducer
objects.
* @return the list of MessageProducer
objects
*/
public List getMessageProducerList()
{
return Collections.unmodifiableList(messageProducerList);
}
}