You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Add Spring Integration channel adapters for SNS (#1473)
* Add Spring Integration channel adapters for SNS
* Add `spring-integration-http` as an optional dep into `spring-cloud-aws-sns`.
* Add `SnsInboundChannelAdapter` to consume SNS notification over HTTP(S)
* Add `SnsMessageHandler` to publish to an SNS topic.
* The `SnsHeaderMapper` and `SnsBodyBuilder` are supporting classes for various SNS publish scenarios
* The `SnsRequestFailureException` is a generic exception with a message a and request context
to throw when publication fails
* Add extra useful `SnsHeaders` constant for some Spring Integration message headers
* Since `SnsMessageHandler` is based on the `SnsAsyncClient`, introduce an `SnsAsyncTopicArnResolver`
for such an async use-case
* Add `spring-cloud-aws-starter-integration-sns`
* Document this new feature
* Add missed `spring-cloud-aws-starter-integration-sqs` module int the root pom
* * Fix typos in docs and Javadocs for SI SNS
* Simplify `try..catch` logic in the `SnsMessageHandler` and add missed `currentThread().interrupt()`
* Add `.fifo` logic to the `SnsAsyncTopicArnResolver`
Copy file name to clipboardExpand all lines: docs/src/main/asciidoc/sns.adoc
+90Lines changed: 90 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -296,3 +296,93 @@ Sample IAM policy granting access to SNS:
296
296
]
297
297
}
298
298
----
299
+
300
+
=== Spring Integration Support
301
+
302
+
Starting with version 4.0, Spring Cloud AWS provides https://spring.io/projects/spring-integration[Spring Integration] channel adapters for Amazon SNS.
303
+
304
+
The `SnsInboundChannelAdapter` is an extension of `HttpRequestHandlingMessagingGateway` and must be as a part of Spring MVC application.
305
+
Its URL must be used from the AWS Management Console to add this endpoint as a subscriber to the SNS Topic.
306
+
However, before receiving any notification itself, this HTTP endpoint must confirm the subscription.
307
+
308
+
See `SnsInboundChannelAdapter` JavaDocs for more information.
309
+
310
+
An important option of this adapter to consider is `handleNotificationStatus`.
311
+
This `boolean` flag indicates if the adapter should send `SubscriptionConfirmation/UnsubscribeConfirmation` message to the `output-channel` or not.
312
+
If that is a case, the `SnsHeaders.NOTIFICATION_STATUS_HEADER` message header is present in the message with the `NotificationStatus` object, which can be used in the downstream flow to confirm subscription or not.
313
+
Or "re-confirm" it in the case of `UnsubscribeConfirmation` message.
314
+
315
+
In addition, the `SnsHeaders.SNS_MESSAGE_TYPE_HEADER` message header is represented to simplify a routing in the downstream flow.
316
+
317
+
The Java Configuration is pretty simple:
318
+
319
+
[source,java]
320
+
----
321
+
@SpringBootApplication
322
+
public static class MyConfiguration {
323
+
324
+
@Autowired
325
+
private SnsClient amazonSns;
326
+
327
+
@Bean
328
+
public PollableChannel inputChannel() {
329
+
return new QueueChannel();
330
+
}
331
+
332
+
@Bean
333
+
public HttpRequestHandler sqsMessageDrivenChannelAdapter(PollableChannel inputChannel) {
334
+
SnsInboundChannelAdapter adapter = new SnsInboundChannelAdapter(this.amazonSns, "/mySampleTopic");
335
+
adapter.setRequestChannel(inputChannel);
336
+
adapter.setHandleNotificationStatus(true);
337
+
return adapter;
338
+
}
339
+
}
340
+
----
341
+
342
+
Note: by default, the message `payload` is a `Map` converted from the received Topic JSON message.
343
+
For the convenience a `payload-expression` is provided with the `Message` as a root object of the evaluation context.
344
+
Hence, even some HTTP headers, populated by the `DefaultHttpHeaderMapper`, are available for the evaluation context.
345
+
346
+
347
+
The `SnsMessageHandler` is a simple one-way Outbound Channel Adapter to send Topic Notification using `SnsAsyncClient` service.
348
+
349
+
This Channel Adapter (`MessageHandler`) accepts these options:
350
+
351
+
- `topic-arn` (`topic-arn-expression`) - the SNS Topic to send notification for.
352
+
- `subject` (`subject-expression`) - the SNS Notification Subject;
353
+
- `body-expression` - the SpEL expression to evaluate the `message` property for the `software.amazon.awssdk.services.sns.model.PublishRequest`.
354
+
- `resource-id-resolver` - a `ResourceIdResolver` bean reference to resolve logical topic names to physical resource ids;
355
+
356
+
See `SnsMessageHandler` JavaDocs for more information.
357
+
358
+
The Java Config looks like:
359
+
360
+
[source,java]
361
+
----
362
+
@Bean
363
+
public MessageHandler snsMessageHandler(SnsAsyncClient amazonSns) {
364
+
SnsMessageHandler handler = new SnsMessageHandler(amazonSns);
NOTE: the `bodyExpression` can be evaluated to a `io.awspring.cloud.sns.integration.SnsBodyBuilder` allowing the configuration of a `json` `messageStructure` for the `PublishRequest` and provide separate messages for different protocols.
378
+
The same `SnsBodyBuilder` rule is applied for the raw `payload` if the `bodyExpression` hasn't been configured.
379
+
380
+
NOTE: if the `payload` of `requestMessage` is a `software.amazon.awssdk.services.sns.model.PublishRequest` already, the `SnsMessageHandler` doesn't do anything with it, and it is sent as-is.
381
+
382
+
The `SnsMessageHandler` can be configured with the `HeaderMapper` to map message headers to the SNS message attributes.
383
+
See `SnsHeaderMapper` implementation for more information and also consult with https://docs.aws.amazon.com/sns/latest/dg/SNSMessageAttributes.html[Amazon SNS Message Attributes] about value types and restrictions.
384
+
385
+
The `SnsMessageHandler` supports sending to SNS FIFO topics using the `messageGroupId`/`messageGroupIdExpression` and `messageDeduplicationIdExpression` properties.
386
+
387
+
The Spring Integration dependency in the `spring-cloud-aws-sns` module is `optional` to avoid unnecessary artifacts on classpath when Spring Integration is not used.
388
+
For convenience, a dedicated `spring-cloud-aws-starter-integration-sns` is provided managing all the required dependencies for Spring Integration support with Amazon SNS.
0 commit comments