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

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

Introduction

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

Prototype


public String getMessageId() 

Source Link

Document

A unique identifier for the message.

Usage

From source file:getting_started.SimpleQueueServiceSample.java

License:Open Source License

public static void main(String[] args) throws Exception {
    /*//from  w ww . j a va 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:hu.cloud.edu.SimpleQueueServiceSample.java

License:Open Source License

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

    /*//  w ww. j  av  a 2s  . c om
     * The ProfileCredentialsProvider will return your [default]
     * credential profile by reading from the credentials file located at
     * (C:\\Users\\Isaac\\.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\\Isaac\\.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 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:io.relution.jenkins.awssqs.SQSTrigger.java

License:Apache License

private void handleMessage(final Message message) {
    Log.info("Message received...");
    Map<String, String> jobParams = new HashMap<>();

    // add job parameters from the message (N.B. won't work post Jenkins v2+) @see https://wiki.jenkins-ci.org/display/JENKINS/Plugins+affected+by+fix+for+SECURITY-170
    for (Map.Entry<String, MessageAttributeValue> att : message.getMessageAttributes().entrySet()) {
        if (StringUtils.isNotBlank(att.getKey()) && att.getValue() != null) {
            jobParams.put("sqs_" + att.getKey(), att.getValue().getStringValue());
        }//w  w  w  . j a v  a 2 s .  co  m
    }
    jobParams.put("sqs_body", message.getBody());
    jobParams.put("sqs_messageId", message.getMessageId());
    jobParams.put("sqs_receiptHandle", message.getReceiptHandle());
    jobParams.put("sqs_bodyMD5", message.getMD5OfBody());
    startJob(jobParams);

    //        final MessageParser parser = this.messageParserFactory.createParser(message);
    //        final EventTriggerMatcher matcher = this.getEventTriggerMatcher();
    //        final List<ExecuteJenkinsJobEvent> events = parser.parseMessage(message);
    //
    //        if (matcher.matches(events, this.job)) {
    //            this.execute();
    //        }else{
    //            Log.info("Executing handleMessage when no event is matched");
    //            this.execute();
    //        }
}

From source file:it.polimi.modaclouds.cpimlibrary.msgqueuemng.AmazonMessageQueue.java

License:Apache License

@Override
public CloudMessage getMessage() {
    String msg = null;//  w w w.  j  a v a  2  s.  c  o  m
    String id = null;
    String queueUrl = sqs.getQueueUrl(new GetQueueUrlRequest(queueName)).getQueueUrl();
    ReceiveMessageRequest receiveMessageRequest = new ReceiveMessageRequest(queueUrl);
    List<Message> messages = sqs.receiveMessage(receiveMessageRequest).getMessages();
    for (Message m : messages) {
        msg = m.getBody();
        id = m.getMessageId();
        receipts.put(id, m.getReceiptHandle());
        break;
    }
    CloudMessage cm = new CloudMessage(msg);
    cm.setId(id);
    return cm;
}

From source file:it.polimi.modaclouds.cpimlibrary.taskqueuemng.AmazonTaskQueue.java

License:Apache License

public AmazonTaskQueueMessage getMessage() {
    String text = null;/*from ww  w  .  j av a2 s.c  om*/
    String id = null;
    String pop = null;
    String queueUrl = sqs.getQueueUrl(new GetQueueUrlRequest(queueName)).getQueueUrl();
    ReceiveMessageRequest receiveMessageRequest = new ReceiveMessageRequest(queueUrl);
    List<Message> messages = sqs.receiveMessage(receiveMessageRequest).getMessages();
    if (messages.size() == 0)
        return null;
    for (Message m : messages) {
        text = m.getBody();
        id = m.getMessageId();
        pop = m.getReceiptHandle();
        receipts.put(id, pop);
        break;
    }
    return new AmazonTaskQueueMessage(id, text, pop);
}

From source file:org.apache.nifi.processors.aws.sqs.GetSQS.java

License:Apache License

@Override
public void onTrigger(final ProcessContext context, final ProcessSession session) {
    final String queueUrl = context.getProperty(DYNAMIC_QUEUE_URL).evaluateAttributeExpressions().getValue();

    final AmazonSQSClient client = getClient();

    final ReceiveMessageRequest request = new ReceiveMessageRequest();
    request.setAttributeNames(Collections.singleton("All"));
    request.setMessageAttributeNames(Collections.singleton("All"));
    request.setMaxNumberOfMessages(context.getProperty(BATCH_SIZE).asInteger());
    request.setVisibilityTimeout(//from   ww w  .  ja v  a 2 s  . c om
            context.getProperty(VISIBILITY_TIMEOUT).asTimePeriod(TimeUnit.SECONDS).intValue());
    request.setQueueUrl(queueUrl);
    request.setWaitTimeSeconds(
            context.getProperty(RECEIVE_MSG_WAIT_TIME).asTimePeriod(TimeUnit.SECONDS).intValue());

    final Charset charset = Charset.forName(context.getProperty(CHARSET).getValue());

    final ReceiveMessageResult result;
    try {
        result = client.receiveMessage(request);
    } catch (final Exception e) {
        getLogger().error("Failed to receive messages from Amazon SQS due to {}", new Object[] { e });
        context.yield();
        return;
    }

    final List<Message> messages = result.getMessages();
    if (messages.isEmpty()) {
        context.yield();
        return;
    }

    final boolean autoDelete = context.getProperty(AUTO_DELETE).asBoolean();

    for (final Message message : messages) {
        FlowFile flowFile = session.create();

        final Map<String, String> attributes = new HashMap<>();
        for (final Map.Entry<String, String> entry : message.getAttributes().entrySet()) {
            attributes.put("sqs." + entry.getKey(), entry.getValue());
        }

        for (final Map.Entry<String, MessageAttributeValue> entry : message.getMessageAttributes().entrySet()) {
            attributes.put("sqs." + entry.getKey(), entry.getValue().getStringValue());
        }

        attributes.put("hash.value", message.getMD5OfBody());
        attributes.put("hash.algorithm", "md5");
        attributes.put("sqs.message.id", message.getMessageId());
        attributes.put("sqs.receipt.handle", message.getReceiptHandle());

        flowFile = session.putAllAttributes(flowFile, attributes);
        flowFile = session.write(flowFile, new OutputStreamCallback() {
            @Override
            public void process(final OutputStream out) throws IOException {
                out.write(message.getBody().getBytes(charset));
            }
        });

        session.transfer(flowFile, REL_SUCCESS);
        session.getProvenanceReporter().receive(flowFile, queueUrl);

        getLogger().info("Successfully received {} from Amazon SQS", new Object[] { flowFile });
    }

    if (autoDelete) {
        // If we want to auto-delete messages, we must fist commit the session to ensure that the data
        // is persisted in NiFi's repositories.
        session.commit();

        final DeleteMessageBatchRequest deleteRequest = new DeleteMessageBatchRequest();
        deleteRequest.setQueueUrl(queueUrl);
        final List<DeleteMessageBatchRequestEntry> deleteRequestEntries = new ArrayList<>();
        for (final Message message : messages) {
            final DeleteMessageBatchRequestEntry entry = new DeleteMessageBatchRequestEntry();
            entry.setId(message.getMessageId());
            entry.setReceiptHandle(message.getReceiptHandle());
            deleteRequestEntries.add(entry);
        }

        deleteRequest.setEntries(deleteRequestEntries);

        try {
            client.deleteMessageBatch(deleteRequest);
        } catch (final Exception e) {
            getLogger().error(
                    "Received {} messages from Amazon SQS but failed to delete the messages; these messages"
                            + " may be duplicated. Reason for deletion failure: {}",
                    new Object[] { messages.size(), e });
        }
    }

}

From source file:org.apache.usergrid.persistence.queue.impl.SNSQueueManagerImpl.java

License:Apache License

@Override
public List<LegacyQueueMessage> getMessages(final int limit, final Class klass) {

    if (sqs == null) {
        logger.error("SQS is null - was not initialized properly");
        return new ArrayList<>(0);
    }//from  w ww .ja  v a  2 s. c  o m

    String url = getReadQueue().getUrl();

    if (logger.isTraceEnabled()) {
        logger.trace("Getting up to {} messages from {}", limit, url);
    }

    ArrayList<String> requestMessageAttributeNames = new ArrayList<String>(1);
    requestMessageAttributeNames.add("ApproximateReceiveCount");

    ReceiveMessageRequest receiveMessageRequest = new ReceiveMessageRequest(url);
    receiveMessageRequest.setAttributeNames(requestMessageAttributeNames);
    receiveMessageRequest.setMaxNumberOfMessages(limit);
    receiveMessageRequest
            .setVisibilityTimeout(Math.max(MIN_VISIBILITY_TIMEOUT, fig.getVisibilityTimeout() / 1000));

    int longPollTimeout = Math.min(20000, fig.getQueuePollTimeout()); // 20000 is the SQS maximum

    // ensure the client's socket timeout is not less than the configure long poll timeout
    if (fig.getQueueClientSocketTimeout() < longPollTimeout) {

        longPollTimeout = Math.max(0, fig.getQueueClientSocketTimeout() - 1000);

    }

    receiveMessageRequest.setWaitTimeSeconds(longPollTimeout / 1000); // convert to seconds

    try {
        ReceiveMessageResult result = sqs.receiveMessage(receiveMessageRequest);
        List<Message> messages = result.getMessages();

        if (logger.isTraceEnabled()) {
            logger.trace("Received {} messages from {}", messages.size(), url);
        }

        List<LegacyQueueMessage> queueMessages = new ArrayList<>(messages.size());

        for (Message message : messages) {

            Object payload;
            final String originalBody = message.getBody();

            try {
                final JsonNode bodyNode = mapper.readTree(message.getBody());

                /**
                 * When a message originates from SNS it has a "Message"  we have to extract
                 * it and then process it separately
                 */

                if (bodyNode.has("Message")) {
                    final String snsNode = bodyNode.get("Message").asText();

                    payload = deSerializeSQSMessage(snsNode, klass);
                } else {
                    payload = deSerializeSQSMessage(originalBody, klass);
                }
            } catch (Exception e) {
                logger.error("failed to deserialize message: {}", message.getBody(), e);
                throw new RuntimeException(e);
            }

            LegacyQueueMessage queueMessage = new LegacyQueueMessage(message.getMessageId(),
                    message.getReceiptHandle(), payload, message.getAttributes().get("type"));
            queueMessage.setStringBody(originalBody);
            int receiveCount = Integer.valueOf(message.getAttributes().get("ApproximateReceiveCount"));
            queueMessage.setReceiveCount(receiveCount);
            queueMessages.add(queueMessage);
        }

        return queueMessages;
    } catch (com.amazonaws.services.sqs.model.QueueDoesNotExistException dne) {
        logger.error("Queue does not exist! [{}]", url, dne);
    } catch (Exception e) {
        logger.error("Programming error getting messages from queue=[{}] exist!", url, e);
    }

    return new ArrayList<>(0);
}

From source file:org.duracloud.common.queue.aws.SQSTaskQueue.java

License:Apache License

protected Task marshallTask(Message msg) {
    Properties props = new Properties();
    Task task = null;/*from   w  ww . j  ava 2 s . co  m*/
    try {
        props.load(new StringReader(msg.getBody()));

        if (props.containsKey(Task.KEY_TYPE)) {
            task = new Task();
            for (final String key : props.stringPropertyNames()) {
                if (key.equals(Task.KEY_TYPE)) {
                    task.setType(Task.Type.valueOf(props.getProperty(key)));
                } else {
                    task.addProperty(key, props.getProperty(key));
                }
            }
            task.addProperty(MsgProp.MSG_ID.name(), msg.getMessageId());
            task.addProperty(MsgProp.RECEIPT_HANDLE.name(), msg.getReceiptHandle());
        } else {
            log.error("SQS message from queue: " + queueName + ", queueUrl: " + queueUrl
                    + " does not contain a 'task type'");
        }
    } catch (IOException ioe) {
        log.error("Error creating Task", ioe);
    }
    return task;
}

From source file:org.duracloud.common.queue.aws.SQSTaskQueue.java

License:Apache License

@Override
public Set<Task> take(int maxTasks) throws TimeoutException {
    ReceiveMessageResult result = sqsClient.receiveMessage(new ReceiveMessageRequest().withQueueUrl(queueUrl)
            .withMaxNumberOfMessages(maxTasks).withAttributeNames("SentTimestamp", "ApproximateReceiveCount"));
    if (result.getMessages() != null && result.getMessages().size() > 0) {
        Set<Task> tasks = new HashSet<>();
        for (Message msg : result.getMessages()) {

            // The Amazon docs claim this attribute is 'returned as an integer
            // representing the epoch time in milliseconds.'
            // http://docs.aws.amazon.com/AWSSimpleQueueService/latest/APIReference/Query_QueryReceiveMessage.html
            try {
                Long sentTime = Long.parseLong(msg.getAttributes().get("SentTimestamp"));
                Long preworkQueueTime = System.currentTimeMillis() - sentTime;
                log.info(/* w  w w.  ja  v a  2 s  . c  om*/
                        "SQS message received - queue: {}, queueUrl: {}, msgId: {},"
                                + " preworkQueueTime: {}, receiveCount: {}",
                        queueName, queueUrl, msg.getMessageId(),
                        DurationFormatUtils.formatDuration(preworkQueueTime, "HH:mm:ss,SSS"),
                        msg.getAttributes().get("ApproximateReceiveCount"));
            } catch (NumberFormatException nfe) {
                log.error("Error converting 'SentTimestamp' SQS message" + " attribute to Long, messageId: "
                        + msg.getMessageId(), nfe);
            }

            Task task = marshallTask(msg);
            task.setVisibilityTimeout(visibilityTimeout);
            tasks.add(task);
        }

        return tasks;
    } else {
        throw new TimeoutException("No tasks available from queue: " + queueName + ", queueUrl: " + queueUrl);
    }
}

From source file:org.flite.mock.amazonaws.sqs.AmazonSQSMock.java

License:Open Source License

public SendMessageResult sendMessage(final SendMessageRequest request)
        throws AmazonServiceException, AmazonClientException {
    if (request == null) {
        throw new AmazonClientException("Null SendMessageRequest");
    }//from   w w  w. j  a va  2s.c o  m
    final String queueUrl = request.getQueueUrl();
    checkURLForException(queueUrl);
    checkStringForExceptionMarker(request.getMessageBody());
    // Ignoring the following exception: InvalidMessageContentsException (thrown for character set conditions?)
    if (!allQueues.containsKey(queueUrl)) {
        throw new AmazonServiceException("Queue Not Found: " + queueUrl);
    }
    final Long seq = incrementer.getAndIncrement();
    final Message msg = new Message().withBody(StringUtils.defaultString(request.getMessageBody()))
            .withMD5OfBody(makeMD5(request.getMessageBody())).withMessageId(MESSAGE_ID_PREFIX + seq);
    allQueues.get(queueUrl).add(msg);
    return new SendMessageResult().withMD5OfMessageBody(msg.getMD5OfBody()).withMessageId(msg.getMessageId());
}