Example usage for com.amazonaws.services.sqs AmazonSQSClient setRegion

List of usage examples for com.amazonaws.services.sqs AmazonSQSClient setRegion

Introduction

In this page you can find the example usage for com.amazonaws.services.sqs AmazonSQSClient setRegion.

Prototype

@Deprecated
public void setRegion(Region region) throws IllegalArgumentException 

Source Link

Document

An alternative to AmazonWebServiceClient#setEndpoint(String) , sets the regional endpoint for this client's service calls.

Usage

From source file:awslabs.lab31.Lab31.java

License:Open Source License

public static void main(String[] args) {
    try {/*from  ww w  .  j a  va 2s . co  m*/
        // Create an SQS client
        AmazonSQSClient sqsClient = new AmazonSQSClient(new ClasspathPropertiesFileCredentialsProvider());
        sqsClient.setRegion(region);

        // Create an SNS client
        AmazonSNSClient snsClient = new AmazonSNSClient(new ClasspathPropertiesFileCredentialsProvider());
        snsClient.setRegion(region);

        String queueName = "Notifications";
        String topicName = "ClassroomEvent";

        // Creating the queue will fail if we've just deleted it and are recreating it
        // which is a possibility if you're tracking down a code error. If that happens,
        // pause and retry for up to a minute.
        System.out.println("Creating " + queueName + " queue.");

        Boolean retry = true, notified = false;
        // Create a timeout for 60-seconds from now.
        Date timeout = new Date(System.currentTimeMillis() + 60000L);
        String queueUrl = "";

        while (retry) {
            try {
                queueUrl = labCode.createQueue(sqsClient, queueName);
                retry = false;
            } catch (QueueDeletedRecentlyException ex) {
                if (new Date().before(timeout)) {
                    if (!notified) {
                        System.out.println(
                                "The attempt to recreate the queue failed because the queue was deleted too");
                        System.out.println("recently. Waiting and retrying for up to 1 minute.");
                        notified = true;
                    }
                    // Timeout hasn't expired yet, so wait and retry in 5 seconds.
                    System.out.print(".");
                    Thread.sleep(5000);
                } else {
                    System.out.println("Retry timeout expired. Aborting.");
                    throw ex;
                }
            }

        }
        if (notified) {
            System.out.println("Recovered.");
        }

        System.out.println("URL for new queue: " + queueUrl);

        // List SQS queues
        System.out.println("Getting ARN for " + queueName + " queue.");
        String queueArn = labCode.getQueueArn(sqsClient, queueUrl);
        System.out.println("ARN for queue: " + queueArn);

        // Create an SNS topic and get ARN
        System.out.println("Creating " + topicName + " topic.");
        String topicArn = labCode.createTopic(snsClient, topicName);
        System.out.println("New topic ARN: " + topicArn);

        System.out.println("Granting the notification topic permission to post in the queue.");
        optionalLabCode.grantNotificationPermission(sqsClient, queueArn, queueUrl, topicArn);
        System.out.println("Permission granted.");

        // Create an SNS subscription
        System.out.println("Creating SNS subscription.");
        labCode.createSubscription(snsClient, queueArn, topicArn);
        System.out.println("Subscription created.");

        // Publish message to topic
        String messageText = "This is the SNS topic notification body.";
        String messageSubject = "SNSTopicNotification";

        System.out.println("Publishing SNS topic notification.");
        labCode.publishTopicMessage(snsClient, topicArn, messageSubject, messageText);
        System.out.println("Notification published.");

        // Send a message to the "Notifications" queue
        messageText = "This is the message posted to the queue directly.";
        System.out.println("Posting message to queue directly.");
        labCode.postToQueue(sqsClient, queueUrl, messageText);
        System.out.println("Message posted.");

        // Read message from queue
        System.out.println("Reading messages from queue.");

        List<Message> messages = labCode.readMessages(sqsClient, queueUrl);
        // We expect two messages here
        if (messages.size() < 2) {
            // Try to read again and see if we've picked up the missing message(s).
            messages.addAll(labCode.readMessages(sqsClient, queueUrl));
            if (messages.size() < 2) {
                System.out
                        .println(">>WARNING<< We didn't receive the expected number of messages. Investigate.");
            } else {
                System.out.println();
                System.out.println(
                        "============================================================================");
                System.out
                        .println("PROBLEM: ReadMessages() had to be called twice to collect all the messages.");
                System.out.println("         Did you remember to set the MaxNumberOfMessages property in the ");
                System.out.println("         ReceiveMessageRequest object?");
                System.out.println(
                        "============================================================================");
                System.out.println();

            }
        }
        PrintAndRemoveMessagesInResponse(sqsClient, messages, queueUrl);

        // Locate and delete the SNS subscription
        System.out.println("Removing provisioned resources.");
        labCode.deleteSubscriptions(snsClient, topicArn);
        System.out.println("Subscriptions removed.");

        // Delete the SNS Topic
        labCode.deleteTopic(snsClient, topicArn);
        System.out.println("Topic deleted.");
        // Locate the previously created queue and delete
        labCode.deleteQueue(sqsClient, queueUrl);
        System.out.println("Queue deleted.");
    } catch (Exception ex) {
        LabUtility.dumpError(ex);
    }
}

From source file:awslabs.lab41.SolutionCode.java

License:Open Source License

@Override
public Boolean appMode_TestSqsAccess(Region region, BasicSessionCredentials credentials) {
    try {/*from   w  w  w . j av a 2 s  .c o m*/
        AmazonSQSClient sqsClient = new AmazonSQSClient(credentials);
        sqsClient.setRegion(region);
        sqsClient.listQueues(new ListQueuesRequest());
        return true;
    } catch (Exception ex) {
        return false;
    }
}

From source file:com.amazon.sqs.javamessaging.SQSConnectionFactory.java

License:Open Source License

private void configureClient(AmazonSQSClient client) throws JMSException {
    try {/*from w  ww. j a  v  a  2 s.  c  o  m*/
        if (region != null) {
            client.setRegion(region);
        }
        if (endpoint != null) {
            client.setEndpoint(endpoint);
        }
        if (signerRegionOverride != null) {
            client.setSignerRegionOverride(signerRegionOverride);
        }
    } catch (IllegalArgumentException e) {
        throw (JMSException) new JMSException("Bad endpoint configuration: " + e.getMessage()).initCause(e);
    }
}

From source file:com.threewks.thundr.deferred.provider.SqsQueueProvider.java

License:Apache License

public SqsQueueProvider(AWSCredentials credentials, Region region, String deferredSqsQueueName) {
    AmazonSQSClient client = new AmazonSQSClient(credentials);
    client.setRegion(region);
    init(client, deferredSqsQueueName);//from  w  ww .  java  2 s  .  c o m
}

From source file:org.apache.usergrid.persistence.queue.impl.SNSQueueManagerImpl.java

License:Apache License

/**
 * Create the SQS client for the specified settings
 *///from   ww w  .  j  a v  a  2 s  . co  m
private AmazonSQSClient createSQSClient(final Region region) {

    final UsergridAwsCredentialsProvider ugProvider = new UsergridAwsCredentialsProvider();
    final AmazonSQSClient sqs = new AmazonSQSClient(ugProvider.getCredentials(), clientConfiguration);

    sqs.setRegion(region);

    return sqs;
}