简体   繁体   English

我如何知道 AmazonS3Client 是否已关闭?

[英]How can I know if an AmazonS3Client has been shutdown?

Is there a way I can tell if an AmazonS3Client has been shutdown?有没有办法判断 AmazonS3Client 是否已关闭?

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. Bellow 是我试图实现的场景,以避免每次都创建新客户端,并确保如果其他组件(错误地)关闭它,它不会中断下一个请求。

 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;
    }

According to Amazon SDK Developer Guide :根据亚马逊 SDK 开发人员指南

Service clients in the SDK are thread-safe and, for best performance, you should treat them as long-lived objects. SDK 中的服务客户端是线程安全的,为了获得最佳性能,您应该将它们视为长期存在的对象。 Each client has its own connection pool resource每个客户端都有自己的连接池资源

and Amazon SDK Reference :亚马逊 SDK 参考

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 (ie 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...)如果您确实有一个用例需要在应用程序的整个生命周期中实例化和终止不同的实例,我建议您密切关注shutdown调用,以便您可以判断它是否已经关闭(尽管一旦资源被释放,应该不再需要保留对关闭客户端的引用......)

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

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