【发布时间】:2021-12-27 19:27:23
【问题描述】:
我正在尝试将文件上传到 S3,但 JVM 说我在代码片段中有一个线程阻塞方法调用,在调用 file.readAllBytes() 时线程不应该被阻塞,所以有没有办法有没有办法使方法与 Flux 或 Mono 异步?或任何其他方法来解决这个问题?
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());
}
【问题讨论】:
-
您无法真正从 InputStream 这样的同步 API 转换为异步 API。您可以通过让它在另一个线程中工作来“假装”它是异步的,但这并不能真正使其异步。
标签: java spring asynchronous project-reactor