Search in sources :

Example 1 with ReceiveMessageRequest

use of software.amazon.awssdk.services.sqs.model.ReceiveMessageRequest in project aws-doc-sdk-examples by awsdocs.

the class SendReceiveMessages method getMessages.

public String getMessages() {
    List attr = new ArrayList<String>();
    attr.add("Name");
    SqsClient sqsClient = getClient();
    try {
        GetQueueUrlRequest getQueueRequest = GetQueueUrlRequest.builder().queueName(QUEUE_NAME).build();
        String queueUrl = sqsClient.getQueueUrl(getQueueRequest).queueUrl();
        // Receive messages from the queue
        ReceiveMessageRequest receiveRequest = ReceiveMessageRequest.builder().queueUrl(queueUrl).maxNumberOfMessages(10).messageAttributeNames(attr).build();
        List<Message> messages = sqsClient.receiveMessage(receiveRequest).messages();
        com.example.Message myMessage;
        List allMessages = new ArrayList<com.example.Message>();
        // Push the messages to a list
        for (Message m : messages) {
            myMessage = new com.example.Message();
            myMessage.setBody(m.body());
            Map map = m.messageAttributes();
            MessageAttributeValue val = (MessageAttributeValue) map.get("Name");
            myMessage.setName(val.stringValue());
            allMessages.add(myMessage);
        }
        return convertToString(toXml(allMessages));
    } catch (SqsException e) {
        e.getStackTrace();
    }
    return "";
}
Also used : ReceiveMessageRequest(software.amazon.awssdk.services.sqs.model.ReceiveMessageRequest) Message(software.amazon.awssdk.services.sqs.model.Message) SqsClient(software.amazon.awssdk.services.sqs.SqsClient)

Example 2 with ReceiveMessageRequest

use of software.amazon.awssdk.services.sqs.model.ReceiveMessageRequest in project aws-doc-sdk-examples by awsdocs.

the class VideoDetect method getLabelJob.

public static void getLabelJob(RekognitionClient rekClient, SqsClient sqs, String queueUrl) {
    List<Message> messages = null;
    ReceiveMessageRequest messageRequest = ReceiveMessageRequest.builder().queueUrl(queueUrl).build();
    try {
        messages = sqs.receiveMessage(messageRequest).messages();
        if (!messages.isEmpty()) {
            for (Message message : messages) {
                String notification = message.body();
                // Get the status and job id from the notification
                ObjectMapper mapper = new ObjectMapper();
                JsonNode jsonMessageTree = mapper.readTree(notification);
                JsonNode messageBodyText = jsonMessageTree.get("Message");
                ObjectMapper operationResultMapper = new ObjectMapper();
                JsonNode jsonResultTree = operationResultMapper.readTree(messageBodyText.textValue());
                JsonNode operationJobId = jsonResultTree.get("JobId");
                JsonNode operationStatus = jsonResultTree.get("Status");
                System.out.println("Job found in JSON is " + operationJobId);
                DeleteMessageRequest deleteMessageRequest = DeleteMessageRequest.builder().queueUrl(queueUrl).build();
                String jobId = operationJobId.textValue();
                if (startJobId.compareTo(jobId) == 0) {
                    System.out.println("Job id: " + operationJobId);
                    System.out.println("Status : " + operationStatus.toString());
                    if (operationStatus.asText().equals("SUCCEEDED"))
                        GetResultsLabels(rekClient);
                    else
                        System.out.println("Video analysis failed");
                    sqs.deleteMessage(deleteMessageRequest);
                } else {
                    System.out.println("Job received was not job " + startJobId);
                    sqs.deleteMessage(deleteMessageRequest);
                }
            }
        }
    } catch (RekognitionException e) {
        e.getMessage();
        System.exit(1);
    } catch (JsonMappingException e) {
        e.printStackTrace();
    } catch (JsonProcessingException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : Message(software.amazon.awssdk.services.sqs.model.Message) ReceiveMessageRequest(software.amazon.awssdk.services.sqs.model.ReceiveMessageRequest) RekognitionException(software.amazon.awssdk.services.rekognition.model.RekognitionException) JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException) DeleteMessageRequest(software.amazon.awssdk.services.sqs.model.DeleteMessageRequest) JsonNode(com.fasterxml.jackson.databind.JsonNode) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) RekognitionException(software.amazon.awssdk.services.rekognition.model.RekognitionException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException)

Example 3 with ReceiveMessageRequest

use of software.amazon.awssdk.services.sqs.model.ReceiveMessageRequest in project aws-doc-sdk-examples by awsdocs.

the class SendReceiveMessages method main.

public static void main(String[] args) {
    SqsClient sqsClient = SqsClient.builder().region(Region.US_WEST_2).build();
    try {
        CreateQueueRequest request = CreateQueueRequest.builder().queueName(QUEUE_NAME).build();
        CreateQueueResponse createResult = sqsClient.createQueue(request);
        GetQueueUrlRequest getQueueRequest = GetQueueUrlRequest.builder().queueName(QUEUE_NAME).build();
        String queueUrl = sqsClient.getQueueUrl(getQueueRequest).queueUrl();
        SendMessageRequest sendMsgRequest = SendMessageRequest.builder().queueUrl(queueUrl).messageBody("hello world").delaySeconds(5).build();
        sqsClient.sendMessage(sendMsgRequest);
        // Send multiple messages to the queue
        SendMessageBatchRequest sendBatchRequest = SendMessageBatchRequest.builder().queueUrl(queueUrl).entries(SendMessageBatchRequestEntry.builder().messageBody("Hello from message 1").id("msg_1").build(), SendMessageBatchRequestEntry.builder().messageBody("Hello from message 2").delaySeconds(10).id("msg_2").build()).build();
        sqsClient.sendMessageBatch(sendBatchRequest);
        // Receive messages from the queue
        ReceiveMessageRequest receiveRequest = ReceiveMessageRequest.builder().queueUrl(queueUrl).build();
        List<Message> messages = sqsClient.receiveMessage(receiveRequest).messages();
        // Print out the messages
        for (Message m : messages) {
            System.out.println("\n" + m.body());
        }
    } catch (QueueNameExistsException e) {
        throw e;
    }
}
Also used : CreateQueueResponse(software.amazon.awssdk.services.sqs.model.CreateQueueResponse) GetQueueUrlRequest(software.amazon.awssdk.services.sqs.model.GetQueueUrlRequest) QueueNameExistsException(software.amazon.awssdk.services.sqs.model.QueueNameExistsException) SendMessageBatchRequest(software.amazon.awssdk.services.sqs.model.SendMessageBatchRequest) ReceiveMessageRequest(software.amazon.awssdk.services.sqs.model.ReceiveMessageRequest) Message(software.amazon.awssdk.services.sqs.model.Message) CreateQueueRequest(software.amazon.awssdk.services.sqs.model.CreateQueueRequest) SqsClient(software.amazon.awssdk.services.sqs.SqsClient) SendMessageRequest(software.amazon.awssdk.services.sqs.model.SendMessageRequest)

Example 4 with ReceiveMessageRequest

use of software.amazon.awssdk.services.sqs.model.ReceiveMessageRequest in project beam by apache.

the class SqsUnboundedReader method pull.

/**
 * BLOCKING. Fetch another batch of messages from SQS.
 */
private void pull() {
    if (inFlight.size() >= MAX_IN_FLIGHT) {
        // messages faster than we can checkpoint them.
        return;
    }
    long requestTimeMsSinceEpoch = now();
    long deadlineMsSinceEpoch = requestTimeMsSinceEpoch + visibilityTimeoutMs;
    final ReceiveMessageRequest receiveMessageRequest = ReceiveMessageRequest.builder().maxNumberOfMessages(MAX_NUMBER_OF_MESSAGES).attributeNamesWithStrings(SENT_TIMESTAMP.toString()).queueUrl(queueUrl()).build();
    final ReceiveMessageResponse receiveMessageResponse = sqsClient.receiveMessage(receiveMessageRequest);
    final List<Message> messages = receiveMessageResponse.messages();
    if (messages == null || messages.isEmpty()) {
        return;
    }
    lastReceivedMsSinceEpoch = requestTimeMsSinceEpoch;
    // Capture the received messages.
    for (Message orgMsg : messages) {
        long msgTimeStamp = Long.parseLong(orgMsg.attributes().get(SENT_TIMESTAMP));
        SqsMessage message = SqsMessage.create(orgMsg.body(), orgMsg.messageId(), orgMsg.receiptHandle(), msgTimeStamp, requestTimeMsSinceEpoch);
        messagesNotYetRead.add(message);
        notYetReadBytes += message.getBody().getBytes(UTF_8).length;
        inFlight.put(message.getMessageId(), new InFlightState(message.getReceiptHandle(), requestTimeMsSinceEpoch, deadlineMsSinceEpoch));
        numReceived++;
        numReceivedRecently.add(requestTimeMsSinceEpoch, 1L);
        minReceivedTimestampMsSinceEpoch.add(requestTimeMsSinceEpoch, msgTimeStamp);
        maxReceivedTimestampMsSinceEpoch.add(requestTimeMsSinceEpoch, msgTimeStamp);
        minUnreadTimestampMsSinceEpoch.add(requestTimeMsSinceEpoch, msgTimeStamp);
    }
}
Also used : ReceiveMessageRequest(software.amazon.awssdk.services.sqs.model.ReceiveMessageRequest) Message(software.amazon.awssdk.services.sqs.model.Message) ReceiveMessageResponse(software.amazon.awssdk.services.sqs.model.ReceiveMessageResponse)

Aggregations

Message (software.amazon.awssdk.services.sqs.model.Message)4 ReceiveMessageRequest (software.amazon.awssdk.services.sqs.model.ReceiveMessageRequest)4 SqsClient (software.amazon.awssdk.services.sqs.SqsClient)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 JsonMappingException (com.fasterxml.jackson.databind.JsonMappingException)1 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 RekognitionException (software.amazon.awssdk.services.rekognition.model.RekognitionException)1 CreateQueueRequest (software.amazon.awssdk.services.sqs.model.CreateQueueRequest)1 CreateQueueResponse (software.amazon.awssdk.services.sqs.model.CreateQueueResponse)1 DeleteMessageRequest (software.amazon.awssdk.services.sqs.model.DeleteMessageRequest)1 GetQueueUrlRequest (software.amazon.awssdk.services.sqs.model.GetQueueUrlRequest)1 QueueNameExistsException (software.amazon.awssdk.services.sqs.model.QueueNameExistsException)1 ReceiveMessageResponse (software.amazon.awssdk.services.sqs.model.ReceiveMessageResponse)1 SendMessageBatchRequest (software.amazon.awssdk.services.sqs.model.SendMessageBatchRequest)1 SendMessageRequest (software.amazon.awssdk.services.sqs.model.SendMessageRequest)1