private static void createResources() {
    ClasspathPropertiesFileCredentialsProvider provider =
        new ClasspathPropertiesFileCredentialsProvider();

    AmazonSQS sqs = new AmazonSQSClient(provider);
    sqs.setEndpoint(SQS_ENDPOINT);
    sqs.createQueue(new CreateQueueRequest().withQueueName(SQS_QUEUE));

    AmazonS3 s3 = new AmazonS3Client(provider);
    if (!s3.doesBucketExist(BUCKET_NAME)) {
      s3.createBucket(new CreateBucketRequest(BUCKET_NAME));
    }

    AmazonDynamoDBClient dynamo = new AmazonDynamoDBClient(provider);
    dynamo.setEndpoint(DYNAMO_ENDPOINT);

    if (!doesTableExist(dynamo, DYNAMO_TABLE_NAME)) {
      dynamo.createTable(
          new CreateTableRequest()
              .withTableName(DYNAMO_TABLE_NAME)
              .withProvisionedThroughput(
                  new ProvisionedThroughput()
                      .withReadCapacityUnits(50l)
                      .withWriteCapacityUnits(50l))
              .withKeySchema(
                  new KeySchema()
                      .withHashKeyElement(
                          new KeySchemaElement()
                              .withAttributeName("id")
                              .withAttributeType(ScalarAttributeType.S))));
      waitForTableToBecomeAvailable(dynamo, DYNAMO_TABLE_NAME);
    }
  }
Exemplo n.º 2
0
 public static void sendMessage(AmazonSQS sqs, String sqsUrl, String info) {
   try {
     sqs.sendMessage(new SendMessageRequest(sqsUrl, info));
     logger.info("Message sent to queue: " + info);
   } catch (AmazonServiceException e) {
     logger.severe(e.getMessage());
   } catch (AmazonClientException e) {
     logger.severe(e.getMessage());
   }
 }
Exemplo n.º 3
0
  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());
    }
  }
Exemplo n.º 5
0
  public static List<Message> getMessages(ReceiveMessageRequest req, AmazonSQS sqs) {
    logger.fine("Getting SQS messages.");

    List<Message> msgs;

    try {
      msgs = sqs.receiveMessage(req).getMessages();
    } catch (AmazonServiceException e) {
      logger.severe(e.getMessage());
      return null;
    } catch (AmazonClientException e) {
      logger.severe(e.getMessage());
      return null;
    }

    return msgs;
  }
Exemplo n.º 6
0
 private void setCredentials() throws C3PROException {
   if (this.sqs == null) {
     AWSCredentials credentials = null;
     try {
       System.setProperty("aws.profile", AppConfig.getProp(AppConfig.AWS_SQS_PROFILE));
       System.out.println(AppConfig.getProp(AppConfig.AWS_SQS_PROFILE));
       credentials = new ProfileCredentialsProvider().getCredentials();
     } catch (Exception e) {
       e.printStackTrace();
       throw new C3PROException(
           "Cannot load the credentials from the credential profiles file. "
               + "Please make sure that the credentials file is at the correct "
               + "location (~/.aws/credentials), and is in valid format.",
           e);
     }
     this.sqs = new AmazonSQSClient(credentials);
     System.out.println(AppConfig.getProp(AppConfig.AWS_SQS_REGION));
     Region usWest2 =
         Region.getRegion(Regions.fromName(AppConfig.getProp(AppConfig.AWS_SQS_REGION)));
     sqs.setRegion(usWest2);
   }
 }
Exemplo n.º 7
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();
    }
  }
Exemplo n.º 8
0
 // Get approximate number of the messages from queue.
 public static int getNumberOfMessages(AmazonSQS sqs, String url) {
   List<String> attr = new ArrayList<String>();
   attr.add("ApproximateNumberOfMessages");
   GetQueueAttributesResult res = sqs.getQueueAttributes(url, attr);
   return Integer.parseInt(res.getAttributes().get("ApproximateNumberOfMessages"));
 }