Exemple #1
0
  private void pushMessage(String msg, String key, String uuid, String version)
      throws C3PROException {
    setCredentials();
    // We send the encrypted message to the Queue. We Base64 encode it
    SendMessageRequest mse = new SendMessageRequest(AppConfig.getProp(AppConfig.AWS_SQS_URL), msg);
    System.out.println(AppConfig.getProp(AppConfig.AWS_SQS_URL));

    // Add SQS Elem metadata: encrypted symmetric key
    MessageAttributeValue atr = new MessageAttributeValue();
    atr.setStringValue(key);
    atr.setDataType("String");
    mse.addMessageAttributesEntry(AppConfig.getProp(AppConfig.SECURITY_METADATAKEY), atr);

    // Add SQS Elem metadata: public key uuid
    atr = new MessageAttributeValue();
    atr.setStringValue(uuid);
    atr.setDataType("String");
    mse.addMessageAttributesEntry(AppConfig.getProp(AppConfig.SECURITY_METADATAKEYID), atr);

    atr = new MessageAttributeValue();
    atr.setStringValue(version);
    atr.setDataType("String");
    mse.addMessageAttributesEntry(AppConfig.getProp(AppConfig.FHIR_METADATA_VERSION), atr);

    try {
      this.sqs.sendMessage(mse);
    } catch (Exception e) {
      e.printStackTrace();
      throw new C3PROException(e.getMessage(), e);
    }
  }
  @Test
  public void shouldSendMessage() {
    String message = "A message";
    provider.send(message);

    ArgumentCaptor<SendMessageRequest> argument = ArgumentCaptor.forClass(SendMessageRequest.class);
    verify(sqsClient, times(1)).sendMessage(argument.capture());

    SendMessageRequest request = argument.getValue();
    assertThat(request.getMessageBody(), equalTo(message));
  }
  public Request<SendMessageRequest> marshall(SendMessageRequest sendMessageRequest) {

    if (sendMessageRequest == null) {
      throw new AmazonClientException("Invalid argument passed to marshall(...)");
    }

    Request<SendMessageRequest> request =
        new DefaultRequest<SendMessageRequest>(sendMessageRequest, "AmazonSQS");
    request.addParameter("Action", "SendMessage");
    request.addParameter("Version", "2012-11-05");

    if (sendMessageRequest.getQueueUrl() != null) {
      request.addParameter("QueueUrl", StringUtils.fromString(sendMessageRequest.getQueueUrl()));
    }
    if (sendMessageRequest.getMessageBody() != null) {
      request.addParameter(
          "MessageBody", StringUtils.fromString(sendMessageRequest.getMessageBody()));
    }
    if (sendMessageRequest.getDelaySeconds() != null) {
      request.addParameter(
          "DelaySeconds", StringUtils.fromInteger(sendMessageRequest.getDelaySeconds()));
    }

    return request;
  }
Exemple #4
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();
    }
  }