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;
    }
예제 #2
0
파일: Utils.java 프로젝트: oryband/homework
  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());
    }
  }
예제 #3
0
  @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;
  }
예제 #4
0
  @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]);
          }
        }
      }
    }
  }
예제 #5
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();
    }
  }