Thread.sleep(500);
ReceiveMessageRequest receiveMessageRequest = new ReceiveMessageRequest();
receiveMessageRequest.setQueueUrl(queueUrl);
receiveMessageRequest.setMaxNumberOfMessages(1);
ReceiveMessageResult receiveMessageResult = sqs.receiveMessage(receiveMessageRequest);
List<Message> messages = receiveMessageResult.getMessages();
if (messages != null && messages.size() == 1) {
JSONObject o = new JSONObject(messages.get(0).getBody());
if (!o.has("SubscribeURL")) {
throw new Exception("message is not a confirmation messsage");
}
String subscriptionUrl = o.getString("SubscribeURL");
httpGet(subscriptionUrl);
DeleteMessageRequest deleteMessageRequest = new DeleteMessageRequest();
deleteMessageRequest.setReceiptHandle(messages.get(0).getReceiptHandle());
deleteMessageRequest.setQueueUrl(queueUrl);
sqs.deleteMessage(deleteMessageRequest);
} else {
throw new Exception("no confirmation message found");
}
}
// publish and receive message
PublishRequest publishRequest = new PublishRequest();
String messageText = "quamvis sint sub aqua, sub aqua maledicere temptant";
publishRequest.setMessage(messageText);
publishRequest.setSubject("unit test message");
publishRequest.setTopicArn(topicArn);
sns.publish(publishRequest);
Thread.sleep(500);
ReceiveMessageRequest receiveMessageRequest = new ReceiveMessageRequest();
receiveMessageRequest.setQueueUrl(queueUrl);
receiveMessageRequest.setMaxNumberOfMessages(1);
ReceiveMessageResult receiveMessageResult = sqs.receiveMessage(receiveMessageRequest);
List<Message> messages = receiveMessageResult.getMessages();
if (messages != null && messages.size() == 1) {
String messageBody = messages.get(0).getBody();