use of com.b2international.snowowl.eventbus.IMessage in project snow-owl by b2ihealthcare.
the class PipeTest method pipeSourceToTargetWithReply.
@Test
public void pipeSourceToTargetWithReply() throws Exception {
final CountDownLatch sourceLatch = new CountDownLatch(1);
final CountDownLatch targetLatch = new CountDownLatch(1);
target.registerHandler(ADDRESS, new CountDownHandler(SEND_MESSAGE, targetLatch) {
@Override
public void handle(IMessage message) {
super.handle(message);
message.reply(REPLY_MESSAGE);
}
});
bus.registerHandler(ADDRESS, new Pipe(target, ADDRESS));
bus.send(ADDRESS, SEND_MESSAGE, null, new CountDownHandler(REPLY_MESSAGE, sourceLatch));
wait(targetLatch);
wait(sourceLatch);
}
use of com.b2international.snowowl.eventbus.IMessage in project snow-owl by b2ihealthcare.
the class PipeTest method pipeToWorker.
@Test
public void pipeToWorker() throws Exception {
IEventBus target = EventBusUtil.getWorkerBus("worker", 2);
LifecycleUtil.activate(target);
final CountDownLatch sourceLatch = new CountDownLatch(1);
final CountDownLatch targetLatch = new CountDownLatch(1);
bus.registerHandler(ADDRESS, new Pipe(target, ADDRESS));
target.registerHandler("work-address", new IHandler<IMessage>() {
@Override
public void handle(IMessage message) {
try {
Thread.sleep(30_000L);
} catch (InterruptedException ignored) {
}
}
});
target.registerHandler(ADDRESS, new CountDownHandler(SEND_MESSAGE, targetLatch) {
@Override
public void handle(IMessage message) {
super.handle(message);
message.reply(REPLY_MESSAGE);
}
});
/*
* XXX: In a regular event bus, the third (reply) registered message handler would be queued after the
* long-running "work-address" handler, and would block.
*/
setWaitTime(1);
target.send("work-address", new Object(), null);
bus.send(ADDRESS, SEND_MESSAGE, null, new CountDownHandler(REPLY_MESSAGE, sourceLatch));
wait(targetLatch);
wait(sourceLatch);
}
use of com.b2international.snowowl.eventbus.IMessage in project snow-owl by b2ihealthcare.
the class AsyncRequest method execute.
/**
* Executes the asynchronous request using the event bus passed in.
* @param bus
* @return {@link Promise}
*/
public Promise<R> execute(IEventBus bus) {
final Promise<R> promise = new Promise<>();
final Class<R> responseType = request.getReturnType();
final CompositeClassLoader classLoader = new CompositeClassLoader();
classLoader.add(request.getClassLoader());
request.getNestedRequests().stream().map(Request::getClassLoader).forEach(classLoader::add);
request.getNestedRequests().stream().map(r -> r.getClass().getClassLoader()).forEach(classLoader::add);
bus.send(Request.ADDRESS, request, Request.TAG, Collections.emptyMap(), new IHandler<IMessage>() {
@Override
public void handle(IMessage message) {
try {
if (message.isSucceeded()) {
promise.resolve(message.body(responseType, classLoader), message.headers());
} else {
promise.reject(message.body(Throwable.class, AsyncRequest.class.getClassLoader()));
}
} catch (Throwable e) {
promise.reject(e);
}
}
});
return promise;
}
use of com.b2international.snowowl.eventbus.IMessage in project snow-owl by b2ihealthcare.
the class AbstractEventBusTest method registerHandlersWithLatch.
protected Collection<IHandler<IMessage>> registerHandlersWithLatch(final int numberOfHandlers, final String address, final CountDownLatch latch) {
final Collection<IHandler<IMessage>> handlers = new HashSet<IHandler<IMessage>>();
for (int i = 0; i < numberOfHandlers; i++) {
CountDownHandler handler = new CountDownHandler(SEND_MESSAGE, latch);
handlers.add(handler);
bus.registerHandler(address, handler);
}
return handlers;
}
use of com.b2international.snowowl.eventbus.IMessage in project snow-owl by b2ihealthcare.
the class EventBusSendTest method test_Send_Reply.
@Test
public void test_Send_Reply() throws InterruptedException {
// one when message arrives, and one for the reply
final int expectedHandlerCalls = 2;
final CountDownLatch latch = new CountDownLatch(expectedHandlerCalls);
bus.registerHandler(ADDRESS, new IHandler<IMessage>() {
@Override
public void handle(IMessage message) {
assertEquals(SEND_MESSAGE, message.body(String.class));
message.reply(REPLY_MESSAGE);
latch.countDown();
}
});
bus.send(ADDRESS, SEND_MESSAGE, null, new IHandler<IMessage>() {
@Override
public void handle(IMessage message) {
assertEquals(REPLY_MESSAGE, message.body(String.class));
latch.countDown();
}
});
wait(latch);
}
Aggregations