Hi Tsume,
there is no such thing as a blocking Future in Akka; if you are referring to java.util.concurrent.Future, then that also does not have a timeout per se (you can limit the time you await its result, but that is equally possible with scala.concurrent.Future).
Sending a request to a database typically (sadly) is a synchronous blocking API call (e.g. JDBC) which also cannot be interrupted, so the only thing you can do is to discard the result it may produce and try anew.
import akka.dispatch.Futures;
import akka.pattern.Patterns;
final Future<Result> f1 = ...; // e.g. wrap a JDBC call here
final Future<Result> t1 = Patterns.after(timeout, context.system.scheduler, context.dispatcher, Futures.failed(new TimeoutException));
final Future<Result> f2 = Futures.firstCompletedOf(Arrays.asList(new Future<Result>[] { f1, t1 }));
final Future<Result> result = f2.recover(new Recover<Result>() {
@Override public Result recover(Throwable failure) {
if (failure instanceof TimeoutException) {
// try again, fall back to different data source, whatever
}
}
Assuming that f1 in the above example results from a Patterns.ask() operation, it will be completed with the timeout given there, so the special handling with t1 and f2 is not necessary. Otherwise failures are not automatically propagated, the Future will only be completed with what the actor sends. In order to send a failure (and thus fail the Future) there is a special wrapper class akka.actor.Status.Failure which needs to be sent back.
Regards,
Roland