KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > tck > ExchangeCompletedListener


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. 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 package org.apache.servicemix.tck;
18
19 import java.util.HashMap JavaDoc;
20 import java.util.Iterator JavaDoc;
21 import java.util.Map JavaDoc;
22
23 import javax.jbi.messaging.ExchangeStatus;
24 import javax.jbi.messaging.MessageExchange;
25
26 import junit.framework.Assert;
27
28 import org.apache.servicemix.jbi.event.ExchangeEvent;
29 import org.apache.servicemix.jbi.event.ExchangeListener;
30
31 public class ExchangeCompletedListener extends Assert implements ExchangeListener {
32
33     private Map JavaDoc exchanges = new HashMap JavaDoc();
34
35     private long timeout;
36
37     public ExchangeCompletedListener() {
38         this(1000);
39     }
40
41     public ExchangeCompletedListener(long timeout) {
42         this.timeout = timeout;
43     }
44
45     public void exchangeSent(ExchangeEvent event) {
46         synchronized (exchanges) {
47             exchanges.put(event.getExchange().getExchangeId(), event.getExchange());
48             exchanges.notify();
49         }
50     }
51
52     public void assertExchangeCompleted() throws Exception JavaDoc {
53         long start = System.currentTimeMillis();
54         MessageExchange active = null;
55         while (true) {
56             synchronized (exchanges) {
57                 for (Iterator JavaDoc it = exchanges.values().iterator(); it.hasNext();) {
58                     active = null;
59                     MessageExchange me = (MessageExchange) it.next();
60                     if (me.getStatus() == ExchangeStatus.ACTIVE) {
61                         active = me;
62                         break;
63                     }
64                 }
65                 if (active == null) {
66                     break;
67                 }
68                 long remain = timeout - (System.currentTimeMillis() - start);
69                 if (remain <= 0) {
70                     assertTrue("Exchange is ACTIVE: " + active, active.getStatus() != ExchangeStatus.ACTIVE);
71                 } else {
72                     exchanges.wait(remain);
73                 }
74             }
75         }
76     }
77
78 }
79
Popular Tags