daniel
daniel

Reputation: 966

Why is withRegion() of AmazonSNSClientBuilder not visible?

I am writing code to create an Amazon Web Services SNS client in Eclipse, when I get an error saying

The method withRegion(Region) from the type AwsClientBuilder is not visible

Here is my code

package com.amazonaws.samples;

import java.util.Date;

import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.AnonymousAWSCredentials;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.client.builder.AwsClientBuilder;
import com.amazonaws.regions.Region;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.sns.AmazonSNS;
import com.amazonaws.services.sns.AmazonSNSClient;
import com.amazonaws.services.sns.AmazonSNSClientBuilder;
import com.amazonaws.services.sns.model.CreateTopicRequest;
import com.amazonaws.services.sns.model.CreateTopicResult;
import com.amazonaws.services.sns.model.PublishRequest;

// Example SNS Sender
public class Main {

    // AWS credentials -- replace with your credentials
    static String ACCESS_KEY = "<Your AWS Access Key>";
    static String SECRET_KEY = "<Your AWS Secret Key>";

    // Sender loop
    public static void main(String... args) throws Exception {

        // Create a client

        AWSCredentials awsCred = new AnonymousAWSCredentials();

        AWSStaticCredentialsProvider cred = new AWSStaticCredentialsProvider(awsCred);

        Region region = Region.getRegion(Regions.US_EAST_1);

        AmazonSNS service = AmazonSNSClientBuilder.standard().withRegion(region).withCredentials(cred).build(); // Error message: The method withRegion(Region) from the type AwsClientBuilder<AmazonSNSClientBuilder,AmazonSNS> is not visible

        // Create a topic
        CreateTopicRequest createReq = new CreateTopicRequest()
            .withName("MyTopic3");
        CreateTopicResult createRes = service.createTopic(createReq);

        for (;;) {

            // Publish to a topic
            PublishRequest publishReq = new PublishRequest()
                .withTopicArn(createRes.getTopicArn())
                .withMessage("Example notification sent at " + new Date());
            service.publish(publishReq);

            Thread.sleep(1000);
        }
    }
}

In the screenshot it shows where the error occurs with the red underline in dotted line:

Eclipse for Java withRegion() underlined in red to show error

What should I check to correct this?

Upvotes: 0

Views: 1038

Answers (1)

burm87
burm87

Reputation: 848

You are passing the wrong parameter, withRegion takes either a String or a Regions (note, not Region, singular).

Try passing Regions.EU_WEST_1.

Both AmazonSNSClientBuilder.standard().withRegion(Regions.EU_WEST_1).build();

and AmazonSNSClientBuilder.standard().withRegion("eu-west-1").build();

are working fine for me.

Upvotes: 2

Related Questions