ChangeMessageVisibility 搭配 AWS SDK或 使用 CLI - AWS SDK 程式碼範例

文件範例儲存庫中有更多 AWS SDK可用的範例。 AWS SDK GitHub

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

ChangeMessageVisibility 搭配 AWS SDK或 使用 CLI

下列程式碼範例示範如何使用 ChangeMessageVisibility

C++
SDK 適用於 C++
注意

還有更多 。 GitHub尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

Aws::Client::ClientConfiguration clientConfig; // Optional: Set to the AWS Region (overrides config file). // clientConfig.region = "us-east-1"; //! Changes the visibility timeout of a message in an Amazon Simple Queue Service //! (Amazon SQS) queue. /*! \param queueUrl: An Amazon SQS queue URL. \param messageReceiptHandle: A message receipt handle. \param visibilityTimeoutSeconds: Visibility timeout in seconds. \param clientConfiguration: AWS client configuration. \return bool: Function succeeded. */ bool AwsDoc::SQS::changeMessageVisibility( const Aws::String &queue_url, const Aws::String &messageReceiptHandle, int visibilityTimeoutSeconds, const Aws::Client::ClientConfiguration &clientConfiguration) { Aws::SQS::SQSClient sqsClient(clientConfiguration); Aws::SQS::Model::ChangeMessageVisibilityRequest request; request.SetQueueUrl(queue_url); request.SetReceiptHandle(messageReceiptHandle); request.SetVisibilityTimeout(visibilityTimeoutSeconds); auto outcome = sqsClient.ChangeMessageVisibility(request); if (outcome.IsSuccess()) { std::cout << "Successfully changed visibility of message " << messageReceiptHandle << " from queue " << queue_url << std::endl; } else { std::cout << "Error changing visibility of message from queue " << queue_url << ": " << outcome.GetError().GetMessage() << std::endl; } return outcome.IsSuccess(); }
CLI
AWS CLI

變更訊息的逾時可見性

此範例會將指定訊息的逾時可見性變更為 10 小時 (10 小時 * 60 分鐘 * 60 秒)。

命令:

aws sqs change-message-visibility --queue-url https://sqs.us-east-1.amazonaws.com/80398EXAMPLE/MyQueue --receipt-handle AQEBTpyI...t6HyQg== --visibility-timeout 36000

輸出:

None.
JavaScript
SDK 適用於 JavaScript (v3)
注意

還有更多 。 GitHub尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

接收 Amazon SQS 訊息並變更其逾時可見性。

import { ReceiveMessageCommand, ChangeMessageVisibilityCommand, SQSClient, } from "@aws-sdk/client-sqs"; const client = new SQSClient({}); const SQS_QUEUE_URL = "queue_url"; const receiveMessage = (queueUrl) => client.send( new ReceiveMessageCommand({ AttributeNames: ["SentTimestamp"], MaxNumberOfMessages: 1, MessageAttributeNames: ["All"], QueueUrl: queueUrl, WaitTimeSeconds: 1, }), ); export const main = async (queueUrl = SQS_QUEUE_URL) => { const { Messages } = await receiveMessage(queueUrl); const response = await client.send( new ChangeMessageVisibilityCommand({ QueueUrl: queueUrl, ReceiptHandle: Messages[0].ReceiptHandle, VisibilityTimeout: 20, }), ); console.log(response); return response; };
SDK 適用於 JavaScript (v2)
注意

還有更多 。 GitHub尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

接收 Amazon SQS 訊息並變更其逾時可見性。

// Load the AWS SDK for Node.js var AWS = require("aws-sdk"); // Set the region to us-west-2 AWS.config.update({ region: "us-west-2" }); // Create the SQS service object var sqs = new AWS.SQS({ apiVersion: "2012-11-05" }); var queueURL = "https://sqs.REGION.amazonaws.com/ACCOUNT-ID/QUEUE-NAME"; var params = { AttributeNames: ["SentTimestamp"], MaxNumberOfMessages: 1, MessageAttributeNames: ["All"], QueueUrl: queueURL, }; sqs.receiveMessage(params, function (err, data) { if (err) { console.log("Receive Error", err); } else { // Make sure we have a message if (data.Messages != null) { var visibilityParams = { QueueUrl: queueURL, ReceiptHandle: data.Messages[0].ReceiptHandle, VisibilityTimeout: 20, // 20 second timeout }; sqs.changeMessageVisibility(visibilityParams, function (err, data) { if (err) { console.log("Delete Error", err); } else { console.log("Timeout Changed", data); } }); } else { console.log("No messages to change"); } } });
PowerShell
適用於 的工具 PowerShell

範例 1:此範例會將指定佇列中具有指定接收處理常式的訊息可見性逾時變更為 10 小時 (10 小時 * 60 分鐘 * 60 秒 = 36000 秒)。

Edit-SQSMessageVisibility -QueueUrl https://sqs.us-east-1.amazonaws.com/8039EXAMPLE/MyQueue -ReceiptHandle AQEBgGDh...J/Iqww== -VisibilityTimeout 36000
Ruby
SDK 適用於 Ruby
注意

還有更多 。 GitHub尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

require 'aws-sdk-sqs' # v2: require 'aws-sdk' # Replace us-west-2 with the AWS Region you're using for Amazon SQS. sqs = Aws::SQS::Client.new(region: 'us-west-2') begin queue_name = 'my-queue' queue_url = sqs.get_queue_url(queue_name: queue_name).queue_url # Receive up to 10 messages receive_message_result_before = sqs.receive_message({ queue_url: queue_url, max_number_of_messages: 10 }) puts "Before attempting to change message visibility timeout: received #{receive_message_result_before.messages.count} message(s)." receive_message_result_before.messages.each do |message| sqs.change_message_visibility({ queue_url: queue_url, receipt_handle: message.receipt_handle, visibility_timeout: 30 # This message will not be visible for 30 seconds after first receipt. }) end # Try to retrieve the original messages after setting their visibility timeout. receive_message_result_after = sqs.receive_message({ queue_url: queue_url, max_number_of_messages: 10 }) puts "\nAfter attempting to change message visibility timeout: received #{receive_message_result_after.messages.count} message(s)." rescue Aws::SQS::Errors::NonExistentQueue puts "Cannot receive messages for a queue named '#{queue_name}', as it does not exist." end