1

I would like to create a temp file in a reactive way to avoid blocking calls.

Mono.fromCallable {
    Files.createTempFile(null, ".zip")
}
    .flatMap { path: Path ->
        DataBufferUtils.write(
            webclient.get().uri("/large-file.zip").retrieve()
                .bodyToFlux(DataBuffer::class.java),
            path,
            StandardOpenOption.CREATE
        ).then(Mono.just(path))
    }

However, I get warning in IDEA on method createTempFile:

Possibly blocking call in non-blocking context could lead to thread starvation

Is there a way to create such file in truly reactive way?

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
pixel
  • 24,905
  • 36
  • 149
  • 251
  • [C.1. How Do I Wrap a Synchronous, Blocking Call?](https://projectreactor.io/docs/core/release/reference/#faq.wrap-blocking) – K.Nicholas Mar 25 '22 at 14:10
  • @K.Nicholas I do that already. However, I wonder if there is any better API for creating files. – pixel Mar 25 '22 at 14:12
  • Looks different. When I've done the above I don't get a "Possibly blocking call" warning that I remember. This `Mono.fromCallable {Files.createTempFile(null, ".zip")}` doesn't look correct to me. – K.Nicholas Mar 25 '22 at 14:33
  • You could use [BlockHound](https://github.com/reactor/BlockHound) to detect blocking code in tests and then like @K.Nicholas suggested execute on another scheduler is required. – Alex Mar 25 '22 at 14:47
  • all operations in files are IO blocking calls if i remember correctly, placing them in a `fromCallable` does not make them "non-blocking" you need to make sure it gets run in a different thread, you solve that by either calling `subscribeOn` somewhere in the chain, or `publishOn` somewhere before the blocking call. You might be able the use https://docs.oracle.com/javase/7/docs/api/java/nio/channels/AsynchronousFileChannel.html but i have very little knowledge about it – Toerktumlare Mar 25 '22 at 23:37
  • It seems to be not possible to do non blocking I/O with files https://stackoverflow.com/questions/3955250/why-filechannel-in-java-is-not-non-blocking – Olivier Boissé Mar 29 '22 at 13:04

1 Answers1

0

It looks like I need to wait for Loom:

On Linux, we plan to use io_uring for asynchronous file I/O, and in the meantime we’re using the ForkJoinPool.ManagedBlocker mechanism to smooth over blocking file I/O operations by adding more OS threads to the worker pool when a worker is blocked.

(source: https://cr.openjdk.java.net/~rpressler/loom/loom/sol1_part1.html)

pixel
  • 24,905
  • 36
  • 149
  • 251