gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 21:21:06 27 4
gpt4 key购买 nike

有什么方法可以判断 AmazonS3Client 是否已关闭?

以下是我试图实现的场景,以避免每次都创建新客户端,并确保如果其他组件关闭它(错误地),它不会破坏下一个请求。

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

最佳答案

根据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

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

因此,虽然似乎没有任何方法可以检查它是否已经关闭(即客户端无法再发出请求),但似乎您可以自己管理它,
但你真的不需要:

由于它是一个长期存在的对象,因此您不应该创建太多实例,并且可以选择在不再计划访问它们时调用关闭。

如果您确实有一个用例,需要在应用程序的整个生命周期中实例化和终止不同的实例,我建议您密切关注您的 shutdown 调用,这样您就可以知道如果它已经关闭(尽管一旦资源被释放,就不再需要保留对关闭客户端的引用......)

关于java - 我如何知道 AmazonS3Client 是否已关闭?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59734968/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com