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

The following examples show how to use reactor.core.publisher.Flux#interval() . 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: R041_Window.java    From reactor-workshop with GNU General Public License v3.0 6 votes vote down vote up
/**
 * 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
Source File: R043_Zip.java    From reactor-workshop with GNU General Public License v3.0 6 votes vote down vote up
@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
Source File: R043_Zip.java    From reactor-workshop with GNU General Public License v3.0 6 votes vote down vote up
/**
 * 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
Source File: R040_Buffer.java    From reactor-workshop with GNU General Public License v3.0 6 votes vote down vote up
/**
 * 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
Source File: NotificationsController.java    From Software-Architecture-with-Spring-5.0 with MIT License 5 votes vote down vote up
@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
Source File: ReactorEssentialsTest.java    From Hands-On-Reactive-Programming-in-Spring-5 with MIT License 5 votes vote down vote up
@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
Source File: SchedulerTest.java    From Hands-On-Reactive-Programming-with-Reactor with MIT License 5 votes vote down vote up
@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
Source File: R040_Buffer.java    From reactor-workshop with GNU General Public License v3.0 5 votes vote down vote up
@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 File: HandlerFunctions.java    From spring-5-examples with 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
Source File: R060_TransformCompose.java    From reactor-workshop with GNU General Public License v3.0 4 votes vote down vote up
@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 File: FooReactiveController.java    From tutorials with 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);
}