Java Code Examples for org.reactivestreams.Subscriber#onSubscribe()

The following examples show how to use org.reactivestreams.Subscriber#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: FlowableFromFuture.java    From RxJava3-preview with Apache License 2.0 6 votes vote down vote up
@Override
public void subscribeActual(Subscriber<? super T> s) {
    DeferredScalarSubscription<T> deferred = new DeferredScalarSubscription<T>(s);
    s.onSubscribe(deferred);

    T v;
    try {
        v = unit != null ? future.get(timeout, unit) : future.get();
    } catch (Throwable ex) {
        Exceptions.throwIfFatal(ex);
        if (!deferred.isCancelled()) {
            s.onError(ex);
        }
        return;
    }
    if (v == null) {
        s.onError(new NullPointerException("The future returned null"));
    } else {
        deferred.complete(v);
    }
}
 
Example 2
Source File: MockedSender.java    From smallrye-reactive-messaging with Apache License 2.0 6 votes vote down vote up
@Override
public void subscribe(Subscriber<? super Message<T>> subscriber) {
    if (!this.subscriber.compareAndSet(null, subscriber)) {
        subscriber.onSubscribe(new Subscription() {
            @Override
            public void request(long l) {
            }

            @Override
            public void cancel() {
            }
        });
        subscriber.onError(new RuntimeException("I only support one subscriber"));
    } else {
        publishers.add(this);
        subscriber.onSubscribe(this);
    }
}
 
Example 3
Source File: SubscriberWrapper.java    From smallrye-reactive-messaging with Apache License 2.0 6 votes vote down vote up
@Override
public void subscribe(Subscriber<? super T> s) {
    if (!this.subscriber.compareAndSet(null, s)) {
        s.onSubscribe(new Subscription() {
            @Override
            public void request(long n) {
                // Ignored.
            }

            @Override
            public void cancel() {
                // Ignored.
            }
        });
        s.onError(ex.illegalStateForNotSupported("Broadcast"));
    }
}
 
Example 4
Source File: WriteResultPublisher.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Override
void subscribe(WriteResultPublisher publisher, Subscriber<? super Void> subscriber) {
	Assert.notNull(subscriber, "Subscriber must not be null");
	if (publisher.changeState(this, SUBSCRIBING)) {
		Subscription subscription = new WriteResultSubscription(publisher);
		publisher.subscriber = subscriber;
		subscriber.onSubscribe(subscription);
		publisher.changeState(SUBSCRIBING, SUBSCRIBED);
		// Now safe to check "beforeSubscribed" flags, they won't change once in NO_DEMAND
		if (publisher.completedBeforeSubscribed) {
			publisher.publishComplete();
		}
		Throwable publisherError = publisher.errorBeforeSubscribed;
		if (publisherError != null) {
			publisher.publishError(publisherError);
		}
	}
	else {
		throw new IllegalStateException(toString());
	}
}
 
Example 5
Source File: FlowableFromCallable.java    From RxJava3-preview with Apache License 2.0 6 votes vote down vote up
@Override
public void subscribeActual(Subscriber<? super T> s) {
    DeferredScalarSubscription<T> deferred = new DeferredScalarSubscription<T>(s);
    s.onSubscribe(deferred);

    T t;
    try {
        t = ObjectHelper.requireNonNull(callable.call(), "The callable returned a null value");
    } catch (Throwable ex) {
        Exceptions.throwIfFatal(ex);
        s.onError(ex);
        return;
    }

    deferred.complete(t);
}
 
Example 6
Source File: ConnectableProcessor.java    From smallrye-mutiny with Apache License 2.0 6 votes vote down vote up
private void manageSubscribeInTheHasSubscriptionState(Subscriber<? super T> subscriber) {
    // We already have a subscription, use it.
    // However, we could complete of failed in the meantime.
    subscriber.onSubscribe(
            new WrappedSubscription(subscription.get(),
                    () -> this.subscriber.set(new Subscriptions.CancelledSubscriber<>())));
    if (!state.compareAndSet(State.HAS_SUBSCRIPTION, State.PROCESSING)) {
        if (state.get() == State.FAILED) {
            subscriber.onError(failure.get());
        } else if (state.get() == State.COMPLETE) {
            subscriber.onComplete();
        } else {
            throw new IllegalStateException("Illegal transition - subscribe called in the "
                    + state.get().name() + " state");
        }
    }
}
 
Example 7
Source File: ScheduledPublisher.java    From smallrye-mutiny with Apache License 2.0 6 votes vote down vote up
@Override
public void subscribe(Subscriber<? super Integer> subscriber) {
    assertEquals(activePublishers.incrementAndGet(), 1);
    subscriber.onSubscribe(new Subscription() {
        @Override
        public void request(long n) {
            if (published.compareAndSet(false, true)) {
                getExecutorService().schedule(() -> {
                    subscriber.onNext(id);
                    getExecutorService().schedule(() -> {
                        activePublishers.decrementAndGet();
                        subscriber.onComplete();
                    }, 100, TimeUnit.MILLISECONDS);
                }, 100, TimeUnit.MILLISECONDS);
            }
        }

        @Override
        public void cancel() {
        }
    });
}
 
Example 8
Source File: FlowableFromIterable.java    From RxJava3-preview with Apache License 2.0 6 votes vote down vote up
public static <T> void subscribe(Subscriber<? super T> s, Iterator<? extends T> it) {
    boolean hasNext;
    try {
        hasNext = it.hasNext();
    } catch (Throwable e) {
        Exceptions.throwIfFatal(e);
        EmptySubscription.error(e, s);
        return;
    }

    if (!hasNext) {
        EmptySubscription.complete(s);
        return;
    }

    if (s instanceof ConditionalSubscriber) {
        s.onSubscribe(new IteratorConditionalSubscription<T>(
                (ConditionalSubscriber<? super T>)s, it));
    } else {
        s.onSubscribe(new IteratorSubscription<T>(s, it));
    }
}
 
Example 9
Source File: FileAsyncResponseTransformerTest.java    From aws-sdk-java-v2 with Apache License 2.0 6 votes vote down vote up
@Override
public void subscribe(Subscriber s) {

    s.onSubscribe(new Subscription() {
        @Override
        public void request(long l) {

            if (isDone) {
                return;
            }
            requestNumber.incrementAndGet();

            if (requestNumber.get() == 2) {
                isDone = true;
                s.onComplete();
                return;
            }

            s.onNext(ByteBuffer.wrap(RandomStringUtils.randomAlphanumeric(30000).getBytes()));
        }

        @Override
        public void cancel() {
        }
    });
}
 
Example 10
Source File: CompletableFutureAsPublisher.java    From Hands-On-Reactive-Programming-in-Spring-5 with MIT License 5 votes vote down vote up
@Override
public void subscribe(Subscriber<? super T> s) {
    Objects.requireNonNull(s, "Subscriber must be present");

    CompletableFutureSubscription<T> subscription =
            new CompletableFutureSubscription<>(completableFuture, s);

    s.onSubscribe(subscription);
}
 
Example 11
Source File: MultiPublishOp.java    From smallrye-mutiny with Apache License 2.0 5 votes vote down vote up
@Override
public void subscribe(Subscriber<? super T> child) {
    InnerSubscriber<T> inner = new InnerSubscriber<>(child);
    child.onSubscribe(inner);
    // concurrent connection/disconnection may change the state,
    // we loop to be atomic while the child subscribes
    for (;;) {
        // get the current subscriber-to-source
        PublishSubscriber<T> r = curr.get();
        // if there isn't one or it is cancelled/disposed
        if (r == null || r.cancelled.get()) {
            // create a new subscriber to source
            PublishSubscriber<T> u = new PublishSubscriber<>(curr, bufferSize);
            // let's try setting it as the current subscriber-to-source
            if (!curr.compareAndSet(r, u)) {
                // didn't work, maybe someone else did it or the current subscriber
                // to source has just finished
                continue;
            }
            // we won, let's use it going onwards
            r = u;
        }

        /*
         * Try adding it to the current subscriber-to-source, add is atomic in respect
         * to other adds and the termination of the subscriber-to-source.
         */
        if (r.add(inner)) {
            if (inner.requested.get() == CANCELLED) {
                r.remove(inner);
            } else {
                inner.parent.set(r);
            }
            r.drain();
            break;
        }
    }
}
 
Example 12
Source File: ResponseHandler.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
@Override
public void subscribe(Subscriber<? super ByteBuffer> subscriber) {
    if (this.subscriber != null) {
        subscriber.onComplete();
        return;
    }
    this.subscriber = subscriber;
    channelContext.channel().attr(ChannelAttributeKey.SUBSCRIBER_KEY)
            .set(subscriber);

    subscriber.onSubscribe(new Subscription() {
        @Override
        public void request(long l) {
            if (running) {
                running = false;
                if (l <= 0) {
                    subscriber.onError(new IllegalArgumentException("Demand must be positive!"));
                } else {
                    subscriber.onNext(fullContent);
                    subscriber.onComplete();
                    executeFuture.complete(null);
                }
            }
        }

        @Override
        public void cancel() {
            running = false;
        }
    });

}
 
Example 13
Source File: MultiRetryWhenOp.java    From smallrye-mutiny with Apache License 2.0 5 votes vote down vote up
private static <T> void subscribe(MultiSubscriber<? super T> downstream,
        Function<? super Multi<Throwable>, ? extends Publisher<?>> triggerStreamFactory,
        Multi<? extends T> upstream) {
    TriggerSubscriber other = new TriggerSubscriber();
    Subscriber<Throwable> signaller = new SerializedSubscriber<>(other.processor);
    signaller.onSubscribe(Subscriptions.empty());
    MultiSubscriber<T> serialized = new SerializedSubscriber<>(downstream);

    RetryWhenOperator<T> operator = new RetryWhenOperator<>(upstream, serialized, signaller);
    other.operator = operator;

    serialized.onSubscribe(operator);
    Publisher<?> publisher;

    try {
        publisher = triggerStreamFactory.apply(other);
        if (publisher == null) {
            throw new NullPointerException("The stream factory returned `null`");
        }
    } catch (Throwable e) {
        downstream.onFailure(e);
        return;
    }

    publisher.subscribe(other);

    if (!operator.isCancelled()) {
        upstream.subscribe(operator);
    }
}
 
Example 14
Source File: RxBusUtil.java    From RxBus2 with Apache License 2.0 5 votes vote down vote up
public static <T> Subscriber<T> wrapSubscriber(Subscriber<T> subscriber, IRxBusQueue isResumedProvider)
{
    return new Subscriber<T>()
    {
        @Override
        public void onSubscribe(Subscription s) {
            subscriber.onSubscribe(s);
        }

        @Override
        public void onComplete()
        {
            subscriber.onComplete();
        }

        @Override
        public void onError(Throwable e)
        {
            subscriber.onError(e);
        }


        @Override
        public void onNext(T t)
        {
            if (RxUtil.safetyQueueCheck(t, isResumedProvider))
                subscriber.onNext(t);
        }
    };
}
 
Example 15
Source File: FlowableRangeLong.java    From RxJava3-preview with Apache License 2.0 5 votes vote down vote up
@Override
public void subscribeActual(Subscriber<? super Long> s) {
    if (s instanceof ConditionalSubscriber) {
        s.onSubscribe(new RangeConditionalSubscription(
                (ConditionalSubscriber<? super Long>)s, start, end));
    } else {
        s.onSubscribe(new RangeSubscription(s, start, end));
    }
}
 
Example 16
Source File: JmsSource.java    From smallrye-reactive-messaging with Apache License 2.0 5 votes vote down vote up
@Override
public void subscribe(Subscriber<? super Message> s) {
    if (downstream.compareAndSet(null, s)) {
        s.onSubscribe(this);
    } else {
        Subscriptions.fail(s, ex.illegalStateAlreadySubscriber());
    }
}
 
Example 17
Source File: Subscriptions.java    From smallrye-mutiny with Apache License 2.0 5 votes vote down vote up
public static void fail(Subscriber<?> subscriber, Throwable failure, Publisher<?> upstream) {
    ParameterValidation.nonNull(subscriber, "subscriber");
    ParameterValidation.nonNull(failure, "failure");
    if (upstream != null) {
        upstream.subscribe(new CancelledSubscriber<>());
    }

    subscriber.onSubscribe(empty());
    subscriber.onError(failure);
}
 
Example 18
Source File: EmptyPublisher.java    From aws-sdk-java-v2 with Apache License 2.0 4 votes vote down vote up
@Override
public void subscribe(Subscriber<? super ByteBuffer> subscriber) {
    subscriber.onSubscribe(new EmptySubscription(subscriber));
}
 
Example 19
Source File: ReactorDemo.java    From reactive-streams-in-java with Apache License 2.0 4 votes vote down vote up
@Override
public void subscribe(Subscriber<? super String> subscriber) {
    subscriber.onSubscribe(
            new FilePublisherSubscription(this, subscriber));
}
 
Example 20
Source File: TranscribeStreamingIntegrationTest.java    From aws-sdk-java-v2 with Apache License 2.0 4 votes vote down vote up
@Override
public void subscribe(Subscriber<? super AudioStream> s) {
    s.onSubscribe(new TestSubscription(s, inputStream));
}