Amazon Simple Notification Service
开发人员指南 (API Version 2010-03-31)
AWS 服务或AWS文档中描述的功能,可能因地区/位置而异。点 击 Getting Started with Amazon AWS to see specific differences applicable to the China (Beijing) Region.

在 Amazon SNS 中使用 AWS SDK for Java

适用于 Java 的开发工具包 提供一个名为 AmazonSNSClient 的类,可以用于与 Amazon SNS 交互。有关下载AWS SDK for Java的信息,请转到AWS SDK for Java

AmazonSNSClient 类定义映射到基础 Amazon SNS 查询 API 操作的方法。(这些操作在 Amazon SNS API 参考中进行了介绍)。调用某个方法时,您必须创建对应的请求对象和响应对象。请求对象包含必须通过实际请求传递的信息。响应对象包含 Amazon SNS 为响应请求而返回的信息。

例如,AmazonSNSClient 类提供 createTopic 方法,用于创建可以向其发布通知的主题。此方法映射到基础 CreateTopic API 操作。使用 CreateTopicRequest 对象可通过 createTopic 方法传递信息。

以下导入语句用于提供的 java 示例。

Copy
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;

以下示例演示如何创建新 Amazon SNS 客户端、设置 Amazon SNS 终端节点进行使用以及创建新主题。

注意

在以下部分示例中,getCachedResponseMetadata 方法用于显示如何以编程方式检索先前成功执行 Amazon SNS 请求的请求 ID。这通常用于调试问题,在请求从 Amazon Web Services 获得帮助时十分有用。

创建主题

Copy
//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));

运行此实例时,以下内容会显示在 IDE(如 Eclipse)的控制台输出窗口中:

Copy
{TopicArn: arn:aws:sns:us-east-1:123456789012:MyNewTopic} CreateTopicRequest - {AWS_REQUEST_ID=93f7fc90-f131-5ca3-ab18-b741fef918b5}

TopicArn 分配有一个字符串变量,以在其他操作中使用。

Copy
String topicArn = "arn:aws:sns:us-east-1:123456789012:MyNewTopic";

以下示例演示如何对主题进行订阅、发布和删除。

订阅主题

Copy
//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.");

运行此示例时,以下内容会显示在 IDE 的控制台输出窗口中:

Copy
SubscribeRequest - {AWS_REQUEST_ID=9b7ff59a-f917-533a-a6bd-be4bf6df0acf} Check your email and confirm subscription.

向主题发布

Copy
//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());

运行此示例时,以下内容会显示在 IDE 的控制台输出窗口中:

Copy
MessageId - 9b888f80-15f7-5c30-81a2-c4511a3f5229

删除主题

Copy
//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));

运行此示例时,以下内容会显示在 IDE 的控制台输出窗口中:

Copy
DeleteTopicRequest - {AWS_REQUEST_ID=067a4980-4e93-5bfc-b88c-0251415bc852}