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

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

Introduction

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

Prototype

AmazonSQSClient(AwsSyncClientParams clientParams) 

Source Link

Document

Constructs a new client to invoke service methods on Amazon SQS using the specified parameters.

Usage

From source file:net.smartcosmos.plugin.service.aws.queue.AwsQueueService.java

License:Apache License

@Override
public void create(String queueName) {
    AmazonSQS sqs = new AmazonSQSClient(credentials);
    Region region = assignRegion(sqs);

    try {/* ww w  .j  a  v a 2s  . co  m*/
        CreateQueueRequest createQueueRequest = new CreateQueueRequest().withQueueName(queueName);
        String assignedUrl = sqs.createQueue(createQueueRequest).getQueueUrl();
        LOG.debug("Assigned URL for queue named {} in region {}: {}",
                new Object[] { queueName, region.getName(), assignedUrl });

        onlineFlag = true;
    } catch (AmazonClientException e) {
        if (e.getCause() != null && e.getCause().getClass() == UnknownHostException.class) {
            // Queue most definitely does not exist!
            LOG.error("AWS host is unreachable: {}", new Object[] { e.getCause().getMessage() });
        } else {
            throw e;
        }
    }
}

From source file:net.smartcosmos.plugin.service.aws.queue.AwsQueueService.java

License:Apache License

@Override
public boolean exists(String queueName) {
    boolean existsFlag = false;

    AmazonSQS sqs = new AmazonSQSClient(credentials);
    Region region = assignRegion(sqs);

    try {//from   ww  w. j  ava 2s. c o  m
        sqs.getQueueUrl(queueName);
        existsFlag = true;
        onlineFlag = true;

        LOG.info("Queue named {} in region {} exists", new Object[] { queueName, region.getName() });
    } catch (QueueDoesNotExistException e) {
        // Queue most definitely does not exist!
        LOG.info("Queue named {} in region {} does not exist", new Object[] { queueName, region.getName() });
    } catch (AmazonClientException e) {
        if (e.getCause() != null && e.getCause().getClass() == UnknownHostException.class) {
            // Queue most definitely does not exist!
            LOG.error("AWS host is unreachable: {}", new Object[] { e.getCause().getMessage() });
        } else {
            throw e;
        }
    }

    return existsFlag;
}

From source file:net.smartcosmos.plugin.service.aws.queue.AwsQueueService.java

License:Apache License

@Override
public boolean isHealthy() {
    try {//from  w w w.j a  v  a  2s.  c  o m
        AmazonSQS sqs = new AmazonSQSClient(credentials);
        assignRegion(sqs);
        return true;
    } catch (Exception e) {
        return false;
    }
}

From source file:org.apache.camel.component.aws.sqs.SqsEndpoint.java

License:Apache License

/**
 * Provide the possibility to override this method for an mock implementation
 * @return AmazonSQSClient//w  w  w. j ava 2  s .c  o  m
 */
AmazonSQS createClient() {
    AWSCredentials credentials = new BasicAWSCredentials(configuration.getAccessKey(),
            configuration.getSecretKey());
    AmazonSQS client = new AmazonSQSClient(credentials);
    if (configuration.getAmazonSQSEndpoint() != null) {
        client.setEndpoint(configuration.getAmazonSQSEndpoint());
    }
    return client;
}

From source file:org.apache.usergrid.apm.service.ServiceFactory.java

License:Apache License

public static AmazonSQSClient getSQSClient() {
    if (sqsClient == null) {
        DeploymentConfig config = DeploymentConfig.geDeploymentConfig();
        AWSCredentials awsCredentials = new BasicAWSCredentials(config.getAccessKey(), config.getSecretKey());
        sqsClient = new AmazonSQSClient(awsCredentials);
    }/*from w  w w .  ja v a 2s  .  c o m*/

    return sqsClient;
}

From source file:org.finra.dm.dao.impl.SqsOperationsImpl.java

License:Apache License

@Override
public void sendSqsTextMessage(ClientConfiguration clientConfiguration, String queueName, String messageText) {
    try {/* w w  w.jav  a  2  s .  c  o m*/
        AmazonSQSClient amazonSQSClient = new AmazonSQSClient(clientConfiguration);
        GetQueueUrlResult queueUrlResult = amazonSQSClient.getQueueUrl(queueName);
        amazonSQSClient.sendMessage(queueUrlResult.getQueueUrl(), messageText);
    } catch (QueueDoesNotExistException ex) {
        throw new IllegalStateException(String.format("AWS SQS queue with \"%s\" name not found.", queueName),
                ex);
    }
}

From source file:org.freeeed.aws.SimpleQueueServiceSample.java

License:Apache License

public static void main(String[] args) throws Exception {

    /*//from  w  w  w  .  ja  v a 2 s  .c  om
     * The ProfileCredentialsProvider will return your [default]
     * credential profile by reading from the credentials file located at
     * (~/.aws/credentials).
     */
    AWSCredentials credentials = null;
    try {
        credentials = new ProfileCredentialsProvider().getCredentials();
    } catch (Exception e) {
        throw new AmazonClientException("Cannot load the credentials from the credential profiles file. "
                + "Please make sure that your credentials file is at the correct "
                + "location (~/.aws/credentials), and is in valid format.", e);
    }

    AmazonSQS sqs = new AmazonSQSClient(credentials);
    Region usWest2 = Region.getRegion(Regions.US_WEST_2);
    sqs.setRegion(usWest2);

    System.out.println("===========================================");
    System.out.println("Getting Started with Amazon SQS");
    System.out.println("===========================================\n");

    try {
        // Create a queue
        System.out.println("Creating a new SQS queue called MyQueue.\n");
        CreateQueueRequest createQueueRequest = new CreateQueueRequest("MyQueue");
        String myQueueUrl = sqs.createQueue(createQueueRequest).getQueueUrl();

        // List queues
        System.out.println("Listing all queues in your account.\n");
        // TODO restore
        System.out.println();

        // Send a message
        System.out.println("Sending a message to MyQueue.\n");
        sqs.sendMessage(new SendMessageRequest(myQueueUrl, "This is my message text."));

        // Receive messages
        System.out.println("Receiving messages from MyQueue.\n");
        ReceiveMessageRequest receiveMessageRequest = new ReceiveMessageRequest(myQueueUrl);
        List<Message> messages = sqs.receiveMessage(receiveMessageRequest).getMessages();
        for (Message message : messages) {
            System.out.println(message.toString());
        }
        System.out.println();

        // Delete a message
        System.out.println("Deleting a message.\n");
        String messageReceiptHandle = messages.get(0).getReceiptHandle();
        sqs.deleteMessage(new DeleteMessageRequest(myQueueUrl, messageReceiptHandle));

        // Delete a queue
        System.out.println("Deleting the test queue.\n");
        sqs.deleteQueue(new DeleteQueueRequest(myQueueUrl));
    } catch (AmazonServiceException ase) {
        System.out.println("Caught an AmazonServiceException, which means your request made it "
                + "to Amazon SQS, but was rejected with an error response for some reason.");
        System.out.println("Error Message:    " + ase.getMessage());
        System.out.println("HTTP Status Code: " + ase.getStatusCode());
        System.out.println("AWS Error Code:   " + ase.getErrorCode());
        System.out.println("Error Type:       " + ase.getErrorType());
        System.out.println("Request ID:       " + ase.getRequestId());
    } catch (AmazonClientException ace) {
        System.out.println("Caught an AmazonClientException, which means the client encountered "
                + "a serious internal problem while trying to communicate with SQS, such as not "
                + "being able to access the network.");
        System.out.println("Error Message: " + ace.getMessage());
    }
}

From source file:org.hashbang.util.AutoDiscoverQueue.java

License:Open Source License

private static AmazonSQS init() {

    /*//from  w  ww. j a va  2  s . c o m
     * The ProfileCredentialsProvider will return your [default]
     * credential profile by reading from the credentials file located at
     * (~/.aws/credentials).
     */

    AWSCredentials credentials = null;
    try {
        credentials = new ProfileCredentialsProvider().getCredentials();
    } catch (Exception e) {
        throw new AmazonClientException("Cannot load the credentials from the credential profiles file. "
                + "Please make sure that your credentials file is at the correct "
                + "location (~/.aws/credentials), and is in valid format.", e);
    }

    AmazonSQS sqs = new AmazonSQSClient(credentials);
    Region usWest2 = Region.getRegion(Regions.US_WEST_2);
    sqs.setRegion(usWest2);
    return sqs;
}

From source file:org.juneja.eventdemo.utils.AmazonSimpleQueueService.java

License:Open Source License

public static void main(String[] args) throws Exception {

    /*//w w  w .  j av a2  s .  c om
     * The ProfileCredentialsProvider will return your [default]
     * credential profile by reading from the credentials file located at
     * (~/.aws/credentials).
     */
    AWSCredentials credentials = null;
    try {
        credentials = new ProfileCredentialsProvider().getCredentials();
    } catch (Exception e) {
        throw new AmazonClientException("Cannot load the credentials from the credential profiles file. "
                + "Please make sure that your credentials file is at the correct "
                + "location (~/.aws/credentials), and is in valid format.", e);
    }

    AmazonSQS sqs = new AmazonSQSClient(credentials);
    Region usWest2 = Region.getRegion(Regions.DEFAULT_REGION);
    sqs.setRegion(usWest2);

    System.out.println("===========================================");
    System.out.println("Getting Started with Amazon SQS");
    System.out.println("===========================================\n");

    try {
        // Create a queue
        // System.out.println("Creating a new SQS queue called MyQueue.\n");
        // CreateQueueRequest createQueueRequest = new CreateQueueRequest("TestQueue_EventDriven_2");
        // String myQueueUrl = sqs.createQueue(createQueueRequest).getQueueUrl();

        GetQueueUrlResult queueResult = sqs.getQueueUrl("TestQueue_EventDriven_2");
        String myQueueUrl = queueResult.getQueueUrl();

        // List queues
        System.out.println("Listing all queues in your account.\n");
        for (String queueUrl : sqs.listQueues().getQueueUrls()) {
            System.out.println("  QueueUrl: " + queueUrl);
        }
        System.out.println();

        // Send a message
        //System.out.println("Sending a message to MyQueue.\n");
        //sqs.sendMessage(new SendMessageRequest(myQueueUrl, "This is my new text."));

        // Receive messages
        System.out.println("Receiving messages from MyQueue.\n");
        ReceiveMessageRequest receiveMessageRequest = new ReceiveMessageRequest(myQueueUrl);
        List<Message> messages = sqs.receiveMessage(receiveMessageRequest).getMessages();

        System.out.println("Number of messages : " + messages.size());

        for (Message message : messages) {
            System.out.println("  Message");
            System.out.println("    MessageId:     " + message.getMessageId());
            System.out.println("    ReceiptHandle: " + message.getReceiptHandle());
            System.out.println("    MD5OfBody:     " + message.getMD5OfBody());
            System.out.println("    Body:          " + message.getBody());
            for (Entry<String, String> entry : message.getAttributes().entrySet()) {
                System.out.println("  Attribute");
                System.out.println("    Name:  " + entry.getKey());
                System.out.println("    Value: " + entry.getValue());
            }
        }
        System.out.println();

        // Delete a message
        //System.out.println("Deleting a message.\n");
        //String messageRecieptHandle = messages.get(0).getReceiptHandle();
        //sqs.deleteMessage(new DeleteMessageRequest(myQueueUrl, messageRecieptHandle));

        // Delete a queue
        /**
        System.out.println("Deleting the test queue.\n");
        sqs.deleteQueue(new DeleteQueueRequest(myQueueUrl));
        **/
    } catch (AmazonServiceException ase) {
        System.out.println("Caught an AmazonServiceException, which means your request made it "
                + "to Amazon SQS, but was rejected with an error response for some reason.");
        System.out.println("Error Message:    " + ase.getMessage());
        System.out.println("HTTP Status Code: " + ase.getStatusCode());
        System.out.println("AWS Error Code:   " + ase.getErrorCode());
        System.out.println("Error Type:       " + ase.getErrorType());
        System.out.println("Request ID:       " + ase.getRequestId());
    } catch (AmazonClientException ace) {
        System.out.println("Caught an AmazonClientException, which means the client encountered "
                + "a serious internal problem while trying to communicate with SQS, such as not "
                + "being able to access the network.");
        System.out.println("Error Message: " + ace.getMessage());
    }
}

From source file:org.mule.modules.sqs.SQSConnector.java

License:Open Source License

/**
 * @param accessKey AWS access key/*from   w  w w .  ja  v  a2  s.c o  m*/
 * @param secretKey AWS secret key
 * @param queueName The name of the queue to connect to
//     * @param region select a different region for the queue
 * @throws ConnectionException If a connection cannot be made
 */
@Connect
public void connect(@ConnectionKey String accessKey, String secretKey, String queueName)
        throws ConnectionException {
    try {
        msgQueue = new AmazonSQSClient(new BasicAWSCredentials(accessKey, secretKey));

        if (region != null) {
            msgQueue.setEndpoint(region.value());
        }

        CreateQueueRequest createQueueRequest = new CreateQueueRequest(queueName);
        setQueueUrl(msgQueue.createQueue(createQueueRequest).getQueueUrl());
    } catch (Exception e) {
        throw new ConnectionException(ConnectionExceptionCode.UNKNOWN, null, e.getMessage(), e);
    }
}