Queries in Ebean
Asynchrous Query execution
Ebean has built in support for executing queries asynchronously. These queries are executed in a background thread and "Future" objects are returned.
The "Future" objects returned extend java.util.concurrent.Future. This provides support for cancelling the query, checking if it is cancelled or done and getting the result with waiting and timeout support.
example
// Methods on Query for ansychronous execution
public FutureList<T> findFutureList();
public FutureIds<T> findFutureIds();
public FutureRowCount<T> findFutureRowCount();
An example showing the use of FutureList:
example
Query<Order> query = Ebean.find(Order.class);
// find list using a background thread
FutureList<Order> futureList = query.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);