Java Code Examples for reactor.core.publisher.Flux#subscribeWith()

The following examples show how to use reactor.core.publisher.Flux#subscribeWith() . 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: ClientCalls.java    From reactive-grpc with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Implements a stream → unary call as {@link Flux} → {@link Mono}, where the client transits a stream of
 * messages.
 */
@SuppressWarnings("unchecked")
public static <TRequest, TResponse> Mono<TResponse> manyToOne(
        Flux<TRequest> fluxSource,
        Function<StreamObserver<TResponse>, StreamObserver<TRequest>> delegate,
        CallOptions options) {
    try {
        ReactorSubscriberAndClientProducer<TRequest> subscriberAndGRPCProducer =
                fluxSource.subscribeWith(new ReactorSubscriberAndClientProducer<>());
        ReactorClientStreamObserverAndPublisher<TResponse> observerAndPublisher =
                new ReactorClientStreamObserverAndPublisher<>(
                    s -> subscriberAndGRPCProducer.subscribe((CallStreamObserver<TRequest>) s),
                    subscriberAndGRPCProducer::cancel
                );
        delegate.apply(observerAndPublisher);

        return Flux.from(observerAndPublisher)
                   .singleOrEmpty();
    } catch (Throwable throwable) {
        return Mono.error(throwable);
    }
}
 
Example 2
Source File: ClientCalls.java    From reactive-grpc with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Implements a bidirectional stream → stream call as {@link Flux} → {@link Flux}, where both the client
 * and the server independently stream to each other.
 */
@SuppressWarnings("unchecked")
public static <TRequest, TResponse> Flux<TResponse> manyToMany(
        Flux<TRequest> fluxSource,
        Function<StreamObserver<TResponse>, StreamObserver<TRequest>> delegate,
        CallOptions options) {
    try {

        final int prefetch = ReactorCallOptions.getPrefetch(options);
        final int lowTide = ReactorCallOptions.getLowTide(options);

        ReactorSubscriberAndClientProducer<TRequest> subscriberAndGRPCProducer =
            fluxSource.subscribeWith(new ReactorSubscriberAndClientProducer<>());
        ReactorClientStreamObserverAndPublisher<TResponse> observerAndPublisher =
            new ReactorClientStreamObserverAndPublisher<>(
                s -> subscriberAndGRPCProducer.subscribe((CallStreamObserver<TRequest>) s),
                subscriberAndGRPCProducer::cancel, prefetch, lowTide
            );
        delegate.apply(observerAndPublisher);

        return Flux.from(observerAndPublisher);
    } catch (Throwable throwable) {
        return Flux.error(throwable);
    }
}
 
Example 3
Source File: FluxSpecTests.java    From reactor-core with Apache License 2.0 6 votes vote down vote up
@Test
	public void whenUnknownNumberOfValuesReducedEachReductionPassedToConsumerOnWindow() {
//		"When an unknown number of values is being reduced, each reduction is passed to a consumer on window"
//		given: "a composable with a reduce function"
		FluxIdentityProcessor<Integer> source =
				Processors.multicast();
		Flux<Integer> reduced = source.window(2)
		                              .log()
		                              .flatMap(it -> it.log("lol")
		                                               .reduce(new Reduction()));
		MonoProcessor<Integer> value = reduced.subscribeWith(MonoProcessor.create());

//		when: "the first value is accepted"
		source.onNext(1);

//		then: "the reduction is not available"
		assertThat(value.peek()).isNull();

//		when: "the second value is accepted and flushed"
		source.onNext(2);

//		then: "the updated reduction is available"
		assertThat(value.peek()).isEqualTo(2);
	}
 
Example 4
Source File: InfoResource.java    From Hands-On-Reactive-Programming-in-Spring-5 with MIT License 5 votes vote down vote up
@StreamListener
public void listen(
    @Input(MessagesSource.INPUT) Flux<MessageResponse> messages,
 @Input(StatisticSource.INPUT) Flux<UsersStatisticVM> statistic
) {
    messages.map(MessageMapper::toViewModelUnit)
            .subscribeWith(messagesStream);
 statistic.subscribeWith(statisticStream);
}
 
Example 5
Source File: ServerCalls.java    From reactive-grpc with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Implements a unary → stream call as {@link Mono} → {@link Flux}, where the server responds with a
 * stream of messages.
 */
public static <TRequest, TResponse> void oneToMany(
        TRequest request, StreamObserver<TResponse> responseObserver,
        Function<Mono<TRequest>, Flux<TResponse>> delegate) {
    try {
        Mono<TRequest> rxRequest = Mono.just(request);

        Flux<TResponse> rxResponse = Preconditions.checkNotNull(delegate.apply(rxRequest));
        ReactorSubscriberAndServerProducer<TResponse> server = rxResponse.subscribeWith(new ReactorSubscriberAndServerProducer<>());
        server.subscribe((ServerCallStreamObserver<TResponse>) responseObserver);
    } catch (Throwable throwable) {
        responseObserver.onError(prepareError(throwable));
    }
}
 
Example 6
Source File: FluxSpecTests.java    From reactor-core with Apache License 2.0 5 votes vote down vote up
@Test
	public void deferredFluxInitialValueLaterAvailableUpToLongMax() throws InterruptedException {
//		"A deferred Flux with an initial value makes that value available later up to Long.MAX "
//		given: "a composable with an initial value"
		AtomicReference<Throwable> e = new AtomicReference<>();
		CountDownLatch latch = new CountDownLatch(1);
		Flux<Integer> stream = Flux.fromIterable(Arrays.asList(1, 2, 3))
		                           .publish()
		                           .autoConnect()
		                           .doOnError(e::set)
		                           .doOnComplete(latch::countDown);

//		when: "cumulated request of Long MAX"
		long test = Long.MAX_VALUE / 2L;
		AssertSubscriber<Integer> controls =
				stream.subscribeWith(AssertSubscriber.create(0));
		controls.request(test);
		controls.request(test);
		controls.request(1);

		//sleep(2000)

//		then: "no error available"
		latch.await(2, TimeUnit.SECONDS);

		assertThat(e.get()).isNull();
	}