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