I'm trying to upload a file to S3, but JVM says that i have a thread-blocking method calls in code fragments where threads should not be blocked when calling file.readAllBytes(), so is there a way to is there a way to make the method asynchronous with Flux or Mono? or any other way to solve that problem?
private Mono<Boolean> uploadFile(InputStream file, String bucket, String name) {
try {
return uploadAdapter.uploadObject(bucket,name,file.readAllBytes());
} catch (IOException e) {
return Mono.just(false);
}
}
@Override
public Mono<Boolean> uploadObject(String bucketName, String objectKey, byte[] fileContent) {
return Mono.fromFuture(
s3AsyncClient.putObject(configurePutObject(bucketName, objectKey),
AsyncRequestBody.fromBytes(fileContent)))
.map(response -> response.sdkHttpResponse().isSuccessful());
}
Since InputStream
is a synchronous API, you have 2 choices, and that's true about any other synchronous API:
java.nio2
, or functions from the Reactor-Netty library, which has great solutions for this use case.single()
and boundedElastic()
. The difference is that boundedElastic()
limits the number of threads that you can open, so you will end up using your threads as blocking queues, which is safer than single()
.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