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

The following examples show how to use reactor.core.publisher.Flux#blockLast() . These examples are extracted from open source projects. 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
/**
 * Notice on which thread everything runs
 */
@Test
public void blockTriggersWork() throws Exception {
	//given
	AtomicBoolean flag = new AtomicBoolean();

	//when
	log.info("About to create Flux");
	final Flux<Integer> work = Flux.fromStream(() -> {
		log.info("Doing hard work");
		flag.set(true);
		return Stream.of(1, 2, 3);
	});
	log.info("Flux was created");
	final Integer result = work.blockLast();
	log.info("Work is done");

	//then
	assertThat(flag).isTrue();
	assertThat(result).isEqualTo(3);
}
 
Example 2
Source Project: reactor-netty   File: HttpClientTest.java    License: Apache License 2.0 6 votes vote down vote up
private ChannelId[] doTestConnectionIdleTime(ConnectionProvider provider) throws Exception {
	disposableServer =
			HttpServer.create()
			          .port(0)
			          .wiretap(true)
			          .handle((req, res) -> res.sendString(Mono.just("hello")))
			          .bindNow();

	Flux<ChannelId> id = createHttpClientForContextWithAddress(provider)
	                       .get()
	                       .uri("/")
	                       .responseConnection((res, conn) -> Mono.just(conn.channel().id())
	                                                              .delayUntil(ch -> conn.inbound().receive()));

	ChannelId id1 = id.blockLast(Duration.ofSeconds(30));
	Thread.sleep(30);
	ChannelId id2 = id.blockLast(Duration.ofSeconds(30));

	assertThat(id1).isNotNull();
	assertThat(id2).isNotNull();

	provider.dispose();
	return new ChannelId[] {id1, id2};
}
 
Example 3
Source Project: reactor-netty   File: HttpClientTest.java    License: Apache License 2.0 6 votes vote down vote up
private ChannelId[] doTestConnectionLifeTime(ConnectionProvider provider) throws Exception {
	disposableServer =
			HttpServer.create()
			          .port(0)
			          .handle((req, resp) ->
			              resp.sendObject(ByteBufFlux.fromString(Mono.delay(Duration.ofMillis(30))
			                                                         .map(Objects::toString))))
			          .wiretap(true)
			          .bindNow();

	Flux<ChannelId> id = createHttpClientForContextWithAddress(provider)
	                       .get()
	                       .uri("/")
	                       .responseConnection((res, conn) -> Mono.just(conn.channel().id())
	                                                              .delayUntil(ch -> conn.inbound().receive()));

	ChannelId id1 = id.blockLast(Duration.ofSeconds(30));
	Thread.sleep(10);
	ChannelId id2 = id.blockLast(Duration.ofSeconds(30));

	assertThat(id1).isNotNull();
	assertThat(id2).isNotNull();

	provider.dispose();
	return new ChannelId[] {id1, id2};
}
 
Example 4
Source Project: milkman   File: BlockingFluxToStringConverter.java    License: MIT License 5 votes vote down vote up
@Override
public String convert(Flux<String> value) {
    StringBuilder b = new StringBuilder();
    value.subscribe(b::append, b::append);
    value.blockLast();
    return b.toString();
}
 
Example 5
public static void main(String[] args) {
    final int maxConcurrency = 1;
    final Map<Integer, RateLimitOperator<String>> limiters = new ConcurrentHashMap<>(maxConcurrency);
    final Map<Integer, AtomicLong> lastIdentify = new ConcurrentHashMap<>(maxConcurrency);

    Flux<Integer> connections = Flux.range(0, maxConcurrency * 8)
            .groupBy(shard -> shard % maxConcurrency)
            .flatMap(group -> group.concatMap(index -> {
                RateLimitOperator<String> limiter = limiters.computeIfAbsent(index % maxConcurrency,
                        k -> new RateLimitOperator<>(1, Duration.ofSeconds(5), Schedulers.parallel()));
                AtomicLong lastIdentifyAt = lastIdentify.computeIfAbsent(index % maxConcurrency,
                        k -> new AtomicLong(0));

                return Flux.just("identify: " + index)
                        .transform(limiter)
                        .doOnNext(it -> {
                            long now = System.nanoTime();
                            if (Duration.ofNanos(lastIdentifyAt.get()).plusSeconds(5).toNanos() > now) {
                                log.warn("Identified too quickly");
                            }
                            log.info(">> {}", it);
                            lastIdentifyAt.set(now);
                        })
                        .then()
                        .thenReturn(index);
            }));

    connections.blockLast();
}
 
Example 6
@Test
void multipleSubscribers() {
    // @formatter:off
    Flux<TopicMessage> generator = Flux.concat(
            domainBuilder.topicMessage(t -> t.topicNum(1).sequenceNumber(1).consensusTimestamp(future.plusNanos(1L))),
            domainBuilder.topicMessage(t -> t.topicNum(1).sequenceNumber(2).consensusTimestamp(future.plusNanos(2L))),
            domainBuilder.topicMessage(t -> t.topicNum(2).sequenceNumber(7).consensusTimestamp(future.plusNanos(3L))),
            domainBuilder.topicMessage(t -> t.topicNum(2).sequenceNumber(8).consensusTimestamp(future.plusNanos(4L))),
            domainBuilder.topicMessage(t -> t.topicNum(1).sequenceNumber(3).consensusTimestamp(future.plusNanos(5L)))
    );
    // @formatter:on

    TopicMessageFilter filter1 = TopicMessageFilter.builder()
            .startTime(Instant.EPOCH)
            .topicNum(1)
            .build();
    TopicMessageFilter filter2 = TopicMessageFilter.builder()
            .startTime(Instant.EPOCH)
            .topicNum(2)
            .build();

    StepVerifier stepVerifier1 = getTopicListener()
            .listen(filter1)
            .map(TopicMessage::getSequenceNumber)
            .as(StepVerifier::create)
            .expectNext(1L, 2L, 3L)
            .thenCancel()
            .verifyLater();

    StepVerifier stepVerifier2 = getTopicListener()
            .listen(filter2)
            .map(TopicMessage::getSequenceNumber)
            .as(StepVerifier::create)
            .expectNext(7L, 8L)
            .thenCancel()
            .verifyLater();

    Uninterruptibles.sleepUninterruptibly(50, TimeUnit.MILLISECONDS);
    generator.blockLast();

    stepVerifier1.verify(Duration.ofMillis(500));
    stepVerifier2.verify(Duration.ofMillis(500));

    getTopicListener()
            .listen(filter1)
            .map(TopicMessage::getSequenceNumber)
            .as(StepVerifier::create)
            .as("Verify can still re-subscribe after poller cancelled when no subscriptions")
            .expectNextCount(0)
            .thenCancel()
            .verify(Duration.ofMillis(100));
}