2017-06-02 85 views
0

我正在使用AWS開發工具包for Java在AWS計量服務中使用。當我試圖用AWSMarketplaceMeteringClientBuilder創建AWSMarketplaceMeteringClient,我發現,如果我用withRegion(Region region)方法,我得到以下編譯時錯誤:AWSMarketplaceMeteringClientBuilder.withRegion()不可見

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

如下圖所示的客戶端代碼:

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

而且當我嘗試使用AWSMarketplaceMeteringClient直接setRegion(Region region)方法,我得到以下運行時錯誤:

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) 

那麼應該如何使用withRegion(Region region)方法?

回答

0

如果我們仔細觀察,AwsClientBuilder類有以下方法:

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

我嘗試使用方法withRegion(區區域),這是在這個基類私有。所以我們應該使用withRegion(Regions region)方法[注意:參數是Regions而不是Region]。使用這種方法解決了我的問題。