Using the AWS SDK for Java with Amazon SNS
The SDK for Java provides a class named AmazonSNSClient that you can use to interact with Amazon SNS. For information about downloading the AWS SDK for Java, go to AWS SDK for Java.
The AmazonSNSClient
class defines methods that map to underlying
Amazon SNS Query API actions. (These actions are described in the Amazon SNS API
Reference). When you call a method, you must create a corresponding request
object and response object. The request object includes information that you must
pass with
the actual request. The response object includes information returned from Amazon
SNS in
response to the request.
For example, the AmazonSNSClient
class provides the createTopic
method to create a topic to which notifications can be published. This method maps
to the
underlying CreateTopic API action. You create a
CreateTopicRequest object to
pass information with the createTopic
method.
The following import statements are used with the provided java samples.
import com.amazonaws.services.sns.AmazonSNSClient; import com.amazonaws.auth.ClasspathPropertiesFileCredentialsProvider; import com.amazonaws.regions.Region; import com.amazonaws.regions.Regions; import com.amazonaws.services.sns.model.CreateTopicRequest; import com.amazonaws.services.sns.model.CreateTopicResult; import com.amazonaws.services.sns.model.SubscribeRequest; import com.amazonaws.services.sns.model.PublishRequest; import com.amazonaws.services.sns.model.PublishResult; import com.amazonaws.services.sns.model.DeleteTopicRequest;
The following example shows how to create a new Amazon SNS client, set the Amazon SNS endpoint to use, and then create a new topic.
Note
In some of the following examples, the getCachedResponseMetadata
method is used to show how to programmatically
retrieve the request ID for a previously executed successful Amazon SNS request. This
is typically used for debugging issues and is helpful when requesting
assistance from Amazon Web Services.
Create a Topic
//create a new SNS client and set endpoint AmazonSNSClient snsClient = new AmazonSNSClient(new ClasspathPropertiesFileCredentialsProvider()); snsClient.setRegion(Region.getRegion(Regions.US_EAST_1)); //create a new SNS topic CreateTopicRequest createTopicRequest = new CreateTopicRequest("MyNewTopic"); CreateTopicResult createTopicResult = snsClient.createTopic(createTopicRequest); //print TopicArn System.out.println(createTopicResult); //get request id for CreateTopicRequest from SNS metadata System.out.println("CreateTopicRequest - " + snsClient.getCachedResponseMetadata(createTopicRequest));
When you run this example, the following is displayed in the console output window of your IDE, such as Eclipse:
{TopicArn: arn:aws:sns:us-east-1:123456789012:MyNewTopic} CreateTopicRequest - {AWS_REQUEST_ID=93f7fc90-f131-5ca3-ab18-b741fef918b5}
The TopicArn is assigned to a string variable to use in additional operations.
String topicArn = "arn:aws:sns:us-east-1:123456789012:MyNewTopic";
The following examples show how to subscribe to, publish to, and delete a topic.
Subscribe to a Topic
//subscribe to an SNS topic SubscribeRequest subRequest = new SubscribeRequest(topicArn, "email", "name@example.com"); snsClient.subscribe(subRequest); //get request id for SubscribeRequest from SNS metadata System.out.println("SubscribeRequest - " + snsClient.getCachedResponseMetadata(subRequest)); System.out.println("Check your email and confirm subscription.");
When you run this example, the following is displayed in the console output window of your IDE:
SubscribeRequest - {AWS_REQUEST_ID=9b7ff59a-f917-533a-a6bd-be4bf6df0acf} Check your email and confirm subscription.
Publish to a Topic
//publish to an SNS topic String msg = "My text published to SNS topic with email endpoint"; PublishRequest publishRequest = new PublishRequest(topicArn, msg); PublishResult publishResult = snsClient.publish(publishRequest); //print MessageId of message published to SNS topic System.out.println("MessageId - " + publishResult.getMessageId());
When you run this example, the following is displayed in the console output window of your IDE:
MessageId - 9b888f80-15f7-5c30-81a2-c4511a3f5229
Delete a Topic
//delete an SNS topic DeleteTopicRequest deleteTopicRequest = new DeleteTopicRequest(topicArn); snsClient.deleteTopic(deleteTopicRequest); //get request id for DeleteTopicRequest from SNS metadata System.out.println("DeleteTopicRequest - " + snsClient.getCachedResponseMetadata(deleteTopicRequest));
When you run this example, the following is displayed in the console output window of your IDE:
DeleteTopicRequest - {AWS_REQUEST_ID=067a4980-4e93-5bfc-b88c-0251415bc852}