Client Constructors Now Deprecated in the AWS SDK for Java
Historically, you’ve been able to create a service client as shown here.
AmazonSNS sns = new AmazonSNSClient();
This mechanism is now deprecated in favor of using one of the builders to create the client as shown here.
AmazonSNS sns = AmazonSNSClient.builder().build();
The client builders (described in detail in this post) are superior to the basic constructors in the following ways.
Clients created via the builder are immutable. The region/endpoint (and other data) can’t be changed. Therefore, clients are safe to reuse across multiple threads.
At build time, the AWS SDK for Java can validate that a client has all the required information to function correctly – namely, a region. A client created via the builders must have a region that is defined either explicitly (i.e. by calling
withRegion) or as part of the DefaultAwsRegionProviderChain. If the builder can’t determine the region for a client, an SdkClientException is thrown. Region is an important concept when communicating with services in AWS. It not only determines where your request will go, but also how it is signed. Requiring a region means the SDK can behave predictably without depending on hidden defaults.
Using the builder allows a client to be constructed in a single statement using method chaining.
AmazonSNS sns = AmazonSNSClient.builder() .withRegion("us-west-1") .withClientConfiguration(cfg) .withCredentials(creds) .build();
The deprecated constructors are no longer created for new service clients. They will be removed from existing clients in a future major version bump (although they’ll remain in all future releases of the 1.x family of the AWS SDK for Java).