01: package com.mockrunner.mock.jms;
02:
03: import java.util.Iterator;
04: import java.util.List;
05:
06: import javax.jms.JMSException;
07: import javax.jms.Message;
08: import javax.jms.MessageListener;
09: import javax.jms.Queue;
10:
11: /**
12: * Mock implementation of JMS <code>Queue</code>.
13: */
14: public class MockQueue extends MockDestination implements Queue {
15: private String name;
16:
17: public MockQueue(String name) {
18: this .name = name;
19: }
20:
21: public String getQueueName() throws JMSException {
22: return name;
23: }
24:
25: /**
26: * Adds a message to this <code>Queue</code> that will
27: * be propagated to the corresponding receiver. Only one
28: * receiver will get the message. The order is not
29: * predictable.
30: * @param message the message
31: */
32: public void addMessage(Message message) throws JMSException {
33: addReceivedMessage(message);
34: boolean isConsumed = false;
35: Iterator sessionsIterator = sessionSet().iterator();
36: while (sessionsIterator.hasNext() && !isConsumed) {
37: MockSession session = (MockSession) sessionsIterator.next();
38: MessageListener globalListener = session
39: .getMessageListener();
40: if (null != globalListener) {
41: globalListener.onMessage(message);
42: isConsumed = true;
43: acknowledgeMessage(message, session);
44: } else {
45: List receivers = session.getQueueTransmissionManager()
46: .getQueueReceiverList(name);
47: for (int ii = 0; ii < receivers.size() && !isConsumed; ii++) {
48: MockQueueReceiver receiver = (MockQueueReceiver) receivers
49: .get(ii);
50: if (receiver.canConsume(message)) {
51: receiver.receiveMessage(message);
52: isConsumed = true;
53: acknowledgeMessage(message, session);
54: }
55: }
56: }
57: }
58: if (!isConsumed) {
59: addCurrentMessage(message);
60: }
61: }
62: }
|