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:sqsAlertPersist.java

License:Open Source License

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

    // get credentials
    String user = "jreilly";
    AWSCredentials credentials = whgHelper.getCred(user);

    // use credentials to set access to SQS
    AmazonSQS sqs = whgHelper.setQueueAccess(credentials);

    // define queue that messages will be retrieved from
    String thisQueue = "alertPersist";
    String nextQueue = "alertCache";

    // set access to database with credentials
    dynamoDB = new AmazonDynamoDBClient(credentials);
    Region usEast1 = Region.getRegion(Regions.US_EAST_1);
    dynamoDB.setRegion(usEast1);// w  ww. j  a  va2  s  . co  m

    // check for table, create one if missing
    String tableName = "alerts";
    whgHelper.setTable(dynamoDB, tableName);

    while (1 > 0) {

        // pull list of current messages (up to 10) in the queue
        List<Message> messages = whgHelper.getMessagesFromQueue(thisQueue, sqs);
        System.out.println("Count of messages in " + thisQueue + ": " + messages.size());

        try {

            for (Message message : messages) {

                whgHelper.printMessage(message);
                for (Entry<String, String> entry : message.getAttributes().entrySet()) {
                    whgHelper.printMessageEntry(entry);
                }

                // Add an item to DynamoDB table
                Map<String, AttributeValue> item = whgHelper.newAlert(message.getBody());
                PutItemRequest putItemRequest = new PutItemRequest(tableName, item);
                PutItemResult putItemResult = dynamoDB.putItem(putItemRequest);
                System.out.println();
                System.out.println("Result: " + putItemResult);

                // then send message to cache queue
                System.out.println("Sending messages to next queue.");
                sqs.sendMessage(new SendMessageRequest(nextQueue, message.getBody()));

                // delete message after sending to persist queue
                System.out.println("Deleting message from this queue.\n");
                String messageRecieptHandle = message.getReceiptHandle();
                sqs.deleteMessage(new DeleteMessageRequest(thisQueue, messageRecieptHandle));

            }
            Thread.sleep(20000); // do nothing for 2000 miliseconds (2 second)

        } catch (AmazonServiceException ase) {
            whgHelper.errorMessagesAse(ase);
        } catch (AmazonClientException ace) {
            whgHelper.errorMessagesAce(ace);
        }
    }
}

From source file:SQS.java

License:Open Source License

public void sendMessage(String myQueueUrl, String message) {
    try {//from   www. j  a  v a  2s .co  m
        //System.out.println("Sending a message");
        sqs.sendMessage(new SendMessageRequest(myQueueUrl, message));
    } 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:app.SQSMessage.java

License:Open Source License

public void sendmsg(Tweet t, String keyword) throws Exception {

    /*// w w  w . j  a  va 2 s .co m
     * The ProfileCredentialsProvider will return your [default]
     * credential profile by reading from the credentials file located at
     * ().
     */
    credentials = new ProfileCredentialsProvider("default").getCredentials();
    AmazonSQS sqs = new AmazonSQSClient(credentials);
    Region usWest2 = Region.getRegion(Regions.US_EAST_1);
    sqs.setRegion(usWest2);

    System.out.println("===========================================");
    System.out.println("sending tweet to Queue");
    System.out.println("===========================================\n");

    try {

        // Send a message
        System.out.println("Sending a message to TweetQueue.\n");

        JSONObject obj = new JSONObject();

        obj.put("userId", t.getUserId() + "");
        obj.put("lng", t.getLongitude() + "");
        obj.put("lat", t.getLatitude() + "");
        obj.put("text", t.getText());
        // obj.put("time", t.getCreatedTime().toString());
        obj.put("kwd", keyword);

        //String TwtJson = "{\"userId\":\"" + t.getUserId() + "\",\"lng\":\"" + t.getLongitude() + "\",\"lat\":\"" + t.getLatitude() + "\",\"text\":\"" + JSONObject.escape(t.getText()) +  "\",\"time\":\"" + t.getCreatedTime() + "\",\"kwd\":\"" + keyword + "\"}";

        String TwtJson = obj.toString();

        System.out.println(TwtJson);
        sqs.sendMessage(new SendMessageRequest(q_url, TwtJson));

    } 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:aws.sample.SimpleQueueServiceSample.java

License:Open Source License

public static void main(String[] args) throws Exception {
    /*//from   w  ww  .j av  a  2s. c  o  m
     * Important: Be sure to fill in your AWS access credentials in the AwsCredentials.properties file before you try to run this sample. http://aws.amazon.com/security-credentials
     */
    AmazonSQS sqs = new AmazonSQSClient(new PropertiesCredentials(
            SimpleQueueServiceSample.class.getResourceAsStream("/AwsCredentials.properties")));

    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:awslabs.lab31.StudentCode.java

License:Open Source License

/**
 * Post a message to the specified queue. Hint: Use the sendMessage() method of the client object.
 * /*from   w w  w .j a  v  a 2 s.c  o m*/
 * @param sqsClient The SQS Client object.
 * @param queueUrl The URL for the queue to place the message in.
 * @param messageText The body of the message to place in the queue.
 */
@Override
public void postToQueue(AmazonSQSClient sqsClient, String queueUrl, String messageText) {
    SendMessageRequest request = new SendMessageRequest(queueUrl, messageText);

    sqsClient.sendMessage(request);
}

From source file:Cloud.Tweets.SimpleQueueServiceSample.java

License:Open Source License

public void addmessage(AmazonSQS abc, String x) {
    System.out.println("Sending a message to MyQueue.\n" + myQueueUrl);
    try {//from www.j av a  2 s .  c  o  m
        System.out.println(x);
        abc.sendMessage(new SendMessageRequest(myQueueUrl, x));

        System.out.println("Message Sent.\n");
    } 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:cloudworker.Animoto.java

License:Apache License

@SuppressWarnings("unchecked")
public void run() {
    //Get queue url   
    GetQueueUrlResult urlResult = sqs.getQueueUrl(responseQName);
    String QueueUrl = urlResult.getQueueUrl();
    JSONObject result = new JSONObject();

    Runtime runtime = Runtime.getRuntime();

    try {/*w  ww.ja  v a  2 s. c  om*/
        String[] urls = task.split(" ");

        for (String url : urls) {
            //System.out.println(url);
            Process p = runtime.exec("wget " + url);
            p.waitFor();
        }

        Process rename = runtime.exec("./rename.sh");
        rename.waitFor();

        runtime.exec("ffmpeg -f image2 -i img%03d.jpg movie.mpg");

        File movie = new File("movie.mpg");

        S3Service s3 = new S3Service();
        URL url = s3.put(movie.getName(), movie);

        //result.put("task_id", task_id);
        result.put("URL", url.toString());

        sqs.sendMessage(new SendMessageRequest(QueueUrl, result.toString()));
        //System.out.println(Thread.currentThread().getName()+" sleep done!");

    } catch (Exception e) {
        //result.put("task_id", task_id);
        result.put("URL", "Failed!");
        sqs.sendMessage(new SendMessageRequest(QueueUrl, result.toString()));

    }
}

From source file:cloudworker.WorkerThread.java

License:Apache License

@SuppressWarnings("unchecked")
@Override/*ww  w  . j av a 2s.c  o m*/
public void run() {
    //Get queue url   
    GetQueueUrlResult urlResult = sqs.getQueueUrl(responseQName);
    String QueueUrl = urlResult.getQueueUrl();
    JSONObject result = new JSONObject();

    try {
        Thread.sleep(sleepLength);

        result.put("task_id", task_id);
        result.put("result", "0");

        sqs.sendMessage(new SendMessageRequest(QueueUrl, result.toString()));
        //System.out.println(Thread.currentThread().getName()+" sleep done!");

    } catch (Exception e) {
        result.put("task_id", task_id);
        result.put("result", "1");
        sqs.sendMessage(new SendMessageRequest(QueueUrl, result.toString()));

    }
}

From source file:com.amazon.aws.demo.anonymous.sqs.SimpleQueue.java

License:Open Source License

public static SendMessageResult sendMessage(String queueUrl, String body) {
    SendMessageRequest req = new SendMessageRequest(queueUrl, body);
    return getInstance().sendMessage(req);
}

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

License:Open Source License

/**
 * <p>// ww  w. j ava  2s . com
 * Delivers a message to the specified queue and uploads the message payload
 * to Amazon S3 if necessary.
 * </p>
 * <p>
 * <b>IMPORTANT:</b> The following list shows the characters (in Unicode)
 * allowed in your message, according to the W3C XML specification. For more
 * information, go to http://www.w3.org/TR/REC-xml/#charsets If you send any
 * characters not included in the list, your request will be rejected. #x9 |
 * #xA | #xD | [#x20 to #xD7FF] | [#xE000 to #xFFFD] | [#x10000 to #x10FFFF]
 * </p>
 * 
 * @param queueUrl
 *            The URL of the Amazon SQS queue to take action on.
 * @param messageBody
 *            The message to send. For a list of allowed characters, see the
 *            preceding important note.
 * 
 * @return The response from the SendMessage service method, as returned by
 *         AmazonSQS.
 * 
 * @throws InvalidMessageContentsException
 * @throws UnsupportedOperationException
 *
 * @throws AmazonClientException
 *             If any internal errors are encountered inside the client
 *             while attempting to make the request or handle the response.
 *             For example if a network connection is not available.
 * @throws AmazonServiceException
 *             If an error response is returned by AmazonSQS indicating
 *             either a problem with the data in the request, or a server
 *             side issue.
 */
public SendMessageResult sendMessage(String queueUrl, String messageBody) {
    SendMessageRequest sendMessageRequest = new SendMessageRequest(queueUrl, messageBody);
    return sendMessage(sendMessageRequest);
}