liveBook · Manning

Interval

As explained in Spring in Action, Sixth Edition

interval is a Project Reactor factory operation that creates a Flux emitting incrementing values at specified time intervals.

interval is a Project Reactor factory method that creates a Flux emitting incrementing values at a specified duration. Unlike range(), it receives an emission interval instead of starting and ending values. section 11.3.1

Emit five values once per second

Flux<Long> intervalFlux = Flux.interval(Duration.ofSeconds(1)).take(5); The sequence emits 0L, 1L, 2L, 3L, and 4L, then completes. Because interval() has no maximum value, take(5) limits the potentially unending sequence. section 11.3.1

@Test
public void createAFlux_interval() {
  Flux<Long> intervalFlux = 
      Flux.interval(Duration.ofSeconds(1))
          .take(5);
  
  StepVerifier.create(intervalFlux)
     .expectNext(0L)
      .expectNext(1L)
      .expectNext(2L)
      .expectNext(3L)
      .expectNext(4L)
      .verifyComplete();
}

View the complete StepVerifier test for a one-second interval producing five incrementing values. section 11.3.1

How Spring in Action, Sixth Edition covers it

How deep it goes
Explains the duration parameter, zero-based increments, unbounded behavior, and take() limiting, but not scheduler internals.
Examples
Uses a one-second Flux.interval(Duration.ofSeconds(1)) sequence verified through five expected Long values.
What you can do after
Apply it via Project Reactor to produce and test a time-spaced incrementing Flux.
How it connects
Connects interval() with Flux, range(), take(), and StepVerifier.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Spring in Action, Sixth Edition. The section links point to the parts of the book it draws on.