Skip to content

Instantly share code, notes, and snippets.

@danbev
Created July 17, 2012 08:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danbev/3128042 to your computer and use it in GitHub Desktop.
Save danbev/3128042 to your computer and use it in GitHub Desktop.
SwitchYardTestKit enhancements
/**
* 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