/** * Atomically sets the single subscription and requests the missed amount from it. * * @param s the Subscription to set. * @return false if this arbiter is cancelled or there was a subscription already set */ protected final boolean set(Subscription s) { Objects.requireNonNull(s, "s"); Subscription a = this.s; if (a == Operators.cancelledSubscription()) { s.cancel(); return false; } if (a != null) { s.cancel(); Operators.reportSubscriptionSet(); return false; } if (S.compareAndSet(this, null, s)) { long r = REQUESTED.getAndSet(this, 0L); if (r != 0L) { s.request(r); } return true; } a = this.s; if (a != Operators.cancelledSubscription()) { s.cancel(); return false; } Operators.reportSubscriptionSet(); return false; }
@Override public void onSubscribe(Subscription s) { if (SubscriptionHelper.validateSubscription(this.s, s)) { return; } this.s = s; Subscriber<? super U> actual = this.actual; U b; try { b = bufferSupplier.get(); } catch (Throwable e) { cancelled = true; s.cancel(); EmptySubscription.error(e, actual); return; } if (b == null) { cancelled = true; s.cancel(); EmptySubscription.error(new NullPointerException("The buffer supplied is null"), actual); return; } buffer = b; Publisher<B> boundary; try { boundary = boundarySupplier.get(); } catch (Throwable ex) { cancelled = true; s.cancel(); EmptySubscription.error(ex, actual); return; } if (boundary == null) { cancelled = true; s.cancel(); EmptySubscription.error( new NullPointerException("The boundary publisher supplied is null"), actual); return; } BufferBoundarySubscriber<T, U, B> bs = new BufferBoundarySubscriber<T, U, B>(this); other = bs; actual.onSubscribe(this); if (!cancelled) { s.request(Long.MAX_VALUE); boundary.subscribe(bs); } }
void next() { Disposable o = other; U next; try { next = bufferSupplier.get(); } catch (Throwable e) { cancel(); actual.onError(e); return; } if (next == null) { cancel(); actual.onError(new NullPointerException("The buffer supplied is null")); return; } Publisher<B> boundary; try { boundary = boundarySupplier.get(); } catch (Throwable ex) { cancelled = true; s.cancel(); actual.onError(ex); return; } if (boundary == null) { cancelled = true; s.cancel(); actual.onError(new NullPointerException("The boundary publisher supplied is null")); return; } BufferBoundarySubscriber<T, U, B> bs = new BufferBoundarySubscriber<T, U, B>(this); if (!OTHER.compareAndSet(this, o, bs)) { return; } U b; synchronized (this) { b = buffer; if (b == null) { return; } buffer = next; } boundary.subscribe(bs); fastpathEmitMax(b, false, this); }
@Override public void onNext(Try<Optional<T>> t) { if (done) { return; } if (t.hasError()) { s.cancel(); onError(t.error()); } else { Optional<T> o = t.value(); if (o.isPresent()) { actual.onNext(o.get()); } else { s.cancel(); onComplete(); } } }
@Override public void cancel() { Subscription a = s; if (a != Operators.cancelledSubscription()) { a = S.getAndSet(this, Operators.cancelledSubscription()); if (a != null && a != Operators.cancelledSubscription()) { a.cancel(); } } }
/** * Sets the Subscription once but does not request anything. * * @param s the Subscription to set * @return true if successful, false if the current subscription is not null */ protected final boolean setWithoutRequesting(Subscription s) { Objects.requireNonNull(s, "s"); while (true) { Subscription a = this.s; if (a == Operators.cancelledSubscription()) { s.cancel(); return false; } if (a != null) { s.cancel(); Operators.reportSubscriptionSet(); return false; } if (S.compareAndSet(this, null, s)) { return true; } } }
@Override public void operationComplete(ChannelFuture future) throws Exception { if (log.isDebugEnabled()) { log.debug("Cancel connection"); } if (subscription != null) { subscription.cancel(); } subscription = null; }
@Override public void cancel() { if (!cancelled) { cancelled = true; s.cancel(); disposeOther(); if (enter()) { queue.clear(); } } }
@Override public void cancel() { super.cancel(); FallbackSubscriber fallbackSubscriber = this.fallbackSubscriber; if (fallbackSubscriber != null) { Subscription subscription = fallbackSubscriber.subscription; if (subscription != null) { fallbackSubscriber.subscription = null; subscription.cancel(); } } }
@Override public void cancel() { if (!cancelled) { cancelled = true; s.cancel(); if (WIP.getAndIncrement(this) == 0) { VALUE.lazySet(this, null); } } }
@Override public void onSubscribe(final Subscription s) { if (BackpressureUtils.checkSubscription(upstreamSubscription, s)) { this.upstreamSubscription = s; try { doOnSubscribe(s); } catch (Throwable t) { Exceptions.throwIfFatal(t); s.cancel(); onError(t); } } }
@Override public void onNext(T t) { if (done) { return; } try { onNext.accept(t); } catch (Throwable e) { s.cancel(); onError(e); return; } actual.onNext(t); }
public void demonstrateADetachedGraphStage() throws Exception { // tests: CompletionStage<Integer> result1 = Source.from(Arrays.asList(1, 2, 3)) .via(new TwoBuffer<>()) .runFold(0, (acc, n) -> acc + n, mat); assertEquals(new Integer(6), result1.toCompletableFuture().get(3, TimeUnit.SECONDS)); TestSubscriber.ManualProbe<Integer> subscriber = TestSubscriber.manualProbe(system); TestPublisher.Probe<Integer> publisher = TestPublisher.probe(0, system); RunnableGraph<NotUsed> flow2 = Source.fromPublisher(publisher).via(new TwoBuffer<>()).to(Sink.fromSubscriber(subscriber)); flow2.run(mat); Subscription sub = subscriber.expectSubscription(); // this happens even though the subscriber has not signalled any demand publisher.sendNext(1); publisher.sendNext(2); sub.cancel(); }
@Override public void cancel() { if (subscription != null) { subscription.cancel(); } }
protected void cancel(Subscription subscription) { if (subscription != SignalType.NOOP_SUBSCRIPTION) { subscription.cancel(); } }
@Override public void dispose() { s.cancel(); disposeOther(); }