Skip to content

Instantly share code, notes, and snippets.

@ahndmal
Created May 7, 2022 10:54
Show Gist options
  • Save ahndmal/84df48a0f7fd37e328bf29742ef02c65 to your computer and use it in GitHub Desktop.
Save ahndmal/84df48a0f7fd37e328bf29742ef02c65 to your computer and use it in GitHub Desktop.
public abstract class IntervalMessageProducer {
public static Flux<String> produce(int c) {
return produce().take(c);
}
public static Flux<String> produce() {
return doProduceCountAndStrings().map(CountAndString::message);
}
private static Flux<CountAndString> doProduceCountAndStrings() {
var counter = new AtomicLong();
return Flux //
.interval(Duration.ofSeconds(1)) // <1>
.map(i -> new CountAndString(counter.incrementAndGet())); //
}
}
record CountAndString(String message, long count) {
CountAndString(long count) {
this("# " + count, count);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment