Created
July 17, 2012 08:31
-
-
Save danbev/3128042 to your computer and use it in GitHub Desktop.
SwitchYardTestKit enhancements
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Waits for an Exchange to complete and returns the content of the message that | |
* was recieved. | |
* | |
* @param type The type of the contents of the Message | |
* @return T an instance of type that is contents of the message. | |
*/ | |
public <T> T waitForCompletion(Class<T> type) { | |
final CompletionNotifier<T> notifier = new CompletionNotifier<T>(type); | |
getServiceDomain().addEventObserver(notifier, ExchangeCompletionEvent.class); | |
notifier.await(); | |
return notifier.getMessage(); | |
} | |
private class CompletionNotifier<T> implements EventObserver { | |
private T _processed; | |
private Class<T> _type; | |
private CountDownLatch latch = new CountDownLatch(1); | |
public CompletionNotifier(final Class<T> type) { | |
_type = type; | |
} | |
public void await() { | |
try { | |
latch.await(); | |
} catch (final InterruptedException ignored) { | |
} | |
} | |
public void await(final long ms) { | |
try { | |
latch.await(ms, TimeUnit.MILLISECONDS); | |
} catch (InterruptedException ignored) { | |
} | |
} | |
@Override | |
public void notify(final EventObject event) { | |
final Exchange ex = (Exchange) event.getSource(); | |
_processed = ex.getMessage().getContent(_type); | |
latch.countDown(); | |
} | |
public T getMessage() { | |
return _processed; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment