Пример #1
0
  @Override
  public void onTrigger(final ProcessContext context, final ProcessSession session) {
    FlowFile flowFile = session.get();
    if (flowFile == null) {
      return;
    }

    final long startNanos = System.nanoTime();
    final AmazonSQSClient client = getClient();
    final SendMessageBatchRequest request = new SendMessageBatchRequest();
    final String queueUrl =
        context.getProperty(QUEUE_URL).evaluateAttributeExpressions(flowFile).getValue();
    request.setQueueUrl(queueUrl);

    final Set<SendMessageBatchRequestEntry> entries = new HashSet<>();

    final SendMessageBatchRequestEntry entry = new SendMessageBatchRequestEntry();
    entry.setId(flowFile.getAttribute("uuid"));
    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    session.exportTo(flowFile, baos);
    final String flowFileContent = baos.toString();
    entry.setMessageBody(flowFileContent);

    final Map<String, MessageAttributeValue> messageAttributes = new HashMap<>();

    for (final PropertyDescriptor descriptor : userDefinedProperties) {
      final MessageAttributeValue mav = new MessageAttributeValue();
      mav.setDataType("String");
      mav.setStringValue(
          context.getProperty(descriptor).evaluateAttributeExpressions(flowFile).getValue());
      messageAttributes.put(descriptor.getName(), mav);
    }

    entry.setMessageAttributes(messageAttributes);
    entry.setDelaySeconds(context.getProperty(DELAY).asTimePeriod(TimeUnit.SECONDS).intValue());
    entries.add(entry);

    request.setEntries(entries);

    try {
      client.sendMessageBatch(request);
    } catch (final Exception e) {
      getLogger()
          .error(
              "Failed to send messages to Amazon SQS due to {}; routing to failure",
              new Object[] {e});
      flowFile = session.penalize(flowFile);
      session.transfer(flowFile, REL_FAILURE);
      return;
    }

    getLogger()
        .info("Successfully published message to Amazon SQS for {}", new Object[] {flowFile});
    session.transfer(flowFile, REL_SUCCESS);
    final long transmissionMillis = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startNanos);
    session.getProvenanceReporter().send(flowFile, queueUrl, transmissionMillis);
  }
Пример #2
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);
    }
  }