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:org.nuxeo.aws.elastictranscoder.GenericAWSClient.java

License:Open Source License

public AmazonSQS getSQSClient() {

    if (sqs == null) {
        sqs = new AmazonSQSClient(awsCredentialsProvider);
    }
    return sqs;
}

From source file:org.springframework.integration.aws.sqs.core.AmazonSQSOperationsImpl.java

License:Apache License

/**
 * The default constructor//from w w  w .  j ava  2s  . c om
 * @param credentials
 */
public AmazonSQSOperationsImpl(final AmazonWSCredentials credentials) {
    if (credentials == null)
        throw new AmazonWSOperationException(null,
                "Credentials cannot be null, provide a non null valid set of credentials");

    this.credentials = credentials;
    clientFactory = new AbstractAmazonWSClientFactory<AmazonSQSClient>() {

        @Override
        protected AmazonSQSClient getClientImplementation() {
            return new AmazonSQSClient(
                    new BasicAWSCredentials(credentials.getAccessKey(), credentials.getSecretKey()));
        }
    };
}

From source file:org.transitime.maintenance.AwsGlacier.java

License:Open Source License

/********************** Member Functions **************************/

public AwsGlacier(String region) {
    // Get credentials from credentials file, environment variable, or 
    // Java property. 
    // See http://docs.aws.amazon.com/AWSJavaSDK/latest/javadoc/com/amazonaws/auth/DefaultAWSCredentialsProviderChain.html
    AWSCredentialsProviderChain credentialsProvider = new DefaultAWSCredentialsProviderChain();
    credentials = credentialsProvider.getCredentials();
    logger.debug("Read in credentials AWSAccessKeyId={} AWSSecretKey={}...", credentials.getAWSAccessKeyId(),
            credentials.getAWSSecretKey().substring(0, 4));

    // Create the glacier client and set to specified region.
    glacierClient = new AmazonGlacierClient(credentials);
    glacierClient.setEndpoint("https://glacier." + region + ".amazonaws.com");

    // Set up params needed for retrieving vault inventory
    sqsClient = new AmazonSQSClient(credentials);
    sqsClient.setEndpoint("https://sqs." + region + ".amazonaws.com");
    snsClient = new AmazonSNSClient(credentials);
    snsClient.setEndpoint("https://sns." + region + ".amazonaws.com");

    // Create the ArchiveTransferManager used for uploading and 
    // downloading files. Need to use ArchiveTransferManager constructor 
    // that allows one to specify sqsClient & snsClient so that they have
    // the proper region. If use ArchiveTransferManager without specifying
    // sqs and sns clients then default ones are constructed, but these
    // use the default Virginia region, which is wrong.
    atm = new ArchiveTransferManager(glacierClient, sqsClient, snsClient);
}

From source file:org.transitime.maintenance.AwsGlacierInventoryRetriever.java

License:Open Source License

/********************** Member Functions **************************/

public AwsGlacierInventoryRetriever(String region) {
    // Get credentials from credentials file, environment variable, or 
    // Java property. 
    // See http://docs.aws.amazon.com/AWSJavaSDK/latest/javadoc/com/amazonaws/auth/DefaultAWSCredentialsProviderChain.html
    AWSCredentialsProviderChain credentialsProvider = new DefaultAWSCredentialsProviderChain();
    AWSCredentials credentials = credentialsProvider.getCredentials();
    logger.debug("Read in credentials AWSAccessKeyId={} AWSSecretKey={}...", credentials.getAWSAccessKeyId(),
            credentials.getAWSSecretKey().substring(0, 4));

    // Create the glacier client and set to specified region.
    glacierClient = new AmazonGlacierClient(credentials);
    glacierClient.setEndpoint("https://glacier." + region + ".amazonaws.com");

    // Set up params needed for retrieving vault inventory
    sqsClient = new AmazonSQSClient(credentials);
    sqsClient.setEndpoint("https://sqs." + region + ".amazonaws.com");
    snsClient = new AmazonSNSClient(credentials);
    snsClient.setEndpoint("https://sns." + region + ".amazonaws.com");
    setupSQS();//from   www  .j a  v  a2 s.c om
    setupSNS();
}

From source file:org.wso2.carbon.inbound.amazonsqs.AmazonSQSPollingConsumer.java

License:Open Source License

/**
 * Create connection with broker and retrieve the messages. Then inject
 * according to the registered handler.//from  w w  w . j  a  v a 2s. c  o m
 */
public Message poll() {
    if (logger.isDebugEnabled()) {
        logger.debug("Polling AmazonSQS messages for " + name);
    }
    try {
        if (!isConnected) {
            sqsClient = new AmazonSQSClient(this.credentials);
            isConnected = true;
        }
        if (sqsClient == null) {
            logger.error("AmazonSQS Inbound endpoint " + name + " unable to get a connection.");
            isConnected = false;
            return null;
        }
        List<Message> messages;
        receiveMessageRequest.setMessageAttributeNames(attributeNames);
        messages = sqsClient.receiveMessage(receiveMessageRequest).getMessages();
        if (!messages.isEmpty()) {
            for (Message message : messages) {
                boolean commitOrRollbacked;
                if (logger.isDebugEnabled()) {
                    logger.debug(
                            "Injecting AmazonSQS message to the sequence : " + injectingSeq + " of " + name);
                }
                //Get the content type of the message.
                if (message.getMessageAttributes().containsKey(AmazonSQSConstants.CONTENT_TYPE)) {
                    contentType = message.getMessageAttributes().get(AmazonSQSConstants.CONTENT_TYPE)
                            .getStringValue();
                    if (contentType.trim().equals("") || contentType.equals("null")) {
                        contentType = AmazonSQSConstants.DEFAULT_CONTENT_TYPE;
                    }
                } else {
                    contentType = properties.getProperty(AmazonSQSConstants.CONTENT_TYPE);
                }
                if (logger.isDebugEnabled()) {
                    logger.debug("Loading the Content-type : " + contentType + " for " + name);
                }
                commitOrRollbacked = injectMessage(message.getBody(), contentType);
                if (commitOrRollbacked) {
                    messageReceiptHandle = message.getReceiptHandle();
                    sqsClient.deleteMessage(new DeleteMessageRequest(destination, messageReceiptHandle));
                }
            }
        } else {
            return null;
        }
    } catch (AmazonServiceException e) {
        throw new SynapseException("Caught an AmazonServiceException, which means your "
                + "request made it to Amazon SQS, but was rejected with an" + "error response for some reason.",
                e);
    } catch (AmazonClientException e) {
        throw new SynapseException("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.", e);
    }
    return null;
}

From source file:pa3.RemoteClientSQS.java

License:Open Source License

/**
 * The only information needed to create a client are security credentials
 * consisting of the AWS Access Key ID and Secret Access Key. All other
 * configuration, such as the service endpoints, are performed
 * automatically. Client parameters, such as proxies, can be specified in an
 * optional ClientConfiguration object when constructing a client.
 *
 * @see com.amazonaws.auth.BasicAWSCredentials
 * @see com.amazonaws.auth.ProfilesConfigFile
 * @see com.amazonaws.ClientConfiguration
 *///  w  w w . ja  v a  2  s .  c om
private static void initSQSandDynamoDB() throws Exception {
    /*
     * The ProfileCredentialsProvider will return your [default] credential
     * profile by reading from the credentials file located at
     * (C:\\Users\\HP\\.aws\\credentials).
     */
    AWSCredentials credentials = null;
    try {
        credentials = new ProfileCredentialsProvider("default").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 (C:\\Users\\HP\\.aws\\credentials), and is in valid format.", e);
    }
    dynamoDB = new AmazonDynamoDBClient(credentials);
    sqs = new AmazonSQSClient(credentials);
    Region usWest2 = Region.getRegion(Regions.US_WEST_2);
    dynamoDB.setRegion(usWest2);
    sqs.setRegion(usWest2);
}

From source file:pa3.RemoteWorkerSQS.java

License:Open Source License

/**
  * The only information needed to create a client are security credentials
  * consisting of the AWS Access Key ID and Secret Access Key. All other
  * configuration, such as the service endpoints, are performed
  * automatically. Client parameters, such as proxies, can be specified in an
  * optional ClientConfiguration object when constructing a client.
  */*from   w ww  .ja va  2 s  . c  om*/
  * @see com.amazonaws.auth.BasicAWSCredentials
  * @see com.amazonaws.auth.ProfilesConfigFile
  * @see com.amazonaws.ClientConfiguration
  */

private static void initSQSandDynamoDB() throws Exception {
    /*
     * The ProfileCredentialsProvider will return your [default] credential
     * profile by reading from the credentials file located at
     * (C:\\Users\\HP\\.aws\\credentials).
     */
    AWSCredentials credentials = null;
    try {
        credentials = new ProfileCredentialsProvider("default").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 (C:\\Users\\HP\\.aws\\credentials), and is in valid format.", e);
    }
    dynamoDB = new AmazonDynamoDBClient(credentials);
    sqs = new AmazonSQSClient(credentials);
    Region usWest2 = Region.getRegion(Regions.US_WEST_2);
    dynamoDB.setRegion(usWest2);
    sqs.setRegion(usWest2);
}

From source file:Reference.SimpleQueueServiceSample.java

License:Open Source License

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

    /*//from www  . j  av a  2  s .co m
     * The ProfileCredentialsProvider will return your [default]
     * credential profile by reading from the credentials file located at
     * ().
     */
    AWSCredentials credentials = null;
    try {
        credentials = new ProfileCredentialsProvider("default").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 (/Users/daniel/.aws/credentials), and is in valid format.", e);
    }

    AmazonSQS sqs = new AmazonSQSClient(credentials);
    Region usEast1 = Region.getRegion(Regions.US_EAST_1);
    sqs.setRegion(usEast1);

    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");
        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 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");
            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:scheduler.SQSService.java

License:Apache License

public SQSService(String queueName) {
    /*/*from w  ww  .  j a va  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);
    }

    sqs = new AmazonSQSClient(credentials);
    Region usEast1 = Region.getRegion(Regions.US_EAST_1);
    sqs.setRegion(usEast1);

    // Create a queue or returns the URL of an existing one
    //System.out.println("Creating a new SQS queue called " + queueName);
    CreateQueueRequest createQueueRequest = new CreateQueueRequest(queueName);
    queueUrl = sqs.createQueue(createQueueRequest).getQueueUrl();

}

From source file:Server.aws.yulei.SQSOperation.java

License:Open Source License

public static void sendSQS(String message) {
    AWSCredentials credentials = null;//from w  w  w  . j  a  v a  2s  . c  o  m
    try {
        credentials = new ProfileCredentialsProvider("default").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 (/Users/daniel/.aws/credentials), and is in valid format.", e);
    }

    AmazonSQS sqs = new AmazonSQSClient(credentials);
    Region usWest2 = Region.getRegion(Regions.US_EAST_1);
    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 = "https://sqs.us-east-1.amazonaws.com/659322195879/MyQueue";

        // 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, message));
    } catch (Exception e) {
        Thread.currentThread().interrupt();
    }

}