Java で RDS が利用可能となるまで待つ

Java SDK で RDS の状態を監視して、利用可能になるまで待つ方法です。

boolean pending = true;
while (pending) {
  String status = null;
  try {
    DescribeDBInstancesResult result =
      client.describeDBInstances(new DescribeDBInstancesRequest()
        .withDBInstanceIdentifier("test-db"));
    List<DBInstance> list = result.getDBInstances();
    for (DBInstance db : list) {
      status = db.getDBInstanceStatus();
    }
  } catch (DBInstanceNotFoundException e) {
      status = "notfound";
  }
  if ("available".equalsIgnoreCase(status)) {
    pending = false;
  }
  Thread.sleep(1000);
}