JavaでSQSの属性を渡す方法

https://hacknote.jp/archives/19677/

の反対でSQSに属性を渡す方法です。

  protected void setQueue(AmazonSQS sqs,String bodyMessage, String attr1) {    
CreateQueueResult createQueue =
      sqs.createQueue(new CreateQueueRequest()
        .withQueueName("YourQueueName"));
    HashMap<String, MessageAttributeValue> messageAttributes =
      new HashMap<String, MessageAttributeValue>();
    messageAttributes.put("attr1", new MessageAttributeValue().withDataType(
      "String").withStringValue(attr1));
    sqs.sendMessage(new SendMessageRequest(createQueue.getQueueUrl(), bodyMessage)
      .withMessageAttributes(messageAttributes));
  }