简体   繁体   English

让 AWS S3 客户端在多线程代码中工作

[英]Getting AWS S3 Client to work in multi-threaded code

I am trying to upload to S3 within my asynchronous Java code我正在尝试在我的异步 Java 代码中上传到 S3

private void submitCallablesWithExecutor() throws InterruptedException, ExecutionException, TimeoutException { private void submitCallablesWithExecutor() 抛出 InterruptedException、ExecutionException、TimeoutException {

        ExecutorService executorService = null;

        try {
            executorService = Executors.newCachedThreadPool();

            Future<String> task1Future = executorService.submit(new Callable<String>() {

                public String call() {
                    try {
                        processExportRequest(xmlPutRequest_, customizedRequest_, response_);
                        return "Success";
                    } catch (Exception ex) {
                        return ex.getMessage();
                    }
                }
            });

        } finally {
            executorService.shutdown();

            try {
                if (!executorService.awaitTermination(800, TimeUnit.MILLISECONDS)) {
                    executorService.shutdownNow();
                }
            } catch (InterruptedException e) {
                executorService.shutdownNow();
            }
        }
    }

within processExportRequest I am calling upload to S3.在 processExportRequest 我调用上传到 S3。 I have tried both S3Client and S3AsyncClient.我已经尝试过 S3Client 和 S3AsyncClient。 In both cases, I am getting following error:在这两种情况下,我都收到以下错误:

Failed to upload to S3: java.lang.IllegalStateException: Interrupted waiting to refresh the value.无法上传到 S3:java.lang.IllegalStateException:中断等待刷新值。

I don't see anywhere in my code that's calling Thread.interrupt(), and everything else seems to work fine, just not S3 upload.我在我的代码中没有看到任何调用 Thread.interrupt() 的地方,其他一切似乎都运行良好,只是不是 S3 上传。 Maybe the multithreaded nature of Java Future is not compatible with AWS SDK?也许 Java Future 的多线程特性与 AWS SDK 不兼容? Thanks.谢谢。

I changed Future to CompletableFuture, and combine two of them (in sequence):我将 Future 更改为 CompletableFuture,并将它们中的两个(按顺序)合并:

private CompletableFuture<PutObjectResponse> processExportAndUploadAsync()
            throws IOException {
        CompletableFuture<PutObjectResponse> result = processExportAsync()
                .thenCompose(fileName -> uploadS3Async(fileName));

        return result;
    }

It seems to work.它似乎工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM