简体   繁体   中英

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. All those methods are using a single s3 client object. Based on the Amazon Java SDK documentation I found that the s3Clients are thread safe objects. 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?

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. 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:

  1. The builders in the java AWS S3 sdk are generally not thread safe . So try not to use S3Client#getS3Client() in multi-threaded environment.
  2. AmazonS3Client is annotated with @ThreadSafe . This is an annotation in Java AWS sdk, that marks the class as thread-safe. 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. In the examples above you clearly create new instance per each and every RequestHandler#readObject() method invocation. 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.

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!)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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