我如何知道 AmazonS3Client 是否已关闭?
How can I know if an AmazonS3Client has been shutdown?
有什么方法可以判断 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;
}
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
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
调用,这样您就可以判断是否它已经关闭了(尽管一旦资源被释放,就不再需要保留对关闭客户端的引用...)
有什么方法可以判断 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;
}
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
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
调用,这样您就可以判断是否它已经关闭了(尽管一旦资源被释放,就不再需要保留对关闭客户端的引用...)