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