Search in sources :

Example 1 with IHandler

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

the class EventBus method unregisterHandler.

@Override
public IEventBus unregisterHandler(String address, IHandler<IMessage> handler) {
    if (isActive()) {
        MessageFactory.checkAddress(address);
        if (handler != null) {
            final ConcurrentMap<String, ChoosableList<Handler>> map = handler instanceof IEventBusProtocol ? protocolMap : handlerMap;
            final ChoosableList<Handler> handlers = map.get(address);
            if (handlers != null) {
                synchronized (handlers) {
                    final int size = handlers.list.size();
                    // we need fast ordered traversal for the round robin
                    for (int i = 0; i < size; i++) {
                        final Handler entry = handlers.list.get(i);
                        if (entry.handler == handler) {
                            handlers.list.remove(i);
                            if (handlers.list.isEmpty()) {
                                map.remove(address);
                                // if this was the last non protocol based handler, send unregistration event
                                if (!entry.isReplyHandler && !(handler instanceof IEventBusProtocol)) {
                                    addressBook.remove(address);
                                    fireEvent(new HandlerChangedEvent(this, address, false));
                                }
                            }
                            LOG.trace("Unregistered handler {} from address {}", entry.handler, address);
                            return this;
                        }
                    }
                }
            }
        }
    }
    return this;
}
Also used : IHandler(com.b2international.snowowl.eventbus.IHandler) IEventBusProtocol(com.b2international.snowowl.eventbus.net4j.IEventBusProtocol)

Example 2 with IHandler

use of com.b2international.snowowl.eventbus.IHandler 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 3 with IHandler

use of com.b2international.snowowl.eventbus.IHandler 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 IHandler

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

the class EventBus method registerHandler.

private void registerHandler(String address, IHandler<IMessage> handler, boolean replyHandler, boolean localOnly) {
    checkActive();
    MessageFactory.checkAddress(address);
    if (handler != null) {
        ChoosableList<Handler> handlers = null;
        if (handler instanceof IEventBusProtocol) {
            handlers = protocolMap.get(address);
            if (handlers == null) {
                handlers = new ChoosableList<Handler>();
                final ChoosableList<Handler> previousHandlers = protocolMap.putIfAbsent(address, handlers);
                if (previousHandlers != null) {
                    handlers = previousHandlers;
                }
            }
        } else {
            handlers = handlerMap.get(address);
            if (handlers == null) {
                handlers = new ChoosableList<Handler>();
                final ChoosableList<Handler> previousHandlers = handlerMap.putIfAbsent(address, handlers);
                if (previousHandlers != null) {
                    handlers = previousHandlers;
                }
            }
        }
        final Handler h = new Handler(address, handler, executorService, replyHandler);
        if (!handlers.list.contains(h)) {
            handlers.list.add(h);
            LOG.trace("Registered handler {} to address {}", handler, address);
            if (!replyHandler && handlers.list.size() == 1 && !(handler instanceof IEventBusProtocol)) {
                addressBook.add(address);
                fireEvent(new HandlerChangedEvent(this, address, true));
            }
        }
    }
}
Also used : IHandler(com.b2international.snowowl.eventbus.IHandler) IEventBusProtocol(com.b2international.snowowl.eventbus.net4j.IEventBusProtocol)

Aggregations

IHandler (com.b2international.snowowl.eventbus.IHandler)4 IMessage (com.b2international.snowowl.eventbus.IMessage)2 IEventBusProtocol (com.b2international.snowowl.eventbus.net4j.IEventBusProtocol)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 IEventBus (com.b2international.snowowl.eventbus.IEventBus)1 CountDownHandler (com.b2international.snowowl.eventbus.util.CountDownHandler)1 Collections (java.util.Collections)1 HashSet (java.util.HashSet)1 TimeUnit (java.util.concurrent.TimeUnit)1