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

The following examples show how to use reactor.core.publisher.Flux#interval() . 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
/**
 * TODO Count how many frames there are approximately per second
 * <p>
 * Hint: use {@link Flux#window(Duration)} and most likely {@link Flux#count()}
 * </p>
 */
@Test
public void countFramesPerSecond() throws Exception {
	//given
	final Flux<Long> frames = Flux.interval(Duration.ofMillis(16));

	//when
	//TODO operator here, add take(4)
	final Flux<Integer> fps = null;

	//then
	fps
			.as(StepVerifier::create)
			.expectNextMatches(x -> x >= 55 && x <= 65)
			.expectNextMatches(x -> x >= 55 && x <= 65)
			.expectNextMatches(x -> x >= 55 && x <= 65)
			.expectNextMatches(x -> x >= 55 && x <= 65)
			.verifyComplete();
}
 
Example 2
@Test
public void pairwise() throws Exception {
	//given
	final Flux<Long> fast = Flux.interval(Duration.ofMillis(200));
	final Flux<Long> slow = Flux.interval(Duration.ofMillis(250));

	//when
	Flux.zip(
			fast, slow
	).subscribe(
			pair -> log.info("Got {}", pair)
	);

	//then
	TimeUnit.SECONDS.sleep(3);
}
 
Example 3
/**
 * TODO Increase sleep at the end. You should see an exception after a while
 */
@Test
public void latest() throws Exception {
	//given
	final Flux<Long> fast = Flux.interval(Duration.ofMillis(100)).delayElements(Duration.ofMillis(1000));
	final Flux<Long> slow = Flux.interval(Duration.ofMillis(250));

	//when
	Flux.combineLatest(
			fast, slow,
			Tuples::of
	).subscribe(
			pair -> log.info("Got {}", pair)
	);

	//then
	TimeUnit.SECONDS.sleep(3);
}
 
Example 4
/**
 * TODO Count how many frames there are approximately per second
 * <p>
 * Hint: use {@link Flux#buffer(Duration)} and most likely {@link Flux#map(Function)}
 * </p>
 */
@Test
public void countFramesPerSecond() throws Exception {
	//given
	final Flux<Long> frames = Flux.interval(Duration.ofMillis(16));

	//when
	//TODO operator here, add take(4)
	final Flux<Integer> fps = null;

	//then
	fps
			.as(StepVerifier::create)
			.expectNextMatches(x -> x >= 55 && x <= 65)
			.expectNextMatches(x -> x >= 55 && x <= 65)
			.expectNextMatches(x -> x >= 55 && x <= 65)
			.expectNextMatches(x -> x >= 55 && x <= 65)
			.verifyComplete();
}
 
Example 5
@GetMapping(value = "/{singer}/comments", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
public Flux<Comment> querySingerComments(@PathVariable String singer) {
    // generate one flux element per second
    Flux<Long> intervalToGenerateComments = Flux.interval(Duration.ofSeconds(1));

    Flux<Comment> comments = Flux.fromStream(Stream.generate(() -> new Comment(composeComment(singer), new Date())));
    return Flux.zip(intervalToGenerateComments, comments)
            .map(fluxTuple -> fluxTuple.getT2());
}
 
Example 6
@Test
public void startStopStreamProcessing() throws Exception {
    Mono<?> startCommand = Mono.delay(Duration.ofSeconds(1));
    Mono<?> stopCommand = Mono.delay(Duration.ofSeconds(3));
    Flux<Long> streamOfData = Flux.interval(Duration.ofMillis(100));

    streamOfData
        .skipUntilOther(startCommand)
        .takeUntilOther(stopCommand)
        .subscribe(System.out::println);

    Thread.sleep(4000);
}
 
Example 7
@Test
public void testImmediateSchedular() throws Exception{
    Flux<Long> fibonacciGenerator = Flux.interval(Duration.ofMillis(10),Schedulers.immediate());
    fibonacciGenerator
            .filter(x -> {
                print("Executing Filter");
                return x < 100;
            })
            .doOnNext(x -> print("Next value is  "+x))
            .doFinally(x -> print("Closing "))
            .subscribe(x -> print("Sub received : "+x));
    Thread.sleep(500);
}
 
Example 8
@Test
public void interval() throws Exception {
	final Flux<Long> frames = Flux.interval(Duration.ofMillis(16));

	frames
			.take(120)
			.subscribe(
					x -> log.info("Got frame {}", x)
			);

	TimeUnit.SECONDS.sleep(3);  //Why ???
}
 
Example 9
Source Project: spring-5-examples   File: HandlerFunctions.java    License: MIT License 5 votes vote down vote up
private Flux<Reservation> dryReservations() {
  final Flux<Long> interval = Flux.interval(Duration.ofSeconds(1));
  final Flux<Reservation> stream = reservations.findAll();

  return Flux.zip(interval, stream)
             .map(Tuple2::getT2);
}
 
Example 10
@Test
public void poorReuse() throws Exception {
	final Flux<Long> input = Flux.interval(ofNanos(1));
	final Flux<Tuple2<Long, Long>> out = countPerSecond(input);
}
 
Example 11
Source Project: tutorials   File: FooReactiveController.java    License: MIT License 4 votes vote down vote up
@GetMapping(produces = MediaType.TEXT_EVENT_STREAM_VALUE, value = "/foos")
public Flux<Foo> getAllFoos2() {
    final Flux<Foo> foosFlux = Flux.fromStream(Stream.generate(() -> new Foo(new Random().nextLong(), randomAlphabetic(6))));
    final Flux<Long> emmitFlux = Flux.interval(Duration.ofSeconds(1));
    return Flux.zip(foosFlux, emmitFlux).map(Tuple2::getT1);
}