Created
May 7, 2020 20:26
-
-
Save aksyuma/d079035e992822f85f3ec7709885d7c9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.syumaK.AWSSNS; | |
| import java.io.IOException; | |
| import java.util.logging.Logger; | |
| import com.amazonaws.regions.Regions; | |
| import com.amazonaws.ClientConfiguration; | |
| import com.amazonaws.services.sns.AmazonSNS; | |
| import com.amazonaws.services.sns.model.PublishResult; | |
| import com.amazonaws.services.sns.model.PublishRequest; | |
| import com.amazonaws.services.sns.AmazonSNSClientBuilder; | |
| import com.amazonaws.auth.profile.ProfileCredentialsProvider; | |
| import org.apache.log4j.BasicConfigurator; | |
| /** | |
| * @author syumaK (Amos Syuma) | |
| * Date: May 07, 2020 | |
| */ | |
| /** | |
| * Sample Java program that uses Amazon SNS SDK for Java to publish messages to | |
| * SNS Topic | |
| * <p> | |
| * <b>Prerequisites:</b> You must have a valid Amazon Web Services developer | |
| * account, and be signed up to use Amazon SNS. | |
| * <b>WARNING:</b> To avoid accidental leakage of your credentials, DO NOT keep | |
| * the credentials file in your source directory. | |
| * | |
| * http://aws.amazon.com/security-credentials | |
| */ | |
| public class App { | |
| // use the classname for the logger, this way you can refactor | |
| private static Logger LOGGER = Logger.getLogger(App.class.getName()); | |
| public static void main(String[] args) throws IOException { | |
| try { | |
| BasicConfigurator.configure(); | |
| AmazonSNS snsClient = AmazonSNSClientBuilder.standard() | |
| .withClientConfiguration(new ClientConfiguration()) | |
| .withCredentials(new ProfileCredentialsProvider("syumaK")) | |
| .withRegion(Regions.US_WEST_2) //<--, if you dont If you don't explicitly set a region using the withRegion methods, the SDK consults the default region provider chain to try and determine the region to use | |
| .build(); | |
| /* | |
| * Publish a message to an Amazon SNS topic. | |
| */ | |
| final String message = "If you receive this message, publishing a message to an Amazon SNS topic works"; | |
| final String subject = "TEST"; | |
| final String snsARN = "REDACTED"; | |
| final PublishRequest publishRequest = new PublishRequest(snsARN, message, subject); | |
| LOGGER.info("Publishing message to sns arn={} region={} message={} subject={}"); | |
| final PublishResult publishResponse = snsClient.publish(publishRequest); | |
| /* | |
| * Print the MessageId of the message. | |
| */ | |
| //LOGGER.info("Published message to sns arn={} with messageId={}", snsARN, publishResponse.getMessageId()); | |
| System.out.println("MessageId: " + publishResponse.getMessageId()); | |
| } catch (Exception ex) { | |
| System.out.println("The message wasn't sent. Error message: " + ex.getMessage()); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment