I have a large list call List object = [1000+ Records], So I have created one end point in spring boot
@GetMapping("/path")
public Flux<Something> streamAPI(){
List<Something> object = [1000+ Records]
return Flux.fromIterable(object).delayElements(Duration.ofSeconds(1));
}
As a consumer I was calling above API from another application using webclient.
FileWriter jsonFileWriter = new FileWriter("example.json");
return webclient.get().uri(builder -> builder.scheme("http").host("localhost").port("8888")
.path("/path").build()).retrieve().onStatus(HttpStatus::is4xxClientError, response ->
{
throw new NotFoundException("Not Found");
}).onStatus(HttpStatus::is5xxServerError, response -> {
throw new InternalServerError("Internal Server Error");
}).bodyToFlux(Something.class).doOnNext(data -> {
try {
jsonFileWriter.write(data.toString() + "\n");
} catch (IOException e) {
e.printStackTrace();
}
}).doOnComplete(() -> {
try {
jsonFileWriter.flush();
jsonFileWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
})
How but here I was facing issue with the writing data into the file, It was taking some 20 seconds and immediately write 20 records I can not able to see writing data in every seconds. Could any one help.