1 /***
2 *
3 * Copyright 2004 Protique Ltd
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 **/
18 package org.codehaus.activecluster;
19
20 import javax.jms.Message;
21 import javax.jms.MessageListener;
22 import java.util.ArrayList;
23 import java.util.List;
24
25 /***
26 * A mock message listener for testing
27 *
28 * @version $Revision: 1.1 $
29 */
30 public class StubMessageListener implements MessageListener {
31 private List messages = new ArrayList();
32 private Object semaphore;
33
34 public StubMessageListener() {
35 this(new Object());
36 }
37
38 public StubMessageListener(Object semaphore) {
39 this.semaphore = semaphore;
40 }
41
42 /***
43 * @return all the messages on the list so far, clearing the buffer
44 */
45 public synchronized List flushMessages() {
46 List answer = new ArrayList(messages);
47 messages.clear();
48 return answer;
49 }
50
51 public synchronized void onMessage(Message message) {
52 messages.add(message);
53 synchronized (semaphore) {
54 semaphore.notifyAll();
55 }
56 }
57
58 public void waitForMessageToArrive() {
59 System.out.println("Waiting for message to arrive");
60
61 long start = System.currentTimeMillis();
62
63 try {
64 if (messages.isEmpty()) {
65 synchronized (semaphore) {
66 semaphore.wait(4000);
67 }
68 }
69 }
70 catch (InterruptedException e) {
71 System.out.println("Caught: " + e);
72 }
73 long end = System.currentTimeMillis() - start;
74
75 System.out.println("End of wait for " + end + " millis");
76 }
77 }