-
Notifications
You must be signed in to change notification settings - Fork 1
Executing Requests Asynchronously 1.2.3
Request executions are blocking operations and as such they mandate several requests be executed sequentially (assuming there's no custom threaded code). To execute a single request asynchronously or several requests in parallel the @Asynchronous annotation and be used.
When applied to an endpoint, all requests are marked to be executed asynchronously. Or it can be used at the method level to identify a subset of requests.
@Endpoint("www.cryptomail.com")
@Parser(PARSER_TYPE.STRING)
@Asynchronous
public interface CryptoMail {}###### At request level ```java @Asynchronous @Request(path = "/send/encrypt", method = RequestMethod.HTTP_POST) public abstract void sendEncryptedMessage(@Param("message") String message); ``` > `@Asynchronous` can even be used for requests susceptible to high network congestion.
Responses of asynchronous requests can be processed using an AyncHandler. It provides two callback methods to handle the response, where the onSuccess() callback should always be implemented. This is invoked if the request executed successfully and it supplies the HttpResponse as well as the parsed response content. If the request failed, the onFailure() callback is invoked.
@Asynchronous @Request(path = "/inbox/starred")
public abstract MessageSet getStarredMessages(AsyncHandler<MessageSet> asyncHandler);###### Invocation ```java cryptoMailEndpoint.getStarredMessages(new AsyncHandler() {
@Override
public void onSuccess(HttpResponse httpResponse, MessageSet messageSet) {
//process results
}
@Override
public void onFailure(HttpResponse httpResponse) {
//handle failure
}
});