Search code examples
javaamazon-web-servicesaws-sdkaws-java-sdk

AWSMarketplaceMeteringClientBuilder.withRegion() is not visible


I am using AWS SDK for Java to use in AWS Metering service. When I tried to use AWSMarketplaceMeteringClientBuilder to create a AWSMarketplaceMeteringClient, I found that if I use withRegion(Region region) method, I get following compile time error:

The method withRegion(Region) from the type AwsClientBuilder<AWSMarketplaceMeteringClientBuilder,AWSMarketplaceMetering> is not visible

The client code is as shown below:

AWSMarketplaceMeteringClient metClient = (AWSMarketplaceMeteringClient) AWSMarketplaceMeteringClientBuilder
        .standard()
        .withRegion(Regions.getCurrentRegion())
        .withCredentials(InstanceProfileCredentialsProvider.getInstance())
        .build();

And when I try to use the setRegion(Region region) method of AWSMarketplaceMeteringClient directly, I get following runtime error:

Exception in thread "main" java.lang.UnsupportedOperationException: Client is immutable when created with the builder.
    at com.amazonaws.AmazonWebServiceClient.checkMutability(AmazonWebServiceClient.java:854)
    at com.amazonaws.AmazonWebServiceClient.setRegion(AmazonWebServiceClient.java:349)

So how should I use the withRegion(Region region) method?


Solution

  • If we observe closely, AwsClientBuilder class has following methods:

    public final Subclass withRegion(Regions region) { }
    public final Subclass withRegion(String region) { }
    private Subclass withRegion(Region region) { }
    

    I was trying to use method withRegion(Region region), which is private in this base class. So we should use method withRegion(Regions region) [NOTE: The parameter is Regions instead of Region]. Using this method solved my issue.