Example usage for com.amazonaws.services.sqs.model Message getBody

List of usage examples for com.amazonaws.services.sqs.model Message getBody

Introduction

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

Prototype


public String getBody() 

Source Link

Document

The message's contents (not URL-encoded).

Usage

From source file:com.ibm.connectors.AmazonSQS.AmazonSQSInputConnector.java

License:Open Source License

@Override
public void poll(long waitInterval) {
    Properties properties = new Properties();

    String access_key_id = getProperty("AccessKeyId");
    String secret_access_key = getProperty("SecretAccessKey");
    BasicAWSCredentials credentials = new BasicAWSCredentials(access_key_id, secret_access_key);

    AmazonSQS sqs = new AmazonSQSClient(credentials);

    // Region selection
    Region region = Region.getRegion(Regions.fromName(getProperty("region")));
    sqs.setRegion(region);/*from  w  ww. jav a  2s .c  om*/

    GetQueueUrlResult queueUrl = sqs.getQueueUrl(getProperty("Queue"));

    ReceiveMessageRequest receiveMessageRequest = new ReceiveMessageRequest(queueUrl.getQueueUrl());
    List<Message> messages = sqs.receiveMessage(receiveMessageRequest).getMessages();

    String outputMessage = "";
    // if there are messages then do the processing
    if (messages.size() > 0) {

        //append the message properties to the localenv tree
        for (Message message : messages) {
            properties.setProperty("MessageId", message.getMessageId());
            properties.setProperty("ReceiptHandle", message.getReceiptHandle());
            properties.setProperty("MD5OfBody", message.getMD5OfBody());
            // get the message body to a string
            outputMessage = message.getBody();
        }
        properties.setProperty("queueUrl", queueUrl.getQueueUrl());
        // delete the message from the queue
        String messageReceiptHandle = messages.get(0).getReceiptHandle();
        sqs.deleteMessage(new DeleteMessageRequest(queueUrl.getQueueUrl(), messageReceiptHandle));
        ConnectorCallback callback = getCallback();
        callback.processInboundData(outputMessage.getBytes(), properties);
    }
}

From source file:com.kiribuki.queueservice.QueueService.java

License:Open Source License

public String ReceiveDeleteMessage() {
    Message newmessage = ReceiveMessage();
    DeleteMessage(newmessage.getReceiptHandle());
    return newmessage.getBody();
}

From source file:com.leverno.ysbos.archive.example.AmazonGlacierDownloadInventoryWithSQSPolling.java

License:Open Source License

private static Boolean waitForJobToComplete(String jobId, String sqsQueueUrl)
        throws InterruptedException, JsonParseException, IOException {

    Boolean messageFound = false;
    Boolean jobSuccessful = false;
    ObjectMapper mapper = new ObjectMapper();
    JsonFactory factory = mapper.getJsonFactory();

    while (!messageFound) {
        List<Message> msgs = sqsClient
                .receiveMessage(new ReceiveMessageRequest(sqsQueueUrl).withMaxNumberOfMessages(10))
                .getMessages();//from   w w  w.  java  2s.  co  m

        if (msgs.size() > 0) {
            for (Message m : msgs) {
                JsonParser jpMessage = factory.createJsonParser(m.getBody());
                JsonNode jobMessageNode = mapper.readTree(jpMessage);
                String jobMessage = jobMessageNode.get("Message").getTextValue();

                JsonParser jpDesc = factory.createJsonParser(jobMessage);
                JsonNode jobDescNode = mapper.readTree(jpDesc);
                String retrievedJobId = jobDescNode.get("JobId").getTextValue();
                String statusCode = jobDescNode.get("StatusCode").getTextValue();
                if (retrievedJobId.equals(jobId)) {
                    messageFound = true;
                    if (statusCode.equals("Succeeded")) {
                        jobSuccessful = true;
                    }
                }
            }

        } else {
            Thread.sleep(sleepTime * 1000);
        }
    }
    return (messageFound && jobSuccessful);
}

From source file:com.mateusz.mateuszsqs.SQSConfig.java

public static List<String> getMessages(AmazonSQS sqs, String sqsURL) {
    ReceiveMessageRequest receiveMessageRequest = new ReceiveMessageRequest(sqsURL);
    List<Message> messages = sqs.receiveMessage(receiveMessageRequest.withMessageAttributeNames("All"))
            .getMessages();//from  www . j  a  v  a2  s.  co  m
    List<String> filesToProcess = new ArrayList<String>();
    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 (Map.Entry<String, MessageAttributeValue> entry : message.getMessageAttributes().entrySet()) {
            System.out.println("  Attribute");
            System.out.println("    Name:  " + entry.getKey());
            System.out.println("    Value: " + entry.getValue().getStringValue());
            filesToProcess.add(entry.getValue().getStringValue());
        }
        System.out.println("Deleting a message.\n");
        String messageReceiptHandle = message.getReceiptHandle();
        sqs.deleteMessage(new DeleteMessageRequest(sqsURL, messageReceiptHandle));
    }

    return filesToProcess;
}

From source file:com.netflix.bdp.s3mper.alert.impl.AlertJanitor.java

License:Apache License

/**
 * Writes out logs to the given path as a separate JSON message per line.
 * /* ww  w.  j  a  v  a 2  s  .  c o m*/
 * @param queue
 * @param path
 * @throws IOException 
 */
public void writeLogs(String queue, Path path) throws IOException {
    FileSystem fs = FileSystem.get(path.toUri(), conf);
    DataOutputStream fout = fs.create(path);

    do {
        List<Message> messages = pull(queue, batchCount);

        if (messages.isEmpty()) {
            break;
        }

        for (Message m : messages) {
            fout.write((m.getBody().replaceAll("[\n|\r]", " ") + "\n").getBytes("UTF8"));
        }

        delete(queue, messages);
    } while (true);

    fout.close();
    fs.close();
}

From source file:com.netflix.spinnaker.echo.pubsub.amazon.SQSSubscriber.java

License:Apache License

private void handleMessage(Message message) {
    try {//from w  w  w. j av a  2 s.  c o  m
        String messageId = message.getMessageId();
        String messagePayload = unmarshallMessageBody(message.getBody());

        Map<String, String> stringifiedMessageAttributes = message.getMessageAttributes().entrySet().stream()
                .collect(Collectors.toMap(Map.Entry::getKey, e -> String.valueOf(e.getValue())));

        log.debug("Received Amazon sqs message: {} with payload: {} and attributes: {}", messageId,
                messagePayload, stringifiedMessageAttributes);

        MessageDescription description = MessageDescription.builder().subscriptionName(subscriptionName())
                .messagePayload(messagePayload).messageAttributes(stringifiedMessageAttributes)
                .pubsubSystem(pubsubSystem).ackDeadlineMillis(TimeUnit.SECONDS.toMillis(50)) // Set a high upper bound on message processing time.
                .retentionDeadlineMillis(TimeUnit.DAYS.toMillis(7)) // Expire key after max retention time, which is 7 days.
                .build();

        AmazonMessageAcknowledger acknowledger = new AmazonMessageAcknowledger(amazonSQS, queueId, message,
                registry, getName());

        if (subscription.getMessageFormat() != AmazonPubsubProperties.MessageFormat.NONE) {
            description.setArtifacts(parseArtifacts(description.getMessagePayload(), messageId));
        }
        pubsubMessageHandler.handleMessage(description, acknowledger, identity.getIdentity(), messageId);
    } catch (Exception e) {
        log.error("Message {} from queue {} failed to be handled", message, queueId);
        // Todo emjburns: add dead-letter queue policy
    }
}

From source file:com.netflix.spinnaker.echo.pubsub.aws.SQSSubscriber.java

License:Apache License

private void handleMessage(Message message) {
    try {/*www.j  a va2 s. com*/
        String messageId = message.getMessageId();
        String messagePayload = unmarshalMessageBody(message.getBody());

        Map<String, String> stringifiedMessageAttributes = message.getMessageAttributes().entrySet().stream()
                .collect(Collectors.toMap(Map.Entry::getKey, e -> String.valueOf(e.getValue())));

        //SNS message attributes are stored within the SQS message body. Add them to other attributes..
        Map<String, MessageAttributeWrapper> messageAttributes = unmarshalMessageAttributes(message.getBody());
        stringifiedMessageAttributes.putAll(messageAttributes.entrySet().stream()
                .collect(Collectors.toMap(Map.Entry::getKey, e -> e.getValue().getAttributeValue())));

        MessageDescription description = MessageDescription.builder().subscriptionName(getSubscriptionName())
                .messagePayload(messagePayload).messageAttributes(stringifiedMessageAttributes)
                .pubsubSystem(pubsubSystem).ackDeadlineSeconds(60) // Set a high upper bound on message processing time.
                .retentionDeadlineSeconds(subscription.getDedupeRetentionSeconds()) // Configurable but default to 1 hour
                .build();

        AmazonMessageAcknowledger acknowledger = new AmazonMessageAcknowledger(amazonSQS, queueId, message,
                registry, getName());

        if (subscription.getMessageFormat() != AmazonPubsubProperties.MessageFormat.NONE) {
            try {
                description.setArtifacts(parseArtifacts(description.getMessagePayload(), messageId));
            } catch (FatalTemplateErrorsException e) {
                log.error("Template failed to process artifacts for message {}", message, e);
            }
        }

        if (subscription.getAlternateIdInMessageAttributes() != null
                && !subscription.getAlternateIdInMessageAttributes().isEmpty()
                && stringifiedMessageAttributes.containsKey(subscription.getAlternateIdInMessageAttributes())) {
            // Message attributes contain the unique id used for deduping
            messageId = stringifiedMessageAttributes.get(subscription.getAlternateIdInMessageAttributes());
        }

        pubsubMessageHandler.handleMessage(description, acknowledger, identity.getIdentity(), messageId);
    } catch (Exception e) {
        registry.counter(getFailedToBeHandledMetricId(e)).increment();
        log.error("Message {} from queue {} failed to be handled", message, queueId, e);
        // Todo emjburns: add dead-letter queue policy
    }
}

From source file:com.netflix.suro.sink.notice.SQSNotice.java

License:Apache License

@Override
public String recv() {
    ReceiveMessageRequest request = new ReceiveMessageRequest().withQueueUrl(queueUrls.get(0))
            .withMaxNumberOfMessages(1);

    try {// ww  w  .j a va  2  s .c o m
        ReceiveMessageResult result = sqsClient.receiveMessage(request);
        if (!result.getMessages().isEmpty()) {
            Message msg = result.getMessages().get(0);

            recvMessageCount.incrementAndGet();

            DeleteMessageRequest deleteReq = new DeleteMessageRequest().withQueueUrl(queueUrls.get(0))
                    .withReceiptHandle(msg.getReceiptHandle());
            sqsClient.deleteMessage(deleteReq);

            if (enableBase64Encoding) {
                return new String(Base64.decodeBase64(msg.getBody().getBytes()), Charsets.UTF_8);
            } else {
                return msg.getBody();
            }
        } else {
            return "";
        }
    } catch (Exception e) {
        log.error("Exception while recving SQS notice: " + e.getMessage(), e);
        return "";
    }
}

From source file:com.netflix.suro.sink.notice.SQSNotice.java

License:Apache License

@Override
public Pair<String, String> peek() {
    ReceiveMessageRequest request = new ReceiveMessageRequest().withQueueUrl(queueUrls.get(0))
            .withMaxNumberOfMessages(1);

    try {//from w  w w.j av  a2 s. co m
        ReceiveMessageResult result = sqsClient.receiveMessage(request);
        if (!result.getMessages().isEmpty()) {
            Message msg = result.getMessages().get(0);

            recvMessageCount.incrementAndGet();

            if (enableBase64Encoding) {
                return new Pair<String, String>(msg.getReceiptHandle(),
                        new String(Base64.decodeBase64(msg.getBody().getBytes()), Charsets.UTF_8));
            } else {
                return new Pair<String, String>(msg.getReceiptHandle(), msg.getBody());
            }
        } else {
            return null;
        }
    } catch (Exception e) {
        log.error("Exception while recving SQS notice: " + e.getMessage(), e);
        return null;
    }
}

From source file:com.pinterest.teletraan.worker.LaunchEventCollector.java

License:Apache License

private boolean processMessage(Message message) throws Exception {
    String messageBody = message.getBody();
    EventMessage event = eventMessageParser.fromJson(messageBody);
    if (event == null) {
        return true;
    } else {/*w  ww . ja  v  a2s .c o  m*/
        return updateGroupInfo(event);
    }
}