简体   繁体   English

如何以线程安全的方式实施 AWS S3 客户端?

[英]How I can implement AWS S3 client in a thread safe manner?

Hi I have a method that is executed by multiple threads concurrently to connect to the s3 bucket objects and read metadata.您好我有一个由多个线程同时执行以连接到 s3 存储桶对象并读取元数据的方法。 All those methods are using a single s3 client object.所有这些方法都使用单个 s3 客户端 object。 Based on the Amazon Java SDK documentation I found that the s3Clients are thread safe objects.根据 Amazon Java SDK 文档,我发现 s3Clients 是线程安全对象。 Can the following way of implementation cause any deadlock or performance issue?以下实现方式会导致任何死锁或性能问题吗? Is this the correct way of implementation when using multiple thread with s3 client?在 s3 客户端使用多线程时,这是正确的实现方式吗?

public class S3Client {
    // static method that returns the s3 client for all the requests
    public static AmazonS3 getS3Client(){
        return AmazonS3ClientBuilder.standard().withRegion(Regions.DEFAULT_REGION).build();
    }
}

And there is another class(RequestHandler->readObject method) that will be executed by multiple threads concurrently.还有另一个类(RequestHandler->readObject 方法)将由多个线程同时执行。 Hence will be executed for each and every requests.因此将为每个请求执行。

public class RequestHandler {
    // Multiple concurrent threads are accessing this method
    public void readObject(){
        AmazonS3 s3Client = S3Client.getS3Client();
        ListObjectsV2Result result = s3Client.listObjectsV2("bucket_name");
    }
}

Please advice.请指教。 Thanks in advance!!提前致谢!!

Lets go one by one:让go一一:

  1. The builders in the java AWS S3 sdk are generally not thread safe . java AWS S3 sdk 中的构建器通常不是线程安全的。 So try not to use S3Client#getS3Client() in multi-threaded environment.所以尽量不要在多线程环境中使用S3Client#getS3Client()
  2. AmazonS3Client is annotated with @ThreadSafe . AmazonS3Client使用@ThreadSafe注释。 This is an annotation in Java AWS sdk, that marks the class as thread-safe.这是 Java AWS sdk 中的注释,将 class 标记为线程安全的。 So there is no need to create some sort of object factory like you did, you only can have one AmazonS3Client singleton object per application.所以没有必要像你一样创建某种 object 工厂,每个应用程序只能有一个AmazonS3Client singleton object。 In the examples above you clearly create new instance per each and every RequestHandler#readObject() method invocation.在上面的示例中,您清楚地为每个RequestHandler#readObject()方法调用创建了新实例。 It is not only unsafe, it will likely cause a performance issue, since you will create a lot of AmazonS3Client , which will degrade your java app garbage collection process.这不仅不安全,而且可能会导致性能问题,因为您将创建大量AmazonS3Client ,这将降低您的 java 应用程序垃圾收集过程。

You can solve pretty much all of it if you will just use a singleton pattern, ie create AmazonS3Client as a singleton object, either by spring, or by any other IoC framework, or by yourself, for example via double check locking . You can solve pretty much all of it if you will just use a singleton pattern, ie create AmazonS3Client as a singleton object, either by spring, or by any other IoC framework, or by yourself, for example via double check locking . In this way you will achieve thread safety along with relatively good performance (in comparison to code in the question).通过这种方式,您将实现线程安全以及相对良好的性能(与问题中的代码相比)。

Hope it helped, have a nice day!)希望对您有所帮助,祝您有美好的一天!)

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

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