2

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.

developer
  • 21
  • 2
  • i can't find `delayItem` in the documentation, what version are you using. The only i can find is `delayElements` https://projectreactor.io/docs/core/release/api/reactor/core/publisher/Flux.html#delayElements-java.time.Duration- – Toerktumlare May 19 '21 at 08:29
  • @Toerktumlare Yes it was delayElements sorry. I have updated – developer May 19 '21 at 11:43

1 Answers1

1

This is expected.

Adding Flux alone will not be enough. You need to add produces = MediaType.TEXT_EVENT_STREAM_VALUE in your GetMapping.

@GetMapping("/path", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
vins
  • 15,030
  • 3
  • 36
  • 47