Reactor – How to Create Flux (Publisher) with Interval

In this tutorial, JavaSampleApproach introduces simple ways to create Publisher (Flux) that emits items every specified Interval of time.

I. Overview

1. Steps to do

Our goal is to create Flux for (data1, data2, data3, …) that emits items every specified Interval of time.

– First, we need to produce a Flux<Long> that is infinite and emits regular ticks from a clock -> use Flux.interval(Duration).
Now we have Flux<Long>: (i1, i2, i3, …)

– Then we have several ways to convert each Long item (i) to each data item (data) that we want to publish:
+ transform i to data -> use map(i -> data).
+ zip i with data together -> use zipWith(publisher,combinator) or zipWithIterable(collection). Then separate Zip(i,data) to get data.

2. Methods’ details
1. Flux#interval

This method creates a new Flux that emits incrementing Long starting with 0 every period.

2. Flux.zipWithIterable

Pairwise combines as Tuple2 elements of the Flux and an Iterable sequence.

To get the content of Tuple2 in Type-safe way:

3. Flux.zipWith

The operator will forward all combinations produced by the combinator from the most recent items emitted by source<T> (that invokes zipWith() method) and source2<T2> (that contains data item to be zipped with source item):

II. Practice

0. Technology

– Java 8
– Maven 3.6.1
– Reactor Core 3.0.4, with the Aluminium release train.

1. Reactor installation in Maven

– First, import the BOM by adding the following to pom.xml:

– Next, add dependency:

2. Code
2.1 Using map()


2.2 Using zipWithIterable()


2.3 Using zipWith()


III. Source Code

Run and check Results:

By grokonez | June 28, 2017.

Last updated on March 13, 2018.

Related Posts

Got Something To Say:

Your email address will not be published. Required fields are marked *