public class Deferred extends Result
A Deferred result, useful for async request processing.
Application can produces a result from a different thread. Once result is ready, a call to
resolve(Object)
is required. Please note, a call to reject(Throwable)
is
required in case of errors.
{ get("/async", deferred(() -> { return "Success"; })); }From MVC route:
public class Controller {
@GET
@Path("/async")
public Deferred async() {
return Deferred.deferred(() -> "Success");
}
}
If you add the AsyncMapper
then your controller method can return a Callable
.
Previous example runs in the default executor, which always run deferred results in the
same/caller thread.
To effectively run a deferred result in new/different thread you need to provide an
Executor
:
{
executor(new ForkJoinPool());
}
This line override the default executor with a ForkJoinPool
. You can add two or more
named executor:
{
executor(new ForkJoinPool());
executor("cached", Executors.newCachedExecutor());
get("/async", deferred("cached", () -> "Success"));
}
A Deferred
object works as a promise too, given you resolve(Object)
and
reject(Throwable)
methods. Examples:
As promise using the default executor (execute promise in same/caller thread):
{ get("/async", promise(deferred -> { try { deferred.resolve(...); // success value } catch (Throwable ex) { deferred.reject(ex); // error value } })); }As promise using a custom executor:
{ executor(new ForkJoinPool()); get("/async", promise(deferred -> { try { deferred.resolve(...); // success value } catch (Throwable ex) { deferred.reject(ex); // error value } })); }As promise using an alternative executor:
{ executor(new ForkJoinPool()); executor("cached", Executors.newCachedExecutor()); get("/async", promise("cached", deferred -> { try { deferred.resolve(...); // success value } catch (Throwable ex) { deferred.reject(ex); // error value } })); }
Modifier and Type | Class and Description |
---|---|
static interface |
Deferred.Handler
A deferred handler.
|
static interface |
Deferred.Initializer
Deferred initializer with
Request access, useful to provide a more functional API. |
static interface |
Deferred.Initializer0
Deferred initializer, useful to provide a more functional API.
|
Constructor and Description |
---|
Deferred()
Creates a new
Deferred . |
Deferred(Deferred.Initializer initializer)
Creates a new
Deferred with an initializer. |
Deferred(Deferred.Initializer0 initializer)
Creates a new
Deferred with an initializer. |
Deferred(String executor,
Deferred.Initializer initializer)
Creates a new
Deferred with an initializer. |
Deferred(String executor,
Deferred.Initializer0 initializer)
Creates a new
Deferred with an initializer. |
Modifier and Type | Method and Description |
---|---|
String |
callerThread()
Name of the caller thread (thread that creates this deferred object).
|
static Deferred |
deferred(Route.OneArgHandler handler)
Functional version of
Deferred(Initializer) . |
static Deferred |
deferred(Route.ZeroArgHandler handler)
Functional version of
Deferred(Initializer) . |
static Deferred |
deferred(String executor,
Route.OneArgHandler handler)
Functional version of
Deferred(Initializer) . |
static Deferred |
deferred(String executor,
Route.ZeroArgHandler handler)
Functional version of
Deferred(Initializer) . |
Optional<String> |
executor()
Get an executor to run this deferred result.
|
void |
handler(Request req,
Deferred.Handler handler)
Setup a handler for this deferred.
|
void |
reject(Throwable cause)
Resolve the deferred with an error and handle it.
|
void |
resolve(Object value)
Resolve the deferred value and handle it.
|
Result |
set(Object value)
resolve(Object) or reject(Throwable) the given value. |
public Deferred(String executor, Deferred.Initializer0 initializer)
Deferred
with an initializer.executor
- Executor to use.initializer
- An initializer.public Deferred(Deferred.Initializer0 initializer)
Deferred
with an initializer.initializer
- An initializer.public Deferred(Deferred.Initializer initializer)
Deferred
with an initializer.initializer
- An initializer.public Deferred(String executor, Deferred.Initializer initializer)
Deferred
with an initializer.executor
- Executor to use.initializer
- An initializer.public Deferred()
Deferred
.public Result set(Object value)
resolve(Object)
or reject(Throwable)
the given value.public Optional<String> executor()
public String callerThread()
public void resolve(Object value)
value
- A value for this deferred.public void reject(Throwable cause)
cause
- A value for this deferred.public void handler(Request req, Deferred.Handler handler) throws Exception
req
- Current request.handler
- A response handler.Exception
- If initializer fails to start.public static Deferred deferred(Route.OneArgHandler handler)
Deferred(Initializer)
.
Using the default executor (current thread):
{
get("/fork", deferred(req -> {
return req.param("value").value();
}));
}
Using a custom executor:
{
executor(new ForkJoinPool());
get("/fork", deferred(req -> {
return req.param("value").value();
}));
}
This handler automatically resolve(Object)
or
reject(Throwable)
a route handler response.handler
- Application block.public static Deferred deferred(Route.ZeroArgHandler handler)
Deferred(Initializer)
.
Using the default executor (current thread):
{
get("/fork", deferred(() -> {
return req.param("value").value();
}));
}
Using a custom executor:
{
executor(new ForkJoinPool());
get("/fork", deferred(() -> {
return req.param("value").value();
}));
}
This handler automatically resolve(Object)
or
reject(Throwable)
a route handler response.handler
- Application block.public static Deferred deferred(String executor, Route.ZeroArgHandler handler)
Deferred(Initializer)
. To use ideally with one
or more Executor
:
{
executor("cached", Executors.newCachedExecutor());
get("/fork", deferred("cached", () -> {
return "OK";
}));
}
This handler automatically resolve(Object)
or
reject(Throwable)
a route handler response.executor
- Executor to run the deferred.handler
- Application block.public static Deferred deferred(String executor, Route.OneArgHandler handler)
Deferred(Initializer)
. To use ideally with one
or more Executor
:
{
executor("cached", Executors.newCachedExecutor());
get("/fork", deferred("cached", req -> {
return req.param("value").value();
}));
}
This handler automatically resolve(Object)
or
reject(Throwable)
a route handler response.executor
- Executor to run the deferred.handler
- Application block.Copyright © 2016. All rights reserved.