本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
为队列配置标签
使用成本分配标签来帮助组织和标识您的 Amazon SQS 队列。以下示例演示如何使用配置标签Amazon SDK for Java. 有关更多信息,请参阅 Amazon SQS 成本分配标签。
运行示例代码之前,请确保您已设置Amazon凭证。有关更多信息,请参阅 。设置Amazon发展凭证和区域中的Amazon SDK for Java 2.x开发人员指南.
列出标签
要列出队列的标签,请使用ListQueueTags方法。
// Create an SqsClient for the specified region. SqsClient sqsClient = SqsClient.builder().region(Region.US_WEST_1).build(); // Get the queue URL. String queueName = "MyStandardQ1"; GetQueueUrlResponse getQueueUrlResponse = sqsClient.getQueueUrl(GetQueueUrlRequest.builder().queueName(queueName).build()); String queueUrl = getQueueUrlResponse.queueUrl(); // Create the ListQueueTagsRequest. final ListQueueTagsRequest listQueueTagsRequest = ListQueueTagsRequest.builder().queueUrl(queueUrl).build(); // Retrieve the list of queue tags and print them. final ListQueueTagsResponse listQueueTagsResponse = sqsClient.listQueueTags(listQueueTagsRequest); System.out.println(String.format("ListQueueTags: \tTags for queue %s are %s.\n", queueName, listQueueTagsResponse.tags() ));
添加或更新标签
要添加或更新队列的标签值,请使用TagQueue方法。
// Create an SqsClient for the specified Region. SqsClient sqsClient = SqsClient.builder().region(Region.US_WEST_1).build(); // Get the queue URL. String queueName = "MyStandardQ1"; GetQueueUrlResponse getQueueUrlResponse = sqsClient.getQueueUrl(GetQueueUrlRequest.builder().queueName(queueName).build()); String queueUrl = getQueueUrlResponse.queueUrl(); // Build a hashmap of the tags. final HashMap<String, String> addedTags = new HashMap<>(); addedTags.put("Team", "Development"); addedTags.put("Priority", "Beta"); addedTags.put("Accounting ID", "456def"); //Create the TagQueueRequest and add them to the queue. final TagQueueRequest tagQueueRequest = TagQueueRequest.builder() .queueUrl(queueUrl) .tags(addedTags) .build(); sqsClient.tagQueue(tagQueueRequest);
删除标签
要从队列中删除一个或多个标签,请使用UntagQueue方法。下面的示例删除Accounting ID标签。
// Create the UntagQueueRequest. final UntagQueueRequest untagQueueRequest = UntagQueueRequest.builder() .queueUrl(queueUrl) .tagKeys("Accounting ID") .build(); // Remove the tag from this queue. sqsClient.untagQueue(untagQueueRequest);