findFutureList

findFutureList により、クエリをバックグラウンドで実行できます。このクエリは必要に応じて待ってから取り消すことができます。

// find list using a background thread
FutureList<Order> futureList =
  Order.find.where()
    .status.equalTo(Order.Status.NEW)
    .findFutureList();

  // do something else ...

if (!futureList.isDone()){
  // you can cancel the query. If supported by the JDBC
  // driver and database this will actually cancel the
  // sql query execution on the database
  futureList.cancel(true);
}

// wait for the query to finish ... no timeout
List<Order> list = futureList.get();

// wait for the query to finish ... with a 30sec timeout
List<Order> list2 = futureList.get(30, TimeUnit.SECONDS);

findFutureCount

バックグラウンドスレッドで行の数の検索クエリを実行します。

これにより、実行の取り消し、実行ステータス (isDone など) のチェック、値の取得 (タイムアウトの有無にかかわらず) が可能な Future オブジェクトが返されます。

FutureRowCount<Customer> futureRowCount = DB.find(Customer.class)
.where().ilike("name", "Rob%")
.findFutureCount()

// do other things

Future の参照があると、実行が完了しているかどうかを確認したり、値を直接取得したり (値を待ってブロックしたり) 、特定のタイムアウトで値を取得したりできます。

// Waits (blocks) for the calculation to be finished and returns the value
Integer count = futureRowCount.get();

// Waits for the calculation to be finished and returns the value
// if it happens under 10 seconds, otherwise throw an exception
Integer count = futureRowCount.get(10, TimeUnit.SECONDS);

if (futureRowCount.isDone()) {
  // Here we can fetch the value (assuming that everything went ok)
}

findFutureIds

findIds と同様に、findFutureIds を使用してバックグラウンドスレッドで ID のリストを見つけることができます。

これにより、実行の取り消し、実行ステータス (isDone など) のチェック、値の取得 (タイムアウトの有無にかかわらず) が可能な Future オブジェクトが返されます。

FutureIds<Long> futureIds = futureList =
  Order.find.where()
    .status.equalTo(Order.Status.NEW)
    .findFutureIds();
そして実際に計算した値を取得するには
// Waits (blocks) for the calculation to be finished and returns the value
List<Long> ids = futureIds.get();

// Waits for the calculation to be finished and returns the value
// if it happens under 10 seconds, otherwise throw an exception
List<Long> ids = futureIds.get(10, TimeUnit.SECONDS);

if (futureIds.isDone()) {
  // Here we can fetch the value (assuming that everything went ok)
}