KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > systest > impl > AgentMessageListener


1 /**
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one or more
4  * contributor license agreements. See the NOTICE file distributed with
5  * this work for additional information regarding copyright ownership.
6  * The ASF licenses this file to You under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with
8  * the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 package org.apache.activemq.systest.impl;
19
20 import java.util.concurrent.CopyOnWriteArrayList JavaDoc;
21
22 import javax.jms.Message JavaDoc;
23 import javax.jms.MessageListener JavaDoc;
24
25 import java.util.ArrayList JavaDoc;
26 import java.util.List JavaDoc;
27
28 /**
29  * A simple consumer which is useful for testing which can be used to wait until
30  * the consumer has received a specific number of messages.
31  *
32  * @author Mike Perham
33  * @version $Revision$
34  */

35 public class AgentMessageListener implements MessageListener JavaDoc {
36     private List JavaDoc messages = new CopyOnWriteArrayList JavaDoc();
37     private Object JavaDoc semaphore = new Object JavaDoc();;
38
39     public void stop() {
40         messages.clear();
41     }
42
43     /**
44      * @return all the messages on the list so far, clearing the buffer
45      */

46     public List JavaDoc flushMessages() {
47         List JavaDoc answer = new ArrayList JavaDoc(messages);
48         messages.clear();
49         return answer;
50     }
51
52     public void onMessage(Message JavaDoc message) {
53         System.out.println("Received message: " + message);
54
55             messages.add(message);
56
57         synchronized (semaphore) {
58             semaphore.notifyAll();
59         }
60     }
61
62     public void waitForMessageToArrive() {
63         System.out.println("Waiting for message to arrive");
64
65         long start = System.currentTimeMillis();
66
67         try {
68             if (hasReceivedMessage()) {
69                 synchronized (semaphore) {
70                     semaphore.wait(4000);
71                 }
72             }
73         }
74         catch (InterruptedException JavaDoc e) {
75             System.out.println("Caught: " + e);
76         }
77         long end = System.currentTimeMillis() - start;
78
79         System.out.println("End of wait for " + end + " millis");
80     }
81
82     public void waitForMessagesToArrive(int messageCount) {
83         System.out.println("Waiting for message to arrive");
84
85         long start = System.currentTimeMillis();
86
87         for (int i = 0; i < 10; i++) {
88             try {
89                 if (hasReceivedMessages(messageCount)) {
90                     break;
91                 }
92                 synchronized (semaphore) {
93                     semaphore.wait(1000);
94                 }
95             }
96             catch (InterruptedException JavaDoc e) {
97                 System.out.println("Caught: " + e);
98             }
99         }
100         long end = System.currentTimeMillis() - start;
101
102         System.out.println("End of wait for " + end + " millis");
103     }
104
105     protected boolean hasReceivedMessage() {
106         return messages.isEmpty();
107     }
108
109     protected boolean hasReceivedMessages(int messageCount) {
110         return messages.size() >= messageCount;
111     }
112
113 }
114
Popular Tags