T
- the type of value that was promised.public interface Fulfiller<T>
This type is used to integrate with asynchronous APIs, via the ExecControl.promise(ratpack.func.Action)
method.
The following example shows usage during request processing.
import ratpack.handling.InjectionHandler; import ratpack.handling.Context; import ratpack.exec.Promise; import ratpack.exec.Fulfiller; import ratpack.func.Action; // Some 3rd party asynchronous API public interface SomeAsyncApi { interface AsyncCallback { void onSuccess(String result); void onError(IOException e); } void doSomeAsyncOperation(AsyncCallback callback); } public class AsyncApiUsingHandler extends InjectionHandler { void handle(final Context context, final SomeAsyncApi asyncApi) { Promise<String> promise = context.promise(new Action<Fulfiller<String>>() { public void execute(final Fulfiller<String> fulfiller) { asyncApi.doSomeAsyncOperation(new SomeAsyncApi.AsyncCallback() { public void onSuccess(String result) { fulfiller.success(result); } public void onError(IOException e) { fulfiller.error(e); } }); } }); promise.then(new Action<String>() { public void execute(String string) { context.render(string); } }); } }
The methods of this method may be executed asynchronously. That is, the promise subscription my be invoked in a separate thread.
Modifier and Type | Method and Description |
---|---|
default void |
accept(CompletableFuture<? extends T> future)
Fulfills via the given completable future.
|
default void |
accept(Result<? extends T> result)
Fulfills via the given result.
|
void |
error(Throwable throwable)
Fulfills the promise with an error result.
|
void |
success(T value)
Fulfills the promise with the given value.
|
@NonBlocking void error(Throwable throwable)
throwable
- the error result@NonBlocking void success(T value)
value
- the value to provide to the promise subscriberdefault void accept(Result<? extends T> result)
result
- the result to use to fulfill.default void accept(CompletableFuture<? extends T> future)
future
- the future to use to fulfill