Ejemplo n.º 1
0
  @Override
  @SuppressWarnings("unchecked")
  public <E extends Event<?>> void route(
      Object key,
      E event,
      List<Registration<Object, ? extends BiConsumer<Object, ? extends Event<?>>>> consumers,
      Consumer<E> completionConsumer,
      Consumer<Throwable> errorConsumer) {
    if (null != consumers && !consumers.isEmpty()) {
      List<Registration<Object, ? extends BiConsumer<Object, ? extends Event<?>>>> regs =
          filter.filter(consumers, key);
      int size = regs.size();
      // old-school for loop is much more efficient than using an iterator
      for (int i = 0; i < size; i++) {
        Registration<Object, ? extends BiConsumer<Object, ? extends Event<?>>> reg = regs.get(i);

        if (null == reg || reg.isCancelled() || reg.isPaused()) {
          continue;
        }
        try {
          ((BiConsumer<Object, E>) reg.getObject()).accept(key, event);
        } catch (Exceptions.CancelException cancel) {
          reg.cancel();
        } catch (Throwable t) {
          if (null != errorConsumer) {
            errorConsumer.accept(Exceptions.addValueAsLastCause(t, event));
          } else {
            logger.error("Event routing failed for {}: {}", reg.getObject(), t.getMessage(), t);
            if (RuntimeException.class.isInstance(t)) {
              throw (RuntimeException) t;
            } else {
              throw new IllegalStateException(t);
            }
          }
        } finally {
          if (reg.isCancelAfterUse()) {
            reg.cancel();
          }
        }
      }
    }
    if (null != completionConsumer) {
      try {
        completionConsumer.accept(event);
      } catch (Throwable t) {
        if (null != errorConsumer) {
          errorConsumer.accept(Exceptions.addValueAsLastCause(t, event));
        } else {
          logger.error("Completion Consumer {} failed: {}", completionConsumer, t.getMessage(), t);
        }
      }
    }
  }
Ejemplo n.º 2
0
  @Override
  public void subscribe(Subscriber<? super T> s) {
    Consumer<Runnable> scheduler;

    try {
      scheduler = schedulerFactory.call();
    } catch (Throwable e) {
      Exceptions.throwIfFatal(e);
      EmptySubscription.error(s, e);
      return;
    }

    if (scheduler == null) {
      EmptySubscription.error(
          s, new NullPointerException("The schedulerFactory returned a null Function"));
      return;
    }

    if (value == null) {
      FluxPublishOn.ScheduledEmptySubscriptionEager parent =
          new FluxPublishOn.ScheduledEmptySubscriptionEager(s, scheduler);
      s.onSubscribe(parent);
      scheduler.accept(parent);
    } else {
      s.onSubscribe(new FluxPublishOn.ScheduledSubscriptionEagerCancel<>(s, value, scheduler));
    }
  }
Ejemplo n.º 3
0
 @Override
 public void subscribe(Subscriber<? super OUT> s) {
   if (s == null) throw Exceptions.spec_2_13_exception();
   downstream.subscribe(s);
 }