Search in sources :

Example 1 with IMessage

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);
}
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 IMessage

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);
}
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 IMessage

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;
}
Also used : TimeUnit(java.util.concurrent.TimeUnit) ScheduleJobRequestBuilder(com.b2international.snowowl.core.jobs.ScheduleJobRequestBuilder) Promise(com.b2international.snowowl.core.events.util.Promise) JobRequests(com.b2international.snowowl.core.jobs.JobRequests) ServiceProvider(com.b2international.snowowl.core.ServiceProvider) CompositeClassLoader(com.b2international.commons.CompositeClassLoader) IEventBus(com.b2international.snowowl.eventbus.IEventBus) IMessage(com.b2international.snowowl.eventbus.IMessage) Collections(java.util.Collections) IHandler(com.b2international.snowowl.eventbus.IHandler) Promise(com.b2international.snowowl.core.events.util.Promise) IMessage(com.b2international.snowowl.eventbus.IMessage) CompositeClassLoader(com.b2international.commons.CompositeClassLoader)

Example 4 with IMessage

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;
}
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 5 with IMessage

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

Aggregations

IMessage (com.b2international.snowowl.eventbus.IMessage)5 CountDownHandler (com.b2international.snowowl.eventbus.util.CountDownHandler)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 Test (org.junit.Test)3 IEventBus (com.b2international.snowowl.eventbus.IEventBus)2 IHandler (com.b2international.snowowl.eventbus.IHandler)2 Pipe (com.b2international.snowowl.eventbus.Pipe)2 CompositeClassLoader (com.b2international.commons.CompositeClassLoader)1 ServiceProvider (com.b2international.snowowl.core.ServiceProvider)1 Promise (com.b2international.snowowl.core.events.util.Promise)1 JobRequests (com.b2international.snowowl.core.jobs.JobRequests)1 ScheduleJobRequestBuilder (com.b2international.snowowl.core.jobs.ScheduleJobRequestBuilder)1 Collections (java.util.Collections)1 HashSet (java.util.HashSet)1 TimeUnit (java.util.concurrent.TimeUnit)1