liveBook · Manning

Take

As explained in Spring in Action, Sixth Edition

take is a Project Reactor operator that limits the number of elements emitted by a publisher and then completes.

Project Reactor’s take() limits what a Flux emits, either by item count or by elapsed time. It is useful when a stream could otherwise keep producing values, or when a consumer needs only an initial portion. section 11.3.1 section 11.3.3

With a count, take(3) passes the first three items and then completes the resulting Flux. Once the limit is reached, it cancels the upstream subscription. By contrast, skip(3) discards the first three items and passes the later ones. section 11.3.3

@Test
public void take() {
  Flux<String> nationalParkFlux = Flux.just(
      "Yellowstone", "Yosemite", "Grand Canyon", "Zion", "Acadia")
      .take(3);
  
  StepVerifier.create(nationalParkFlux)
      .expectNext("Yellowstone", "Yosemite", "Grand Canyon")
      .verifyComplete();
}

This test takes the first three park names. StepVerifier checks those values and confirms completion. section 11.3.3

With a duration, take() passes items arriving during the specified period after subscription, then completes. The number of items depends on how many arrive in that time. section 11.3.3

@Test
public void takeForAwhile() {
  Flux<String> nationalParkFlux = Flux.just(
      "Yellowstone", "Yosemite", "Grand Canyon", "Zion", "Grand Teton")
      .delayElements(Duration.ofSeconds(1))
      .take(Duration.ofMillis(3500));
  
  StepVerifier.create(nationalParkFlux)
      .expectNext("Yellowstone", "Yosemite", "Grand Canyon")
      .verifyComplete();
}

Here, values are delayed by one second each, and take(Duration.ofMillis(3500)) allows three through before completion. section 11.3.3

@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();
}

Flux.interval() emits an incrementing value every second and can otherwise run indefinitely. take(5) limits it to 0 through 4, as the test verifies. section 11.3.1

A reactive controller can also use take() to limit a response. If a repository returns an Iterable, Flux.fromIterable() converts it to a Flux before take(12) limits the result to at most 12 items. A repository that already returns a Flux can be used directly; the conversion is a limited step toward an end-to-end reactive stack. section 12.1.2

@GetMapping(params="recent")
public Flux<Taco> recentTacos() {
  return Flux.fromIterable(tacoRepo.findAll()).take(12);
}

The controller converts repository results to a Flux and applies the count limit. The shown code does not specify a sort order. section 12.1.2

How Spring in Action, Sixth Edition covers it

How deep it goes
Explains count and duration variants through tests and cancellation behavior, without detailing implementation internals.
Examples
Uses park-name streams, a timed counter, and a taco controller returning at most 12 objects.
What you can do after
Apply it via Project Reactor to limit a Flux and verify its emissions and completion with StepVerifier.
How it connects
Connects take() with Flux, skip(), predicate filtering, and reactive repositories.

Also explained in

Compare how 4 books explain take →

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.