Java Code Examples for org.reactivestreams.Subscription#cancel()

The following examples show how to use org.reactivestreams.Subscription#cancel() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FluxBufferBoundary.java    From reactor-core with Apache License 2.0 6 votes vote down vote up
void otherComplete() {
	Subscription s = S.getAndSet(this, Operators.cancelledSubscription());
	if(s != Operators.cancelledSubscription()) {
		C b;
		synchronized (this) {
			b = buffer;
			buffer = null;
		}

		if(s != null){
			s.cancel();
		}

		if (b != null && !b.isEmpty()) {
			if (emit(b)) {
				actual.onComplete();
			} //failed emit will discard buffer content
		}
		else {
			actual.onComplete();
		}
	}
}
 
Example 2
Source File: SafeSubscriber.java    From smallrye-mutiny with Apache License 2.0 6 votes vote down vote up
@Override
public void onSubscribe(Subscription s) {
    if (upstream.compareAndSet(null, s)) {
        try {
            downstream.onSubscribe(this);
        } catch (Throwable e) {
            done = true;
            // can't call onError because the actual's state may be corrupt at this point
            try {
                s.cancel();
            } catch (Throwable e1) {
                // ignore it, nothing we can do.
            }
        }
    } else {
        s.cancel();
    }
}
 
Example 3
Source File: RSocketResponder.java    From rsocket-java with Apache License 2.0 6 votes vote down vote up
private void handleCancelFrame(int streamId) {
  Subscription subscription = sendingSubscriptions.remove(streamId);
  Processor<Payload, Payload> processor = channelProcessors.remove(streamId);

  if (processor != null) {
    try {
      processor.onError(new CancellationException("Disposed"));
    } catch (Exception e) {
      // ignore
    }
  }

  if (subscription != null) {
    subscription.cancel();
  }
}
 
Example 4
Source File: Util.java    From brave with Apache License 2.0 5 votes vote down vote up
public static boolean validate(Subscription current, Subscription next) {
  if (next == null) {
    RxJavaPlugins.onError(new NullPointerException("next is null"));
    return false;
  }
  if (current != null) {
    next.cancel();
    RxJavaPlugins.onError(new ProtocolViolationException("Subscription already set!"));
    return false;
  }
  return true;
}
 
Example 5
Source File: MonoToCompletableFuture.java    From reactor-core with Apache License 2.0 5 votes vote down vote up
@Override
public void onSubscribe(Subscription s) {
	if (Operators.validate(ref.getAndSet(s), s)) {
		s.request(Long.MAX_VALUE);
	}
	else {
		s.cancel();
	}
}
 
Example 6
Source File: WrappedSubscriber.java    From smallrye-reactive-streams-operators with Apache License 2.0 5 votes vote down vote up
@Override
public void onSubscribe(Subscription subscription) {
    Objects.requireNonNull(subscription);
    if (subscribed.compareAndSet(false, true)) {
        source.onSubscribe(
                new WrappedSubscription(subscription, () -> future.completeExceptionally(new CancellationException())));
    } else {
        subscription.cancel();
    }
}
 
Example 7
Source File: ReplayProcessor.java    From reactor-core with Apache License 2.0 5 votes vote down vote up
@Override
public void onSubscribe(Subscription s) {
	if (buffer.isDone()) {
		s.cancel();
	}
	else if (Operators.validate(subscription, s)) {
		subscription = s;
		s.request(Long.MAX_VALUE);
	}
}
 
Example 8
Source File: MonoToCompletableFuture.java    From reactor-core with Apache License 2.0 5 votes vote down vote up
@Override
public void onNext(T t) {
	Subscription s = ref.getAndSet(null);
	if (s != null) {
		complete(t);
		if (cancelSourceOnNext) {
			s.cancel();
		}
	}
	else {
		Operators.onNextDropped(t, currentContext());
	}
}
 
Example 9
Source File: AmqpCreditBasedSender.java    From smallrye-reactive-messaging with Apache License 2.0 5 votes vote down vote up
@Override
public void cancel() {
    Subscription sub = upstream.getAndSet(Subscriptions.CANCELLED);
    if (sub != null && sub != Subscriptions.CANCELLED) {
        sub.cancel();
    }
}
 
Example 10
Source File: FluxTakeUntilOther.java    From reactor-core with Apache License 2.0 5 votes vote down vote up
void setOther(Subscription s) {
	if (!OTHER.compareAndSet(this, null, s)) {
		s.cancel();
		if (other != Operators.cancelledSubscription()) {
			Operators.reportSubscriptionSet();
		}
	}
}
 
Example 11
Source File: DirectProcessor.java    From reactive-streams-commons with Apache License 2.0 5 votes vote down vote up
@Override
public void onSubscribe(Subscription s) {
    Objects.requireNonNull(s, "s");
    if (subscribers != TERMINATED) {
        s.request(Long.MAX_VALUE);
    } else {
        s.cancel();
    }
}
 
Example 12
Source File: MultiBufferWithTimeoutOp.java    From smallrye-mutiny with Apache License 2.0 5 votes vote down vote up
@Override
public void onSubscribe(Subscription subscription) {
    if (upstream.compareAndSet(null, subscription)) {
        doOnSubscribe();
        downstream.onSubscribe(this);
    } else {
        subscription.cancel();
    }
}
 
Example 13
Source File: Operators.java    From reactor-core with Apache License 2.0 5 votes vote down vote up
/**
 * A generic utility to atomically replace a subscription or cancel the replacement
 * if the current subscription is marked as already cancelled (as in
 * {@link #cancelledSubscription()}).
 *
 * @param field The Atomic container
 * @param instance the instance reference
 * @param s the subscription
 * @param <F> the instance type
 *
 * @return true if replaced
 */
public static <F> boolean replace(AtomicReferenceFieldUpdater<F, Subscription> field,
		F instance,
		Subscription s) {
	for (; ; ) {
		Subscription a = field.get(instance);
		if (a == CancelledSubscription.INSTANCE) {
			s.cancel();
			return false;
		}
		if (field.compareAndSet(instance, a, s)) {
			return true;
		}
	}
}
 
Example 14
Source File: HttpStreamReader.java    From armeria with Apache License 2.0 5 votes vote down vote up
@Override
public void onSubscribe(Subscription subscription) {
    this.subscription = subscription;
    if (cancelled) {
        subscription.cancel();
        return;
    }
    if (deferredInitialMessageRequest > 0) {
        request(deferredInitialMessageRequest);
    }
}
 
Example 15
Source File: ChannelSendOperator.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
public void cancel() {
	Subscription s = this.subscription;
	if (s != null) {
		this.subscription = null;
		try {
			s.cancel();
		}
		finally {
			releaseCachedItem();
		}
	}
}
 
Example 16
Source File: PublisherTakeUntil.java    From reactive-streams-commons with Apache License 2.0 5 votes vote down vote up
void setOther(Subscription s) {
    if (!OTHER.compareAndSet(this, null, s)) {
        s.cancel();
        if (other != SubscriptionHelper.cancelled()) {
            SubscriptionHelper.reportSubscriptionSet();
        }
    }
}
 
Example 17
Source File: Subscriptions.java    From smallrye-mutiny with Apache License 2.0 5 votes vote down vote up
@Override
public void cancel() {
    Subscription actual = subscription.get();
    if (actual != CANCELLED) {
        actual = subscription.getAndSet(CANCELLED);
        if (actual != null && actual != CANCELLED) {
            actual.cancel();
        }
    }
}
 
Example 18
Source File: MonoDelayUntil.java    From reactor-core with Apache License 2.0 5 votes vote down vote up
@Override
public void onSubscribe(Subscription s) {
	if (Operators.setOnce(S, this, s)) {
		s.request(Long.MAX_VALUE);
	} else {
		s.cancel();
	}
}
 
Example 19
Source File: BlockingBaseSubscriber.java    From RxJava3-preview with Apache License 2.0 5 votes vote down vote up
@Override
public final void onSubscribe(Subscription s) {
    if (SubscriptionHelper.validate(this.s, s)) {
        this.s = s;
        if (!cancelled) {
            s.request(Long.MAX_VALUE);
            if (cancelled) {
                this.s = SubscriptionHelper.CANCELLED;
                s.cancel();
            }
        }
    }
}
 
Example 20
Source File: MultiSkipUntilPublisherOp.java    From smallrye-mutiny with Apache License 2.0 5 votes vote down vote up
void setOtherSubscription(Subscription s) {
    if (other.compareAndSet(null, s)) {
        s.request(1);
    } else {
        s.cancel();
    }
}