Search in sources :

Example 1 with CountDownHandler

use of com.b2international.snowowl.eventbus.util.CountDownHandler 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);
}
Also used : IMessage(com.b2international.snowowl.eventbus.IMessage) Pipe(com.b2international.snowowl.eventbus.Pipe) CountDownLatch(java.util.concurrent.CountDownLatch) CountDownHandler(com.b2international.snowowl.eventbus.util.CountDownHandler) Test(org.junit.Test)

Example 2 with CountDownHandler

use of com.b2international.snowowl.eventbus.util.CountDownHandler 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);
}
Also used : IMessage(com.b2international.snowowl.eventbus.IMessage) Pipe(com.b2international.snowowl.eventbus.Pipe) CountDownLatch(java.util.concurrent.CountDownLatch) CountDownHandler(com.b2international.snowowl.eventbus.util.CountDownHandler) IEventBus(com.b2international.snowowl.eventbus.IEventBus) Test(org.junit.Test)

Example 3 with CountDownHandler

use of com.b2international.snowowl.eventbus.util.CountDownHandler 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;
}
Also used : IMessage(com.b2international.snowowl.eventbus.IMessage) IHandler(com.b2international.snowowl.eventbus.IHandler) CountDownHandler(com.b2international.snowowl.eventbus.util.CountDownHandler) HashSet(java.util.HashSet)

Example 4 with CountDownHandler

use of com.b2international.snowowl.eventbus.util.CountDownHandler in project snow-owl by b2ihealthcare.

the class PipeTest method pipeSourceToTarget.

@Test
public void pipeSourceToTarget() throws Exception {
    final CountDownLatch targetLatch = new CountDownLatch(1);
    target.registerHandler(ADDRESS, new CountDownHandler(SEND_MESSAGE, targetLatch));
    bus.registerHandler(ADDRESS, new Pipe(target, ADDRESS));
    bus.send(ADDRESS, SEND_MESSAGE, null);
    wait(targetLatch);
}
Also used : Pipe(com.b2international.snowowl.eventbus.Pipe) CountDownLatch(java.util.concurrent.CountDownLatch) CountDownHandler(com.b2international.snowowl.eventbus.util.CountDownHandler) Test(org.junit.Test)

Aggregations

CountDownHandler (com.b2international.snowowl.eventbus.util.CountDownHandler)4 IMessage (com.b2international.snowowl.eventbus.IMessage)3 Pipe (com.b2international.snowowl.eventbus.Pipe)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 Test (org.junit.Test)3 IEventBus (com.b2international.snowowl.eventbus.IEventBus)1 IHandler (com.b2international.snowowl.eventbus.IHandler)1 HashSet (java.util.HashSet)1