Amazon SQS キューへのメッセージ属性の送信 - Amazon Simple Queue Service

Amazon SQS キューへのメッセージ属性の送信

メッセージ属性をメッセージに使用すると、構造化メタデータ (タイムスタンプ、地理空間データ、署名、識別子) などを指定することができます。詳細については、「Amazon SQS メッセージ属性」を参照してください。

サンプルコードを実行する前に、AWS認証情報の設定を確認してください。詳細については、AWS SDK for Java 2.x デベロッパーガイドの「デベロップメントAWS 認証情報とリージョンのセットアップ」を参照してください。

属性の定義

メッセージの属性を定義するには、MessageAttributeValue データタイプを使用する以下のコードを追加します。詳細については、「メッセージ属性コンポーネント」および「メッセージ属性のデータ型」を参照してください。

AWS SDK for Java はメッセージ本文とメッセージ属性のチェックサムを自動的に計算し、Amazon SQS が返信するデータと比較します。詳細については、AWS SDK for Java 2.xデベロッパーガイドを参照してください。他のプログラミング言語については、「メッセージ属性のMD5メッセージダイジェストの計算」を参照してください。

String

この例では、値がStringである、Nameという名前のJane属性を定義します。

final Map<String, MessageAttributeValue> messageAttributes = new HashMap<>(); messageAttributes.put("Name", new MessageAttributeValue() .withDataType("String") .withStringValue("Jane"));
Number

この例では、値がNumberである、AccurateWeightという名前の230.000000000000000001属性を定義します。

final Map<String, MessageAttributeValue> messageAttributes = new HashMap<>(); messageAttributes.put("AccurateWeight", new MessageAttributeValue() .withDataType("Number") .withStringValue("230.000000000000000001"));
Binary

この例では、初期化されていない10バイト配列の値を持つ、Binaryという名前のByteArray属性を定義します。

final Map<String, MessageAttributeValue> messageAttributes = new HashMap<>(); messageAttributes.put("ByteArray", new MessageAttributeValue() .withDataType("Binary") .withBinaryValue(ByteBuffer.wrap(new byte[10])));
String (custom)

この例では、値がString.EmployeeIdである、EmployeeIdという名前のカスタム属性ABC123456を定義します。

final Map<String, MessageAttributeValue> messageAttributes = new HashMap<>(); messageAttributes.put("EmployeeId", new MessageAttributeValue() .withDataType("String.EmployeeId") .withStringValue("ABC123456"));
Number (custom)

この例では、値がNumber.AccountIdである、AccountIdという名前のカスタム属性000123456を定義します。

final Map<String, MessageAttributeValue> messageAttributes = new HashMap<>(); messageAttributes.put("AccountId", new MessageAttributeValue() .withDataType("Number.AccountId") .withStringValue("000123456"));
注記

ベースデータタイプはNumberであるため、ReceiveMessageメソッドは123456を返します。

Binary (custom)

この例では、初期化されていない10バイト配列の値を持つ、Binary.JPEGという名前のカスタム属性ApplicationIconを定義します。

final Map<String, MessageAttributeValue> messageAttributes = new HashMap<>(); messageAttributes.put("ApplicationIcon", new MessageAttributeValue() .withDataType("Binary.JPEG") .withBinaryValue(ByteBuffer.wrap(new byte[10])));

属性を含むメッセージの送信

この例では、SendMessageRequestメッセージを送信する前に属性を追加します。

// Send a message with an attribute. final SendMessageRequest sendMessageRequest = new SendMessageRequest(); sendMessageRequest.withMessageBody("This is my message text."); sendMessageRequest.withQueueUrl(myQueueUrl); sendMessageRequest.withMessageAttributes(messageAttributes); sqs.sendMessage(sendMessageRequest);
重要

First-In-First-Out(FIFO)キューにメッセージを送信する場合は、メッセージグループIDを提供したsendMessageで、 メソッドが実行されることを確認します。

SendMessageの代わりにSendMessageBatch を使用する場合は、バッチの各メッセージに対してメッセージ属性を指定する必要があります。