findFutureList
findFutureList allows for the background execution of a query which we can wait for and cancel if desired.
// 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
Execute find row count query in a background thread.
This returns a Future object which can be used to cancel, check the execution status (isDone etc) and get the value (with or without a timeout).
FutureRowCount<Customer> futureRowCount = DB.find(Customer.class)
.where().ilike("name", "Rob%")
.findFutureCount()
// do other things
Having a Future
reference allows you to check if the execution is done,
get the value directly (blocking while waiting for the value) or get the value with
a specified timeout.
// 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
Similar to findIds you can find a list of Ids in a background thread using findFutureIds
.
This returns a Future object which can be used to cancel, check the execution status (isDone etc) and get the value (with or without a timeout).
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)
}