Reputation: 109
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!!
Upvotes: 2
Views: 7927
Reputation: 3175
Lets go one by one:
S3Client#getS3Client()
in multi-threaded environment.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, i.e 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!)
Upvotes: 6