Martin Blaustein
Martin Blaustein

Reputation: 1163

How can I know if an AmazonS3Client has been shutdown?

Is there a way I can tell if an AmazonS3Client has been shutdown?

Bellow is the scenario I'm trying to achieve to avoid creating new clients every time and to make sure if some other component shutdowns it (by mistake) it wont break the next requests.

 private AmazonS3ClientBuilder createBuilder() {

        return AmazonS3ClientBuilder.standard()
                .withCredentials(InstanceProfileCredentialsProvider.getInstance();)
                .withRegion(clientRegion);
    }


    public synchronized AmazonS3 buildAmazonClient() {
        if (s3Client == null || s3Client.*IS_SHUTDOWN?*)
            s3Client = buildAmazonClient();
        return s3Client;
    }

Upvotes: 4

Views: 947

Answers (1)

Shlomi Uziel
Shlomi Uziel

Reputation: 908

According to Amazon SDK Developer Guide:

Service clients in the SDK are thread-safe and, for best performance, you should treat them as long-lived objects. Each client has its own connection pool resource

and Amazon SDK Reference:

This is an optional method, and callers are not expected to call it, but can if they want to explicitly release any open resources

So although there doesn't seem to be any method to check if it's already shutdown (i.e. the client is unusable any longer to make requests), it seems you could manage it yourself,
but you don't really need to:

Since it's a long lived object you shouldn't create too many instances, and optionally invoke shutdown once you no longer plan on accessing them.

If you really have a use case in which you need to instantiate and kill different instances throughout the lifetime of your application, I'd suggest you keep tabs on your shutdown invocation, so you can tell if it's been shutdown already (although once the resources are freed, there shouldn't be a real need to keep a reference to a shutdown client any longer...)

Upvotes: 1

Related Questions