public class Deferred extends Result
{ ExecutorService executor = ...; get("/async", promise(deferred -> { executor.execute(() -> { try { deferred.resolve(...); // success value } catch (Exception ex) { deferred.reject(ex); // error value } }); })); }
Or with automatic error handler:
{ ExecutorService executor = ...; get("/async", promise(deferred -> { executor.execute(() -> { deferred.resolve(() -> { Object value = ... return value; }); // success value }); })); }
Or as Runnable
with automatic error handler:
{ ExecutorService executor = ...; get("/async", promise(deferred -> { executor.execute(deferred.run(() -> { Object value = ... return value; }); // success value })); }
Application can produces a result from a different thread. Once result is ready, a call to
set(Object)
is required. Please note, a call to set(Object)
is required in case
of errors.
Due the code will be handle by an external/new thread, you MUST be ready to handle errors,
usually with a try/catch statement. A call to set(Object)
is required too in case of an
error.
Checkout the utility method resolve(CheckedFunction0)
and/or
run(CheckedFunction0)
. Both of them catch and handle exceptions for you.
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. |
Modifier and Type | Method and Description |
---|---|
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.
|
<T> void |
resolve(javaslang.CheckedFunction0<T> block)
|
void |
resolve(Object value)
Resolve the deferred value and handle it.
|
<T> Runnable |
run(javaslang.CheckedFunction0<T> block)
Produces a
Runnable that runs the given Callable and
resolve(CheckedFunction0) or reject(Throwable) the deferred. |
Result |
set(Object value)
resolve(Object) or reject(Throwable) the given value. |
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()
Deferred
.public Result set(Object value)
resolve(Object)
or reject(Throwable)
the given value.public void resolve(Object value)
value
- A value for this deferred.public void reject(Throwable cause)
cause
- A value for this deferred.public <T> Runnable run(javaslang.CheckedFunction0<T> block)
Runnable
that runs the given Callable
and
resolve(CheckedFunction0)
or reject(Throwable)
the deferred.
Please note, the given Callable
runs in the caller thread.T
- Resulting type.block
- Callable that produces a result.Runnable
.public <T> void resolve(javaslang.CheckedFunction0<T> block)
Callable
and resolve(CheckedFunction0)
or
reject(Throwable)
the
deferred.
Please note, the given Callable
runs in the caller thread.T
- Resulting type.block
- Callable that produces a result.public void handler(Request req, Deferred.Handler handler) throws Exception
req
- Current request.handler
- A response handler.Exception
- If initializer fails to start.Copyright © 2016. All rights reserved.