From c7bc6716e1a93a21c8239ac4608238e7df448afa Mon Sep 17 00:00:00 2001 From: m c <458905+goatrocks@users.noreply.github.com> Date: Tue, 13 Jul 2021 09:07:09 -0400 Subject: [PATCH] Update SQSExample.java --- examples/SQSExample.java | 34 +++++++++++++++++++++++----------- 1 file changed, 23 insertions(+), 11 deletions(-) diff --git a/examples/SQSExample.java b/examples/SQSExample.java index cd4a654..b1a8e2b 100644 --- a/examples/SQSExample.java +++ b/examples/SQSExample.java @@ -52,19 +52,32 @@ public static void main(String[] args) throws InterruptedException, IOException .build(); //request the messages - ReceiveMessageResponse messages = sqsClient.receiveMessage(request); + var messages = sqsClient.receiveMessage(request); while(true) { // iterate the messages from SQS for (Message m : messages.messages()) { //fetch receipt handle and body String rh = m.receiptHandle(); - JSONObject body = new JSONObject(m.body()); - String status = body.getString("status"); - String url = body.getString("result_url"); - Integer id = body.getInt("submission_id"); + //handle the fact that certain SQS queues + //may escpae JSON even with raw value setting + if(m.body().startsWith("\"")) { + StringBuilder sb = new StringBuilder(m.body()); + sb.deleteCharAt(0); + while (sb.indexOf("\\") > -1) { + int indx = sb.indexOf("\\"); + sb.deleteCharAt(indx); + } + sb.deleteCharAt(sb.length() - 1); + bod = sb.toString(); + } + + JSONObject body = new JSONObject(bod); + var status = body.getString("status"); + var url = body.getString("result_url"); + var id = body.getInt("submission_id"); System.out.println(body); - if(status.equalsIgnoreCase("COMPLETE")){ + if(status.equalsIgnoreCase("COMPLETED")){ System.out.println("Completed: " + id); //process a completed message process_result(indicoClient, id, url); @@ -73,7 +86,7 @@ public static void main(String[] args) throws InterruptedException, IOException System.out.print("Submission failed: " + id); } //remove message when processed.. - DeleteMessageRequest delmsg = DeleteMessageRequest.builder() + var delmsg = DeleteMessageRequest.builder() .queueUrl(queueUrl) .receiptHandle(rh) .build(); @@ -88,15 +101,14 @@ public static void main(String[] args) throws InterruptedException, IOException public static void process_result(IndicoClient indicoClient, int id, String url) throws IOException { System.out.println("get blob...."); - RetrieveBlob ret_storage_obj = indicoClient.retrieveBlob(); + var ret_storage_obj = indicoClient.retrieveBlob(); ret_storage_obj.url(url); ret_storage_obj.execute(); System.out.println("update sub...."); - UpdateSubmission update_sub = indicoClient.updateSubmission(); + var update_sub = indicoClient.updateSubmission(); update_sub.submissionId(id); update_sub.retrieved(true); update_sub.execute(); - } public static void submit_workflow(IndicoClient indicoClient){ @@ -105,7 +117,7 @@ public static void submit_workflow(IndicoClient indicoClient){ String pdf = PDF_LOCATION; List files = new ArrayList(); files.add(pdf); - WorkflowSubmission submission = indicoClient.workflowSubmission(); + var submission = indicoClient.workflowSubmission(); submission.files(files); submission.workflowId(workflow_id); submission.execute();