Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

Cloud Computing

8 th laboratory

Application Integration – Amazon Simple Notification Service (SNS)

To read:
https://docs.aws.amazon.com/sns/

https://docs.aws.amazon.com/sns/latest/dg/welcome.html

To read and test examples:


https://docs.aws.amazon.com/sns/latest/dg/sns-getting-started.html

Tasks to complete:
Create a topic named MyTopic and assign two subscriptions for it: one for your email (you will have to
confirm it – you will receive a confirmation email first) and one for your phone number (no confirmation
needed in this case) one for another email (it seems that AWS no longer supports sending SMS messages
without extra configuration).

Publish a test message to the topic using the "Publish Message" button in AWS console. You should
receive this message on both subscriptions.

Publish a message programmatically: create a java application that publishes a message to this topic as
described in the following section.

Creating a Java project that uses SNS to post a message to a topic

For this part is recommended to use an AWS EC2 instance and to update the credentials file from ~/.aws
with the information from Learner Lab - Foundational Services or directly
https://labs.vocareum.com/main/, go to AWS Details from the top menu bar, where it says Cloud Access
AWS CLI click on Show.

If the following tutorial is done from outside an EC2 instance, then the credentials must be configured
(e.g., from the Java application - https://docs.aws.amazon.com/sdk-for-java/v1/developer-
guide/credentials.html).

To read:
https://docs.aws.amazon.com/sdk-for-java/v2/developer-guide/setup-project-maven.html
yum install maven
Create a directory and cd into it. Issue the following maven command to generate an empty java
project:
mvn archetype:generate -B \

-DarchetypeGroupId=pl.org.miki \

-DarchetypeArtifactId=java8-quickstart-archetype \

-DarchetypeVersion=1.0.0 \

-DgroupId=com.example \

-DartifactId=sdk-sandbox \

-Dversion=1.0 \

-Dpackage=com.example

Go into the generated project directory. Edit the file pom.xml and add the following immediately above
<dependencies> (this sets the AWS SDK project dependency):

<dependencyManagement>
<dependencies>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>bom</artifactId>
<version>X.X.X</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

Replace version X.X.X with the latest version available (as of now – 2.17.181) – check version
at: https://mvnrepository.com/artifact/software.amazon.awssdk/bom

Edit the file pom.xml and add the following directly inside <dependencies> (this selects only the parts
from AWS SDK needed for this project – in our case, only SNS). Be careful to add the following in the
<dependencies> element which is NOT inside the <dependencyManagement> element.

<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>sns</artifactId>
</dependency>
Create a file Main.java in directory src/main/java/com/example with the following content:

package com.example;

import software.amazon.awssdk.services.sns.SnsClient;

import software.amazon.awssdk.services.sns.model.PublishRequest;

import software.amazon.awssdk.services.sns.model.PublishResponse;

public class Main {

public static void main(String[] args) {

// initialize the Amazon SNS client

SnsClient snsClient = SnsClient.builder().build();

// publish a message to an Amazon SNS topic

String topicArn = "********************";

final String subject = "Test";

final String msg = "Hello!\nThis is a test message...";


final PublishRequest publishRequest =
PublishRequest.builder().topicArn(topicArn).subject(subject).message(msg).build();

final PublishResponse publishResponse =


snsClient.publish(publishRequest);

// print response details

System.out.println("MessageId: " + publishResponse.messageId());

System.out.println("Response metadata requestID: " +


publishResponse.responseMetadata().requestId());

System.out.println("SDK HTTP response headers: " +


publishResponse.sdkHttpResponse().headers());

System.out.println("SDK HTTP response code: " +


publishResponse.sdkHttpResponse().statusCode() + " " +
publishResponse.sdkHttpResponse().statusText());

// close the client

snsClient.close();

In the previous code, replace ******************** with your topic ARN (Amazon Resource Name).
Compile the application:
mvn compile

Run the application:


mvn exec:java -Dexec.mainClass="com.example.Main"

Resources
AWS SDK for Java:
https://aws.amazon.com/sdk-for-java/

AWS SDK for Java API Docs:


https://sdk.amazonaws.com/java/api/latest/

You might also like