使用 Amazon 软件开发工具包创建并发布到 FIFO Amazon SNS 主题 - Amazon Simple Notification Service
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅中国的 Amazon Web Services 服务入门

使用 Amazon 软件开发工具包创建并发布到 FIFO Amazon SNS 主题

以下代码示例展示如何创建并发布到 FIFO Amazon SNS 主题。

Java
SDK for Java 2.x
提示

要了解如何设置和运行此示例,请参阅 GitHub

创建 FIFO 主题和 FIFO 队列。为队列订阅主题。

public static void main(String[] args) { final String usage = "\n" + "Usage: " + " <topicArn>\n\n" + "Where:\n" + " fifoTopicName - The name of the FIFO topic. \n\n" + " fifoQueueARN - The ARN value of a SQS FIFO queue. You can get this value from the AWS Management Console. \n\n"; if (args.length != 2) { System.out.println(usage); System.exit(1); } String fifoTopicName = "PriceUpdatesTopic3.fifo"; String fifoQueueARN = "arn:aws:sqs:us-east-1:814548047983:MyPriceSQS.fifo"; SnsClient snsClient = SnsClient.builder() .region(Region.US_EAST_1) .credentialsProvider(ProfileCredentialsProvider.create()) .build(); createFIFO(snsClient, fifoTopicName, fifoQueueARN); } public static void createFIFO(SnsClient snsClient, String topicName, String queueARN) { try { // Create a FIFO topic by using the SNS service client. Map<String, String> topicAttributes = new HashMap<>(); topicAttributes.put("FifoTopic", "true"); topicAttributes.put("ContentBasedDeduplication", "false"); CreateTopicRequest topicRequest = CreateTopicRequest.builder() .name(topicName) .attributes(topicAttributes) .build(); CreateTopicResponse response = snsClient.createTopic(topicRequest); String topicArn = response.topicArn(); System.out.println("The topic ARN is"+topicArn); // Subscribe to the endpoint by using the SNS service client. // Only Amazon SQS FIFO queues can receive notifications from an Amazon SNS FIFO topic. SubscribeRequest subscribeRequest = SubscribeRequest.builder() .topicArn(topicArn) .endpoint(queueARN) .protocol("sqs") .build(); snsClient.subscribe(subscribeRequest); System.out.println("The topic is subscribed to the queue."); // Compose and publish a message that updates the wholesale price. String subject = "Price Update"; String payload = "{\"product\": 214, \"price\": 79.99}"; String groupId = "PID-214"; String dedupId = UUID.randomUUID().toString(); String attributeName = "business"; String attributeValue = "wholesale"; MessageAttributeValue msgAttValue = MessageAttributeValue.builder() .dataType("String") .stringValue(attributeValue) .build(); Map<String, MessageAttributeValue> attributes = new HashMap<>(); attributes.put(attributeName, msgAttValue); PublishRequest pubRequest = PublishRequest.builder() .topicArn(topicArn) .subject(subject) .message(payload) .messageGroupId(groupId) .messageDeduplicationId(dedupId) .messageAttributes(attributes) .build(); snsClient.publish(pubRequest); System.out.println("Message was published to "+topicArn); } catch (SnsException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } } }

有关 Amazon 软件开发工具包开发人员指南和代码示例的完整列表,请参阅 将 Amazon SNS 与 Amazon 开发工具包结合使用。本主题还包括有关入门的信息以及有关先前的软件开发工具包版本的详细信息。