|
| 1 | +/* |
| 2 | + * Copyright 2013-2025 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package io.awspring.cloud.sqs.sample; |
| 17 | + |
| 18 | +import io.awspring.cloud.sqs.annotation.SqsListener; |
| 19 | +import io.awspring.cloud.sqs.operations.SqsTemplate; |
| 20 | +import io.awspring.cloud.sqs.support.converter.SnsNotification; |
| 21 | +import org.slf4j.Logger; |
| 22 | +import org.slf4j.LoggerFactory; |
| 23 | +import org.springframework.boot.ApplicationRunner; |
| 24 | +import org.springframework.context.annotation.Bean; |
| 25 | +import org.springframework.stereotype.Component; |
| 26 | + |
| 27 | +/** |
| 28 | + * Sample demonstrating how to receive SNS notifications from SQS. |
| 29 | + * |
| 30 | + * @author Damien Chomat |
| 31 | + */ |
| 32 | +@Component |
| 33 | +public class SnsNotificationSample { |
| 34 | + |
| 35 | + private static final Logger LOGGER = LoggerFactory.getLogger(SnsNotificationSample.class); |
| 36 | + |
| 37 | + /** |
| 38 | + * Receives SNS notifications from the "sns-notification-custom-queue" SQS queue. The message payload is |
| 39 | + * automatically converted to a CustomMessage object. |
| 40 | + * |
| 41 | + * @param notification the SNS notification wrapper containing the message and metadata |
| 42 | + */ |
| 43 | + @SqsListener("sns-notification-custom-queue") |
| 44 | + public void receiveCustomMessage(SnsNotification<CustomMessage> notification) { |
| 45 | + LOGGER.info("Received SNS notification with ID: {}", notification.getMessageId()); |
| 46 | + LOGGER.info("From topic: {}", notification.getTopicArn()); |
| 47 | + notification.getSubject().ifPresent(subject -> LOGGER.info("Subject: {}", subject)); |
| 48 | + |
| 49 | + CustomMessage message = notification.getMessage(); |
| 50 | + LOGGER.info("Message content: {}", message.content()); |
| 51 | + LOGGER.info("Message timestamp: {}", message.timestamp()); |
| 52 | + |
| 53 | + LOGGER.info("Notification timestamp: {}", notification.getTimestamp()); |
| 54 | + LOGGER.info("Message attributes: {}", notification.getMessageAttributes()); |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * ApplicationRunner to send sample SNS messages to the queues for demonstration purposes. This simulates SNS |
| 59 | + * notifications being sent to SQS queues that are subscribed to SNS topics. |
| 60 | + */ |
| 61 | + @Bean |
| 62 | + public ApplicationRunner sendSnsNotificationMessage(SqsTemplate sqsTemplate) { |
| 63 | + return args -> { |
| 64 | + // Simulate an SNS notification for an order processing topic |
| 65 | + String orderNotificationMessage = """ |
| 66 | + { |
| 67 | + "Type": "Notification", |
| 68 | + "MessageId": "order-12345-notification", |
| 69 | + "TopicArn": "arn:aws:sns:us-east-1:123456789012:order-processing-topic", |
| 70 | + "Subject": "Order Processing Update", |
| 71 | + "Message": "{\\"content\\": \\"Order #12345 has been processed successfully\\", \\"timestamp\\": 1672531200000}", |
| 72 | + "Timestamp": "2023-01-01T12:00:00Z", |
| 73 | + "MessageAttributes": { |
| 74 | + "eventType": { |
| 75 | + "Type": "String", |
| 76 | + "Value": "ORDER_PROCESSED" |
| 77 | + }, |
| 78 | + "priority": { |
| 79 | + "Type": "String", |
| 80 | + "Value": "high" |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | + """; |
| 85 | + |
| 86 | + LOGGER.info("Sending SNS notification messages to subscribed SQS queue..."); |
| 87 | + sqsTemplate.send("sns-notification-custom-queue", orderNotificationMessage); |
| 88 | + }; |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * A custom message record for demonstration purposes. |
| 93 | + */ |
| 94 | + public record CustomMessage(String content, long timestamp) { |
| 95 | + } |
| 96 | +} |
0 commit comments