AWSSDK を使用して Amazon SQS キューに一括送信する - AWSSDK コードサンプル

AWSDocAWS SDKGitHub サンプルリポジトリには、さらに多くの SDK サンプルがあります


AWSSDK を使用して Amazon SQS キューに一括送信する

次のコード例は、Amazon SQS キューにメッセージのバッチを送信する方法を示しています。

SDK for Java 2.x

他にもありますGitHub。用例一覧を検索し、AWS コード例リポジトリでの設定と実行の方法を確認してください。

SendMessageBatchRequest sendMessageBatchRequest = SendMessageBatchRequest.builder() .queueUrl(queueUrl) .entries(SendMessageBatchRequestEntry.builder().id("id1").messageBody("Hello from msg 1").build(), SendMessageBatchRequestEntry.builder().id("id2").messageBody("msg 2").delaySeconds(10).build()) .build(); sqsClient.sendMessageBatch(sendMessageBatchRequest);
  • API の詳細については、AWS SDK for Java 2.xAPI SendMessageBatchリファレンスのを参照してください

SDK for Python (Boto3)

他にもありますGitHub。用例一覧を検索し、AWS コード例リポジトリでの設定と実行の方法を確認してください。

def send_messages(queue, messages): """ Send a batch of messages in a single request to an SQS queue. This request may return overall success even when some messages were not sent. The caller must inspect the Successful and Failed lists in the response and resend any failed messages. :param queue: The queue to receive the messages. :param messages: The messages to send to the queue. These are simplified to contain only the message body and attributes. :return: The response from SQS that contains the list of successful and failed messages. """ try: entries = [{ 'Id': str(ind), 'MessageBody': msg['body'], 'MessageAttributes': msg['attributes'] } for ind, msg in enumerate(messages)] response = queue.send_messages(Entries=entries) if 'Successful' in response: for msg_meta in response['Successful']: "Message sent: %s: %s", msg_meta['MessageId'], messages[int(msg_meta['Id'])]['body'] ) if 'Failed' in response: for msg_meta in response['Failed']: logger.warning( "Failed to send: %s: %s", msg_meta['MessageId'], messages[int(msg_meta['Id'])]['body'] ) except ClientError as error: logger.exception("Send messages failed to queue: %s", queue) raise error else: return response
  • API の詳細については、「AWSSDK for Python (Boto3) API リファレンス」を参照してくださいSendMessageBatch

SDK for Ruby

他にもありますGitHub。用例一覧を検索し、AWS コード例リポジトリでの設定と実行の方法を確認してください。

require "aws-sdk-sqs" require "aws-sdk-sts" # # @param sqs_client [Aws::SQS::Client] An initialized Amazon SQS client. # @param queue_url [String] The URL of the queue. # @param entries [Hash] The contents of the messages to be sent, # in the correct format. # @return [Boolean] true if the messages were sent; otherwise, false. # @example # exit 1 unless messages_sent?( # 'us-west-2'), # '', # [ # { # id: 'Message1', # message_body: 'This is the first message.' # }, # { # id: 'Message2', # message_body: 'This is the second message.' # } # ] # ) def messages_sent?(sqs_client, queue_url, entries) sqs_client.send_message_batch( queue_url: queue_url, entries: entries ) true rescue StandardError => e puts "Error sending messages: #{e.message}" false end # Full example call: # Replace us-west-2 with the AWS Region you're using for Amazon SQS. def run_me region = "us-west-2" queue_name = "my-queue" entries = [ { id: "Message1", message_body: "This is the first message." }, { id: "Message2", message_body: "This is the second message." } ] sts_client = region) # For example: # '' queue_url = "https://sqs." + region + "" + sts_client.get_caller_identity.account + "/" + queue_name sqs_client = region) puts "Sending messages to the queue named '#{queue_name}'..." if messages_sent?(sqs_client, queue_url, entries) puts "Messages sent." else puts "Messages not sent." end end
  • API の詳細については、AWS SDK for RubyAPI SendMessageBatchリファレンスのを参照してください