1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
package org.mule.tck.functional; |
11 | |
|
12 | |
import org.mule.api.MuleEventContext; |
13 | |
import org.mule.api.lifecycle.InitialisationException; |
14 | |
import org.mule.config.i18n.MessageFactory; |
15 | |
|
16 | |
import edu.emory.mathcs.backport.java.util.concurrent.CountDownLatch; |
17 | |
import edu.emory.mathcs.backport.java.util.concurrent.TimeUnit; |
18 | |
import junit.framework.AssertionFailedError; |
19 | |
|
20 | |
public class CountdownCallback implements EventCallback |
21 | |
{ |
22 | |
private CountDownLatch countDown; |
23 | |
|
24 | |
public CountdownCallback(int messagesExpected) |
25 | 0 | { |
26 | 0 | this.countDown = new CountDownLatch(messagesExpected); |
27 | 0 | } |
28 | |
|
29 | |
public void eventReceived(MuleEventContext context, Object Component) throws Exception |
30 | |
{ |
31 | 0 | synchronized (this) |
32 | |
{ |
33 | 0 | if (countDown.getCount() > 0) |
34 | |
{ |
35 | 0 | countDown.countDown(); |
36 | |
} |
37 | |
else |
38 | |
{ |
39 | 0 | throw new AssertionFailedError("Too many messages received"); |
40 | |
} |
41 | 0 | } |
42 | 0 | } |
43 | |
|
44 | |
public long getCount() throws InitialisationException |
45 | |
{ |
46 | 0 | if (countDown != null) |
47 | |
{ |
48 | 0 | return countDown.getCount(); |
49 | |
} |
50 | |
else |
51 | |
{ |
52 | 0 | throw new InitialisationException(MessageFactory.createStaticMessage("CountDownLatch has not been initialized."), null); |
53 | |
} |
54 | |
} |
55 | |
|
56 | |
public boolean await(long timeout) throws InterruptedException |
57 | |
{ |
58 | 0 | return countDown.await(timeout, TimeUnit.MILLISECONDS); |
59 | |
} |
60 | |
|
61 | |
} |