Java Code Examples for io.reactivex.SingleObserver#onSubscribe()

The following examples show how to use io.reactivex.SingleObserver#onSubscribe() . 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: SubscribeOnlyOnceSingleOperator.java    From reactive-grpc with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public SingleObserver<? super T> apply(final SingleObserver<? super T> observer) {
    return new SingleObserver<T>() {
        @Override
        public void onSubscribe(Disposable d) {
            if (subscribedOnce.getAndSet(true)) {
                throw new NullPointerException("You cannot directly subscribe to a gRPC service multiple times " +
                        "concurrently. Use Flowable.share() instead.");
            } else {
                observer.onSubscribe(d);
            }
        }

        @Override
        public void onSuccess(T t) {
            observer.onSuccess(t);
        }

        @Override
        public void onError(Throwable e) {
            observer.onError(e);
        }
    };
}
 
Example 2
Source File: SubscribeOnlyOnceTest.java    From reactive-grpc with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void subscribeOnlyOnceSingleOperatorErrorsWhenMultipleSubscribe() {
    SubscribeOnlyOnceSingleOperator<Object> op = new SubscribeOnlyOnceSingleOperator<Object>();
    SingleObserver<Object> innerSub = mock(SingleObserver.class);
    final Disposable disposable = mock(Disposable.class);

    final SingleObserver<Object> outerSub = op.apply(innerSub);

    outerSub.onSubscribe(disposable);
    assertThatThrownBy(new ThrowableAssert.ThrowingCallable() {
        @Override
        public void call() {
            outerSub.onSubscribe(disposable);
        }
    })
            .isInstanceOf(NullPointerException.class)
            .hasMessageContaining("cannot directly subscribe to a gRPC service multiple times");

    verify(innerSub, times(1)).onSubscribe(disposable);
}
 
Example 3
Source File: MemberSingle.java    From rxjava2-jdbc with Apache License 2.0 6 votes vote down vote up
@Override
protected void subscribeActual(SingleObserver<? super Member<T>> observer) {
    // the action of checking out a member from the pool is implemented as a
    // subscription to the singleton MemberSingle
    MemberSingleObserver<T> m = new MemberSingleObserver<T>(observer, this);
    observer.onSubscribe(m);
    if (pool.isClosed()) {
        observer.onError(new PoolClosedException());
        return;
    }
    add(m);
    if (m.isDisposed()) {
        remove(m);
    } else {
        // atomically change requested
        while (true) {
            Observers<T> a = observers.get();
            if (observers.compareAndSet(a, a.withRequested(a.requested + 1))) {
                break;
            }
        }
    }
    log.debug("subscribed");
    drain();
}
 
Example 4
Source File: RxJava2FutureUtils.java    From future-converter with Apache License 2.0 6 votes vote down vote up
@Override
protected void subscribeActual(SingleObserver<? super T> observer) {
    ValueSourceDisposable disposable = new ValueSourceDisposable();
    valueSource.addCallbacks(
        result -> {
            try {
                observer.onSuccess(result);
            } catch (Throwable e) {
                observer.onError(e);
            }
        },
        ex -> {
            if (!disposable.isDisposed()) {
                observer.onError(ex);
            }
        }
    );
    observer.onSubscribe(disposable);
}
 
Example 5
Source File: SingleCircuitBreaker.java    From resilience4j with Apache License 2.0 5 votes vote down vote up
@Override
protected void subscribeActual(SingleObserver<? super T> downstream) {
    if (circuitBreaker.tryAcquirePermission()) {
        upstream.subscribe(new CircuitBreakerSingleObserver(downstream));
    } else {
        downstream.onSubscribe(EmptyDisposable.INSTANCE);
        downstream.onError(createCallNotPermittedException(circuitBreaker));
    }
}
 
Example 6
Source File: SingleBulkhead.java    From resilience4j with Apache License 2.0 5 votes vote down vote up
@Override
protected void subscribeActual(SingleObserver<? super T> downstream) {
    if (bulkhead.tryAcquirePermission()) {
        upstream.subscribe(new BulkheadSingleObserver(downstream));
    } else {
        downstream.onSubscribe(EmptyDisposable.INSTANCE);
        downstream.onError(BulkheadFullException.createBulkheadFullException(bulkhead));
    }
}
 
Example 7
Source File: SingleRateLimiter.java    From resilience4j with Apache License 2.0 5 votes vote down vote up
@Override
protected void subscribeActual(SingleObserver<? super T> downstream) {
    long waitDuration = rateLimiter.reservePermission();
    if (waitDuration >= 0) {
        if (waitDuration > 0) {
            Completable.timer(waitDuration, TimeUnit.NANOSECONDS)
                .subscribe(() -> upstream.subscribe(new RateLimiterSingleObserver(downstream)));
        } else {
            upstream.subscribe(new RateLimiterSingleObserver(downstream));
        }
    } else {
        downstream.onSubscribe(EmptyDisposable.INSTANCE);
        downstream.onError(RequestNotPermitted.createRequestNotPermitted(rateLimiter));
    }
}