Search in sources :

Example 1 with DeleteMessageRequest

use of software.amazon.awssdk.services.sqs.model.DeleteMessageRequest 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)

Aggregations

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 DeleteMessageRequest (software.amazon.awssdk.services.sqs.model.DeleteMessageRequest)1 Message (software.amazon.awssdk.services.sqs.model.Message)1 ReceiveMessageRequest (software.amazon.awssdk.services.sqs.model.ReceiveMessageRequest)1