Ejemplo n.º 1
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.º 2
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);
        }
      }
    }
  }
 @Override
 protected void doCancel() {
   if (cancelConsumer != null) {
     cancelConsumer.accept(null);
   }
   super.doCancel();
 }
 @Override
 protected void doOnSubscribe(Subscription subscription) {
   if (onSubscribeConsumer != null) {
     onSubscribeConsumer.accept(subscription);
   }
   subscriber.onSubscribe(this);
 }
Ejemplo n.º 5
0
  @Override
  protected void doNext(T ev) {
    if (consumer != null) {
      consumer.accept(ev);
    }

    COUNTING.incrementAndGet(this);

    if (upstreamSubscription != null
        && capacity != Long.MAX_VALUE
        && COUNTED.decrementAndGet(this) == 0) {
      requestMore(COUNTING.getAndSet(this, 0l));
    }
  }