

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

# Amazon SQS キューへのメッセージ属性の送信
<a name="sqs-java-send-message-with-attributes"></a>

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

 サンプルコードを実行する前に、 AWS 認証情報が設定されていることを確認してください。詳細については、「 *AWS SDK for Java 2.x デベロッパーガイド*」の[「開発用の AWS 認証情報とリージョンの設定](https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/setup.html#setup-credentials)」を参照してください。

## 属性の定義
<a name="sqs-java-define-attributes"></a>

メッセージの属性を定義するには、`[MessageAttributeValue](https://docs.aws.amazon.com/AWSSimpleQueueService/latest/APIReference/API_MessageAttributeValue.html)` データタイプを使用する以下のコードを追加します。詳細については、「[メッセージ属性コンポーネント](sqs-message-metadata.md#message-attribute-components)」および「[メッセージ属性のデータ型](sqs-message-metadata.md#message-attribute-data-types)」を参照してください。

はメッセージ本文とメッセージ属性のチェックサム AWS SDK for Java を自動的に計算し、Amazon SQS が返すデータと比較します。詳細については、*[AWS SDK for Java 2.x デベロッパーガイド](https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/)*を参照してください。他のプログラミング言語については、「[メッセージ属性のMD5メッセージダイジェストの計算](sqs-message-metadata.md#sqs-attributes-md5-message-digest-calculation)」を参照してください。

------
#### [ 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](https://docs.aws.amazon.com/AWSSimpleQueueService/latest/APIReference/API_ReceiveMessage.html)`メソッドは`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])));
```

------

## 属性を含むメッセージの送信
<a name="sqs-java-send-attributes"></a>

この例では、`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](https://docs.aws.amazon.com/AWSSimpleQueueService/latest/APIReference/API_SendMessage.html)`の代わりに`[SendMessageBatch](https://docs.aws.amazon.com/AWSSimpleQueueService/latest/APIReference/API_SendMessageBatch.html)` を使用する場合は、バッチの各メッセージに対してメッセージ属性を指定する必要があります。