Share
Embed
Transcript
18 slides
-
CSC 5100 Modern Software Engineering Lecture 15. Cloud Messaging ::
SNS vs SQS Dr. Javier Gonzalez-Sanchez [email protected] www.javiergs.info ffi o ce: 14 -227 -
You already know Pub/Sub: MQTT Subscriber A Publisher IoT Sensor
MQTT Topic Subscriber B Subscriber C One publication can be delivered to multiple subscribers. CSC 5100 • 4 3 -
Amazon Simple Noti ication Service (SNS) Lambda SNS Topic Publisher
SQS HTTP / other Same Pub/Sub idea as MQTT; different technology and environment. 4 f CSC 5100 • 5 -
Amazon Simple Queue Service (SQS) SQS Queue Producer Consumer [M1]
[M2] [M3] Messages wait until a consumer receives and processes them. SQS asks: “Who can process this message/work?” CSC 5100 • 6 5 -
Our existing Temperature system DynamoDB MQTT IoT Sensor AWS IoT
Core TemperatureLambda Amazon S3 Simple Storage Service Now we add communication from one cloud component to another. CSC 5100 • 8 6 -
New requirements • Notify another cloud component whenever a new
temperature reading arrives. • Send every reading to a local Java application that plots temperature over time. • Do not make the IoT sensor wait for either activity. Two different communication problems → use two different messaging patterns. CSC 5100 • 9 7 -
Final architecture SNS Topic IoT Sensor IoT Core TemperatureLambda DynamoDB
/ S3 SQS Queue SNS = event notification NotificationLambda • Java Plot App SQS = queued message CSC 5100 • 10 8 -
One reading, three actions // TemperatureLambda receives one reading saveToDynamoDB(reading);
saveToS3(reading); publishToSNS(json); sendToSQS(json); // EVENT: a reading happened // MESSAGE: hold for Java Plot CSC 5100 • 11 9 -
AWS Console 1: create the SNS path • Create an
SNS topic: TemperatureEvents. • Copy the Topic ARN (Resource Name) — the publisher needs this identifier. • Create NotificationLambda. • Add TemperatureEvents as an SNS trigger/subscription for NotificationLambda. TemperatureEvents SNS Topic NotificationLambda CSC 5100 • 13 10 -
AWS Console 2: create the SQS path • Create a
Standard queue: TemperaturePlotQueue. • Copy the Queue URL — producer and desktop consumer use it. • Keep the first example simple: use the default queue settings. • The Java Plot app will poll this queue. TemperaturePlotQueue SQS Java Plot App CSC 5100 • 14 11 -
Java SDK modules <dependency> <groupId>software.amazon.awssdk</groupId> <artifactId>sns</artifactId> <version>2.39.4</version> </dependency> <dependency> <groupId>software.amazon.awssdk</groupId>
<artifactId>sqs</artifactId> <version>2.39.4</version> </dependency> Same AWS SDK for Java model as DynamoDB and S3. CSC 5100 • 16 12 -
TemperatureLambda: publish to SNS SnsClient sns = SnsClient.builder().build(); sns.publish(PublishRequest.builder() .topicArn(SNS_TOPIC_ARN)
.message(json) .build()); TemperatureLambda SNS Topic NotificationLambda CSC 5100 • 17 13 -
Noti icationLambda receives the SNS event public class NotificationLambda implements
RequestHandler<SNSEvent, Void> { public void handleRequest(SNSEvent event, Context context) { for (SNSEvent.SNSRecord record : event.getRecords()) { String json = record.getSNS().getMessage(); System.out.println(json); } return null; } } SNS pushes the notification to Lambda. 14 f CSC 5100 • 19 -
TemperatureLambda: send to SQS SqsClient sqs = SqsClient.builder().build(); sqs.sendMessage(SendMessageRequest.builder() .queueUrl(SQS_QUEUE_URL)
.messageBody(json) .build()); TemperatureLambda SQS Queue Java Plot CSC 5100 • 18 15 -
Java Plot app reads the SQS queue ReceiveMessageResponse response =
sqs.receiveMessage(r -> r.queueUrl(SQS_QUEUE_URL).waitTimeSeconds(10)); for (Message message : response.messages()) { TemperatureReading reading = mapper.readValue(message.body(),TemperatureReading.class); chart.add(reading.timestamp(), reading.temperature()); deleteMessage(message); } RECEIVE → PARSE → PLOT → DELETE CSC 5100 • 20 16 -
CSC 5100 Modern Software Engineering Javier Gonzalez-Sanchez, Ph.D. [email protected] Fall
2026 Copyright. These slides may be used only as study material for CSC 5100 within the California State University system. They may not be distributed or used for any other purpose.