I use Spring boot 3.1.x for some shell interaction. I want to stream text from a method to the console.
I've tried something like this:
@ShellMethod(key = "stream")
public Flux<String> streamMe(){
List<String> cities = List.of("London", "Paris", "Rome", "Amsterdam","Boston");
Flux<String> flux = Flux.fromIterable(cities);
return flux.delayElements(Duration.ofSeconds(2));
}
But when calling it from the shell I get the output FluxConcatMapNoPrefetch on screen
Spring Shell expects commands to complete, so it can print the result to the console. However, when you return a Flux that has an ongoing stream of elements, it might not behave as expected in an interactive shell.
One way to print stream of elements to the console is subscribing to the stream and handle the results individually.
@ShellMethod(key = "stream")
public void streamMe() {
List<String> cities = List.of("London", "Paris", "Rome", "Amsterdam", "Boston");
Flux<String> flux = Flux.fromIterable(cities).delayElements(Duration.ofSeconds(2));
flux.subscribe(
System.out::println,
Throwable::printStackTrace,
() -> System.out.println("Stream completed")
);
}