T
- The generic type of the operation's output valueA
- The generic type of the operation's output attributes@NoImplement public interface CompletionCallback<T,A>
In order to implement a non blocking operation, the method needs to:
The operation's output will be derived from the generics, so even though the method is void, the runtime will consider the operation to return values of the generic types.
When the non blocking operation has finished, it has to notify the result either by
invoking the success(Result)
or error(Throwable)
methods.
Only then will the operation be considered as completed and the next processor in the
pipeline will be executed.
For example, let's see a very simple non blocking http request
public void request(String path, @Content Object content, CompletionCallbackcallback) { httpClient.requestNonBlocking(path, content, new HttpClientCallback() { void onSuccess(HttpResponse response) { callback.onSuccess(Result. .builder() .output(response.getBody()) .attributes(toAttributes(response)) .build()); } void onFailure(Exception e) { callback.onException(e); } } }
If the operation is void, the Void
type can be used in the generics.
Modifier and Type | Method and Description |
---|---|
void |
error(Throwable e)
This method is not be invoked when the operation failed to execute.
|
void |
success(Result<T,A> result)
This method is to be invoked with the operation's result.
|
void success(Result<T,A> result)
Result
instance.result
- the operation's resultvoid error(Throwable e)
e
- the exception foundCopyright © 2018. All rights reserved.