1 17 package org.apache.servicemix.tck; 18 19 import java.util.HashMap ; 20 import java.util.Iterator ; 21 import java.util.Map ; 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 exchanges = new HashMap (); 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 { 53 long start = System.currentTimeMillis(); 54 MessageExchange active = null; 55 while (true) { 56 synchronized (exchanges) { 57 for (Iterator 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 |