private List<JsonNode> pullMessages(int interval) { List<JsonNode> msgs = new ArrayList<JsonNode>(); if (!isBlank(QUEUE_URL)) { try { ReceiveMessageRequest receiveReq = new ReceiveMessageRequest(QUEUE_URL); receiveReq.setMaxNumberOfMessages(MAX_MESSAGES); receiveReq.setWaitTimeSeconds(interval); List<Message> list = sqs.receiveMessage(receiveReq).getMessages(); if (list != null && !list.isEmpty()) { if (DEBUG) logger.info("Received {} messages from queue.", list.size()); List<DeleteMessageBatchRequestEntry> deletionList = new ArrayList<DeleteMessageBatchRequestEntry>(); for (Message message : list) { if (!isBlank(message.getBody())) { msgs.add(mapper.readTree(message.getBody())); } deletionList.add( new DeleteMessageBatchRequestEntry( UUID.randomUUID().toString(), message.getReceiptHandle())); } sqs.deleteMessageBatch(QUEUE_URL, deletionList); } } catch (IOException ex) { logger.error(ex.getMessage()); } catch (AmazonServiceException ase) { logException(ase); } catch (AmazonClientException ace) { logger.error("Could not reach SQS. {}", ace.getMessage()); } } return msgs; }
public static void deleteTaskMessage(Message msg, String sqsUrl, AmazonSQS sqs) { String handle = msg.getReceiptHandle(); try { sqs.deleteMessage(new DeleteMessageRequest(sqsUrl, handle)); logger.info("Message deleted: " + msg.getBody()); } catch (AmazonServiceException e) { logger.severe(e.getMessage()); } catch (AmazonClientException e) { logger.severe(e.getMessage()); } }
/** * @param args * @throws IOException */ public static void main(String[] args) throws IOException { try { String myQueueUrl = "https://queue.amazonaws.com/034307772076/MyQueue"; AmazonSQS sqs = new AmazonSQSClient( new PropertiesCredentials( SimpleQueueServiceSample.class.getResourceAsStream("AwsCredentials.properties"))); System.out.println("Sending message 1."); sqs.sendMessage(new SendMessageRequest(myQueueUrl, "Test message.")); System.out.println("Receiving messages:"); ReceiveMessageRequest receiveMessageRequest = new ReceiveMessageRequest(myQueueUrl); List<Message> messages = sqs.receiveMessage(receiveMessageRequest).getMessages(); for (Message message : messages) { System.out.print(message.getMessageId()); System.out.println(" : " + message.getBody()); } } catch (AmazonServiceException ase) { System.out.println( "Caught an AmazonServiceException, which means your request made it " + "to Amazon SQS, but was rejected with an error response for some reason."); System.out.println("Error Message: " + ase.getMessage()); System.out.println("HTTP Status Code: " + ase.getStatusCode()); System.out.println("AWS Error Code: " + ase.getErrorCode()); System.out.println("Error Type: " + ase.getErrorType()); System.out.println("Request ID: " + ase.getRequestId()); } catch (AmazonClientException ace) { System.out.println( "Caught an AmazonClientException, which means the client encountered " + "a serious internal problem while trying to communicate with SQS, such as not " + "being able to access the network."); System.out.println("Error Message: " + ace.getMessage()); } }
@Override public void run() { while (true) { // take element from request queue List<Message> messages = this.sqsImplementation.retrrieveElementFromQueue(this.taskQueue); if (null != messages && messages.size() > 0) { for (Message message : messages) { String[] messageParts = message.getBody().split(" "); // check for entry in a dynamoDB table boolean isEntryExist = this.dbImpl.checkIfEntryExist(this.tableName, messageParts[0]); if (!isEntryExist) { // insert task in a dynamoDB table this.dbImpl.insertElementInTable(tableName, messageParts[0]); try { Thread.sleep(Integer.parseInt(messageParts[1])); } catch (NumberFormatException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } // insert element in response queue this.sqsImplementation.insertElementInQueue(responseQueue, messageParts[0] + " 0"); // Delete a message from a request queue String messageReceiptHandle = message.getReceiptHandle(); this.sqsImplementation .getSqs() .deleteMessage( new DeleteMessageRequest( sqsImplementation.getQueueURL(this.taskQueue), messageReceiptHandle)); } else { System.out.println("Duplicate Entry Found" + messageParts[0]); } } } } }
public static void main(String[] args) { // Get Credentials AWSCredentials credentials = Credentials.getCredentials(); // Get SQS client AmazonSQS sqs = new AmazonSQSClient(credentials); // Start processing the messages from inbox queue for (; ; ) { // Receive messages System.out.println("Receiving messages from Inbox Queue.\n"); ReceiveMessageRequest receiveMessageRequest = new ReceiveMessageRequest(inboxQueueUrl); List<Message> messages = sqs.receiveMessage(receiveMessageRequest).getMessages(); if (messages.isEmpty()) { continue; } else { for (Message message : messages) { // temp variable to handle the integers in the message // MessageAttributeValue a=null; // MessageAttributeValue b=null; String a = ""; String b = ""; System.out.println(" Message"); System.out.println(" MessageId: " + message.getMessageId()); System.out.println(" ReceiptHandle: " + message.getReceiptHandle()); System.out.println(" MD5OfBody: " + message.getMD5OfBody()); System.out.println(" Body: " + message.getBody()); String receiptHandle = message.getReceiptHandle(); String[] numbers = message.getBody().split(":"); /*Map<String, MessageAttributeValue> e = message.getMessageAttributes(); //for (Entry<String, MessageAttributeValue> entry: e.entrySet()) { for(Entry<String, String> entry : message.getAttributes().entrySet()){ System.out.println(" Attribute"); String temp1="firstNumber"; String temp2="secondNumber"; if(temp1.compareTo(entry.getKey())==0){ a=entry.getValue().toString(); } if(temp2.compareTo(entry.getKey())==0){ b=entry.getValue().toString(); } System.out.println(" Name: " + entry.getKey()); System.out.println(" Value: " + entry.getValue()); } */ // process the numbers String sum = numbers[0] + "+" + numbers[1]; String diff = numbers[0] + "-" + numbers[1]; System.out.println(sum); System.out.println(diff); Map<String, MessageAttributeValue> messageAttributes = new HashMap<>(); messageAttributes.put( "sum", new MessageAttributeValue().withDataType("String").withStringValue(sum)); messageAttributes.put( "diff", new MessageAttributeValue().withDataType("String").withStringValue(diff)); SendMessageRequest request = new SendMessageRequest(); request.withMessageBody("A test message body."); request.withQueueUrl(outboxQueueUrl); request.withMessageAttributes(messageAttributes); sqs.sendMessage(request); // delete the message from inbox queue sqs.deleteMessage(new DeleteMessageRequest(inboxQueueUrl, receiptHandle)); } } System.out.println(); } }
/** Convert received SQSMessage into ObjectMessage */ public SQSObjectMessage(Acknowledger acknowledger, String queueUrl, Message sqsMessage) throws JMSException { super(acknowledger, queueUrl, sqsMessage); body = sqsMessage.getBody(); }
@Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (obj instanceof Message == false) return false; Message other = (Message) obj; if (other.getMessageId() == null ^ this.getMessageId() == null) return false; if (other.getMessageId() != null && other.getMessageId().equals(this.getMessageId()) == false) return false; if (other.getReceiptHandle() == null ^ this.getReceiptHandle() == null) return false; if (other.getReceiptHandle() != null && other.getReceiptHandle().equals(this.getReceiptHandle()) == false) return false; if (other.getMD5OfBody() == null ^ this.getMD5OfBody() == null) return false; if (other.getMD5OfBody() != null && other.getMD5OfBody().equals(this.getMD5OfBody()) == false) return false; if (other.getBody() == null ^ this.getBody() == null) return false; if (other.getBody() != null && other.getBody().equals(this.getBody()) == false) return false; if (other.getAttributes() == null ^ this.getAttributes() == null) return false; if (other.getAttributes() != null && other.getAttributes().equals(this.getAttributes()) == false) return false; return true; }