value.setStringValue(context.getProperty(entry.getKey()).evaluateAttributeExpressions(flowFile).getValue()); value.setDataType("String"); request.addMessageAttributesEntry(entry.getKey().getName(), value);
@Override public void send(DataHighwayNotification notification) { String topicArn = topicArnFactory.topicArn(notification); try { final String jsonMessage = objectWriter.writeValueAsString(notification); if (jsonMessage.getBytes(Charsets.UTF_8).length > SNS_MESSAGE_SIZE_LIMIT_BYTES) { log.error("Message length exceeds SNS limit ({} bytes).", SNS_MESSAGE_SIZE_LIMIT_BYTES); } log.debug("Attempting to send message to topic '{}': {}", topicArn, jsonMessage); String message = messageFactory.message(topicArn, notification); PublishRequest request = new PublishRequest(topicArn, jsonMessage, message); request.addMessageAttributesEntry(PROTOCOL_VERSION, attributeStringValue(notification.getProtocolVersion())); request.addMessageAttributesEntry(TYPE, attributeStringValue(notification.getType().name())); request.addMessageAttributesEntry(ROAD_NAME, attributeStringValue(notification.getRoadName())); try { sns.publish(request); } catch (AmazonClientException e) { log.error("Could not publish message '{}' to SNS topic '{}'.", message, topicArn, e); } } catch (JsonProcessingException e) { log.error("Could not serialize message '{}'.", notification, e); } }
@Override protected Void run() throws Exception { final String topicArn = this.step.getTopicArn(); final String subject = this.step.getSubject(); final String message = this.step.getMessage(); final Map<String, String> messageAttributes = this.step.getMessageAttributes(); TaskListener listener = this.getContext().get(TaskListener.class); AmazonSNS snsClient = AWSClientFactory.create(AmazonSNSClientBuilder.standard(), this.getContext()); listener.getLogger().format("Publishing notification %s to %s %n", subject, topicArn); PublishRequest publishRequest = new PublishRequest() .withTopicArn(topicArn).withMessage(message).withSubject(subject); if (messageAttributes != null && !messageAttributes.isEmpty()) { for (Map.Entry<String, String> entry : messageAttributes.entrySet()) { MessageAttributeValue value = new MessageAttributeValue(); value.setStringValue(entry.getValue()); value.setDataType(STRING_DATATYPE); publishRequest.addMessageAttributesEntry(entry.getKey(), value); } } PublishResult result = snsClient.publish(publishRequest); listener.getLogger().format("Message published as %s %n", result.getMessageId()); return null; }
value.setStringValue(context.getProperty(entry.getKey()).evaluateAttributeExpressions(flowFile).getValue()); value.setDataType("String"); request.addMessageAttributesEntry(entry.getKey().getName(), value);