KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > tool > MemMessageIdList


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.tool;
19
20 import java.util.ArrayList JavaDoc;
21 import java.util.List JavaDoc;
22
23 import javax.jms.JMSException JavaDoc;
24 import javax.jms.Message JavaDoc;
25 import javax.jms.MessageListener JavaDoc;
26
27
28 import org.apache.commons.logging.Log;
29 import org.apache.commons.logging.LogFactory;
30
31 /**
32  * A simple container of messages for performing testing and rendezvous style
33  * code. You can use this class a {@link MessageListener} and then make
34  * assertions about how many messages it has received allowing a certain maximum
35  * amount of time to ensure that the test does not hang forever.
36  * <p/>
37  * Also you can chain these instances together with the
38  * {@link #setParent(MessageListener)} method so that you can aggregate the
39  * total number of messages consumed across a number of consumers.
40  *
41  * @version $Revision: 1.6 $
42  */

43 public class MemMessageIdList implements MessageListener JavaDoc {
44
45     protected static final Log log = LogFactory.getLog(MemMessageIdList.class);
46
47     private List JavaDoc messageIds = new ArrayList JavaDoc();
48     private Object JavaDoc semaphore;
49     private boolean verbose;
50     private MessageListener JavaDoc parent;
51     private long maximumDuration = 15000L;
52
53     public MemMessageIdList() {
54         this(new Object JavaDoc());
55     }
56
57     public MemMessageIdList(Object JavaDoc semaphore) {
58         this.semaphore = semaphore;
59     }
60
61     public boolean equals(Object JavaDoc that) {
62         if (that instanceof MemMessageIdList) {
63             MemMessageIdList thatListMem = (MemMessageIdList) that;
64             return getMessageIds().equals(thatListMem.getMessageIds());
65         }
66         return false;
67     }
68
69     public int hashCode() {
70         synchronized (semaphore) {
71             return messageIds.hashCode() + 1;
72         }
73     }
74
75     public String JavaDoc toString() {
76         synchronized (semaphore) {
77             return messageIds.toString();
78         }
79     }
80
81     /**
82      * @return all the messages on the list so far, clearing the buffer
83      */

84     public List JavaDoc flushMessages() {
85         synchronized (semaphore) {
86             List JavaDoc answer = new ArrayList JavaDoc(messageIds);
87             messageIds.clear();
88             return answer;
89         }
90     }
91
92     public synchronized List JavaDoc getMessageIds() {
93         synchronized (semaphore) {
94             return new ArrayList JavaDoc(messageIds);
95         }
96     }
97
98     public void onMessage(Message JavaDoc message) {
99         String JavaDoc id = null;
100         try {
101             id = message.getJMSMessageID();
102             synchronized (semaphore) {
103                 messageIds.add(id);
104                 semaphore.notifyAll();
105             }
106             if (verbose) {
107                 log.info("Received message: " + message);
108             }
109         } catch (JMSException JavaDoc e) {
110             e.printStackTrace();
111         }
112         if (parent != null) {
113             parent.onMessage(message);
114         }
115     }
116
117     public int getMessageCount() {
118         synchronized (semaphore) {
119             return messageIds.size();
120         }
121     }
122
123     public void waitForMessagesToArrive(int messageCount) {
124         log.info("Waiting for " + messageCount + " message(s) to arrive");
125
126         long start = System.currentTimeMillis();
127
128         for (int i = 0; i < messageCount; i++) {
129             try {
130                 if (hasReceivedMessages(messageCount)) {
131                     break;
132                 }
133                 long duration = System.currentTimeMillis() - start;
134                 if (duration >= maximumDuration) {
135                     break;
136                 }
137                 synchronized (semaphore) {
138                     semaphore.wait(maximumDuration - duration);
139                 }
140             } catch (InterruptedException JavaDoc e) {
141                 log.info("Caught: " + e);
142             }
143         }
144         long end = System.currentTimeMillis() - start;
145
146         log.info("End of wait for " + end + " millis and received: " + getMessageCount() + " messages");
147     }
148
149
150     public boolean hasReceivedMessage() {
151         return getMessageCount() == 0;
152     }
153
154     public boolean hasReceivedMessages(int messageCount) {
155         return getMessageCount() >= messageCount;
156     }
157
158     public boolean isVerbose() {
159         return verbose;
160     }
161
162     public void setVerbose(boolean verbose) {
163         this.verbose = verbose;
164     }
165
166     public MessageListener JavaDoc getParent() {
167         return parent;
168     }
169
170     /**
171      * Allows a parent listener to be specified such as to aggregate messages
172      * consumed across consumers
173      */

174     public void setParent(MessageListener JavaDoc parent) {
175         this.parent = parent;
176     }
177
178 }
179
Popular Tags