Example usage for com.amazonaws.services.sqs.model SendMessageRequest SendMessageRequest

List of usage examples for com.amazonaws.services.sqs.model SendMessageRequest SendMessageRequest

Introduction

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

Prototype

public SendMessageRequest(String queueUrl, String messageBody) 

Source Link

Document

Constructs a new SendMessageRequest object.

Usage

From source file:se252.jan15.calvinandhobbes.project0.IIScCampusMapGETProxyService.java

License:Open Source License

private static String getCategoryData(String category) {
    AmazonSQS queueClient = Queues.getQueue();
    String resp = null;// ww w.  ja  v  a  2  s  .c o m
    try {
        // Send a message
        queueClient.sendMessage(new SendMessageRequest(Queues.req, category));

        // Receive messages
        Boolean flag = true;
        while (flag) {
            ReceiveMessageRequest receiveReq = new ReceiveMessageRequest(Queues.resp);
            receiveReq.setWaitTimeSeconds(10);
            List<Message> messages = queueClient.receiveMessage(receiveReq).getMessages();
            for (Message message : messages) {
                String[] strs = message.getBody().split("\\$");
                if (strs[0].equals(category)) {
                    flag = false;
                    resp = strs[1];
                    queueClient
                            .deleteMessage(new DeleteMessageRequest(Queues.resp, message.getReceiptHandle()));
                    break;
                }
            }
        }
    } 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());
    }
    return resp;
}

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

License:Open Source License

public static void sendSQS(String message) {
    AWSCredentials credentials = null;//from   ww  w .j  a  va2  s .  co 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();
    }

}

From source file:services.SQSQueue.java

License:Open Source License

/**
 * This function sends message to queue.
 * @param msg the message to send/*from  ww w  . j a v a  2  s. c o m*/
 */
public int sendMsg(String msg) {
    try {
        System.out.println("Sending a message to " + QueueName + ".\n");
        sqs.sendMessage(new SendMessageRequest(queueUrl, msg));
        this.numberOfMsg++;
    }

    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("Org message" + this.QueueName + " m is " + msg);
        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());
        numberOfMsg--;
        return -1;
    }

    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());
        numberOfMsg--;
        return -1;
    }

    return 0;
}

From source file:servlet.FileUploadServlet.java

public void sqs(String msg) throws Exception {
    AWSCredentials awsCreds = new PropertiesCredentials(
            new File("/Users/paulamontojo/Desktop/AwsCredentials.properties"));

    AmazonSQS sqs = new AmazonSQSClient(awsCreds);
    Region usWest2 = Region.getRegion(Regions.US_WEST_2);
    sqs.setRegion(usWest2);/*from   ww w . ja  va2  s .  co m*/

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

    try {

        String myQueueUrl = "https://sqs.us-west-2.amazonaws.com/711690152696/MyQueue";

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

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

    } 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:shnakkydoodle.queueing.provider.aws.AwsProvider.java

License:Open Source License

/**
 * Enqueue a message/*from  w  w  w .  j a  v a2s . co  m*/
 * 
 * @param queuename
 * @param message
 */
@Override
public void enqueue(String queueName, String message) throws Exception {
    AmazonSQS sqs = new AmazonSQSClient(credentials);
    sqs.setRegion(Region.EU_Ireland.toAWSRegion());
    sqs.createQueue(queueName);
    CreateQueueRequest createQueueRequest = new CreateQueueRequest(queueName);
    String myQueueUrl = sqs.createQueue(createQueueRequest).getQueueUrl();
    sqs.sendMessage(new SendMessageRequest(myQueueUrl, message));
}

From source file:smartthings.brave.sqs.AmazonSQSRule.java

License:Apache License

private void sendSpansInternal(List<Span> spans) {
    String body = Base64.encodeAsString(SpanBytesEncoder.JSON_V2.encodeList(spans));
    client.sendMessage(new SendMessageRequest(queueUrl, body));
}

From source file:smartthings.brave.sqs.TracingAmazonSQSClient.java

License:Apache License

@Override
public SendMessageResult sendMessage(String queueUrl, String messageBody) {
    return this.sendMessage(new SendMessageRequest(queueUrl, messageBody));
}

From source file:sqs_examples.SimpleQueueServiceSample.java

License:Open Source License

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

    /*//www  .jav a 2 s.  c om
     * The ProfileCredentialsProvider will return your [default]
     * credential profile by reading from the credentials file located at
     * (/Users/pmacharl/.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 (/Users/pmacharl/.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");
        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 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:test1.SimpleQueueServiceSample.java

License:Open Source License

public static void getMessage(String msg, AmazonSQS sqs, String myQueueUrl) {
    try {//from   w w  w .  j a va2s.  c o m
        sqs.sendMessage(new SendMessageRequest(myQueueUrl, msg));

    } 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:util.SQSManager.java

License:Open Source License

public void sendMessage(String messageBody) {
    try {// w  w  w  . j a  v a 2  s  . co m
        // Send a message
        System.out.println("HTTP Sending " + messageBody + " a message to MyQueue" + myQueueUrl + ".\n");
        sqs.sendMessage(new SendMessageRequest(myQueueUrl, messageBody));
        System.out.println(myQueueUrl);
    } catch (Exception e) {
        m_instance = null;
        e.printStackTrace();
        throw e;
    }
}