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

The following examples show how to use reactor.core.publisher.Flux#blockLast() . 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: R021_FluxSubscribing.java    From reactor-workshop with GNU General Public License v3.0 7 votes vote down vote up
/**
 * 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 File: HttpClientTest.java    From reactor-netty with 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 File: HttpClientTest.java    From reactor-netty with 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 File: BlockingFluxToStringConverter.java    From milkman with 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
Source File: ExamplePayloadTransformer.java    From Discord4J with GNU Lesser General Public License v3.0 5 votes vote down vote up
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
Source File: AbstractTopicListenerTest.java    From hedera-mirror-node with Apache License 2.0 4 votes vote down vote up
@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));
}