I would like to asynchronously read messages produced by user from stdin. Something like:
Flux.from(stdinPublisher())
.subscribe(msg -> System.out.println("Received: " + msg));
So how to implement such stdin publisher here?
It was easy. Sorry for disturb :)
import java.util.Scanner;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;
import reactor.core.publisher.Flux;
@Component
@Slf4j
public class StdinProducerExample implements ApplicationRunner {
@Override
public void run(ApplicationArguments args) throws Exception {
Flux
.create(sink -> {
Scanner scanner = new Scanner(System.in);
while (scanner.hasNext()) {
sink.next(scanner.nextLine());
}
})
.subscribeOn(Schedulers.newSingle("stdin publisher"))
.subscribe(m -> log.info("User message: {}", m));
log.info("Started listening stdin");
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With