public void waitForJobToComplete(String jobId) {
        while (true) {
            List<Message> messages = sqs.receiveMessage(new ReceiveMessageRequest(queueUrl)).getMessages();
            for (Message message : messages) {
                String messageBody = message.getBody();
                if (!messageBody.startsWith("{")) {
                    messageBody = new String(BinaryUtils.fromBase64(messageBody));
                }

                try {
                    JsonNode json = MAPPER.readTree(messageBody);

                    String jsonMessage = json.get("Message").asText().replace("\\\"", "\"");

                    json = MAPPER.readTree(jsonMessage);
                    String messageJobId = json.get("JobId").asText();
                    String messageStatus = json.get("StatusMessage").asText();

                    // Don't process this message if it wasn't the job we were looking for
                    if (!jobId.equals(messageJobId)) continue;

                    try {
                        if (StatusCode.Succeeded.toString().equals(messageStatus)) return;
                        if (StatusCode.Failed.toString().equals(messageStatus)) {
                            throw new AmazonClientException("Archive retrieval failed");
                        }
                    } finally {
                        deleteMessage(message);
                    }
                } catch (IOException e) {
                    throw new AmazonClientException("Unable to parse status message: " + messageBody, e);
                }
            }

            sleep(1000 * 30);
        }
    }
public void waitForJobToComplete ( String jobId ) { while ( true ) { List < Message > messages = sqs . receiveMessage ( new ReceiveMessageRequest ( queueUrl ) ) . getMessages ( ) ; for ( Message message : messages ) { String messageBody = message . getBody ( ) ; if ( ! messageBody . startsWith ( "{" ) ) { messageBody = new String ( BinaryUtils . fromBase64 ( messageBody ) ) ; } try { JsonNode json = MAPPER . readTree ( messageBody ) ; String jsonMessage = json . get ( "Message" ) . asText ( ) . replace ( "\\\"" , "\"" ) ; json = MAPPER . readTree ( jsonMessage ) ; String messageJobId = json . get ( "JobId" ) . asText ( ) ; String messageStatus = json . get ( "StatusMessage" ) . asText ( ) ; if ( ! jobId . equals ( messageJobId ) ) continue ; try { if ( StatusCode . Succeeded . toString ( ) . equals ( messageStatus ) ) return ; if ( StatusCode . Failed . toString ( ) . equals ( messageStatus ) ) { throw new AmazonClientException ( "Archive retrieval failed" ) ; } } finally { deleteMessage ( message ) ; } } catch ( IOException e ) { throw new AmazonClientException ( "Unable to parse status message: " + messageBody , e ) ; } } sleep ( 1000 * 30 ) ; } }
Poll the SQS queue to see if we've received a message about the job completion yet. *