SNS Producer in Java in Old Project
SNS Producer in Java in Old Project
In one Old Java Project, I need to send events to SNS in AWS. Here is how I did that> Here is the package I need to put in dependencies. Lib/aws-java-sdk-1.11.515.jar Lib/aws-java-sdk-sns-1.11.515.jar Lib/aws-java-sdk-core-1.11.515.jar Lib/jackson-databind-2.6.6.jar Lib/jackson-core-2.6.6.jar Lib/jackson-annotations-2.6.6.jar Lib/joda-time-2.10.jar This is the major class I need package com.sillycat.masterserver.engine.notification; import com.amazonaws.auth.AWSStaticCredentialsProvider; import com.amazonaws.auth.BasicAWSCredentials; import com.amazonaws.services.sns.AmazonSNS; import com.amazonaws.services.sns.AmazonSNSClientBuilder; import com.amazonaws.services.sns.model.CreateTopicResult; import com.amazonaws.services.sns.model.PublishRequest; import com.amazonaws.util.StringUtils; import com.google.gson.Gson; import com.sillycat.masterserver.common.log.MasterServerLogger; import com.sillycat.masterserver.common.log.MasterServerLogger.MMSLogLevel; import com.sillycat.masterserver.common.utils.LoggerUtils; public class EventBusNotificationManager { static String ACCESS_KEY = "AWS_ACCESS_KEY"; static String SECRET_KEY = "AWS_SECRET_KEY"; static String REGION = "AWS_REGION"; static String TOPIC = "AWS_EVENTBUS_TOPIC"; static AmazonSNS client = null; static String topicArn = null; private static void init() { String accessKey = System.getenv(ACCESS_KEY); String secretKey = System.getenv(SECRET_KEY); String region = System.getenv(REGION); String topic = System.getenv(TOPIC); if (StringUtils.isNullOrEmpty(accessKey) || StringUtils.isNullOrEmpty(secretKey) || StringUtils.isNullOrEmpty(region) || StringUtils.isNullOrEmpty(topic)) { MasterServerLogger.log(MMSLogLevel.ERROR, "EventBusNotificationManager configuration error "); return ; } BasicAWSCredentials creds = new BasicAWSCredentials(accessKey, secretKey); client = AmazonSNSClientBuilder.standard().withCredentials(new AWSStaticCredentialsProvider(creds)) .withRegion(region).build(); CreateTopicResult createTopicResponse = client.createTopic(topic); topicArn = createTopicResponse.getTopicArn(); if (StringUtils.isNullOrEmpty(topicArn)) { MasterServerLogger.log(MMSLogLevel.ERROR, "EventBusNotificationManager connection error "); } } public static void sendMessage(Object msg) { if (client == null || topicArn == null) { init(); if(client == null) { MasterServerLogger.log(MMSLogLevel.ERROR, "Fail to init connection with AWS SNS, give up, do not break the flow."); return ; } } String jsonMsg = convertToJSON(msg); final PublishRequest publishRequest = new PublishRequest().withMessage(jsonMsg).withTargetArn(topicArn); client.publish(publishRequest); } private static String convertToJSON(Object obj) { return new Gson().toJson(obj); } } EventBusMessage is just a POJO. Here is the System Settings export AWS_ACCESS_KEY=AKIxxxxxxxx export AWS_SECRET_KEY=P3o3xxxxxxxxx export AWS_REGION=us-west-1 export AWS_EVENTBUS_TOPIC=topicName-in-SNS
References: https://docs.aws.amazon.com/sns/latest/dg/sns-tutorial-publish-message-to-topic.html#publish-message-to-topic-aws-java https://stackoverflow.com/questions/31466916/sending-sms-from-java-web-app-using-aws-sns https://www.example-code.com/java/sns_publish_send_message.asp https://github.com/mfine/AmazonSNSExample/blob/master/AmazonSNSSender.java https://github.com/Cs4r/SNS-producer-SQS-consumer