users@jersey.java.net

[Jersey] Re: Jersey ClientRuntime submit strategy

From: Marek Potociar <marek.potociar_at_oracle.com>
Date: Tue, 16 Jun 2015 12:37:01 -0700

Hi Joel,

Come up with a working solution that supports SSE and async client calls on top of a blocking IO that can avoid the Future concept and please contribute it. :)

Marek

> On 16 Jun 2015, at 12:01, Joel Joseph <joe.jose2706_at_gmail.com> wrote:
>
> Hi All,
>
> In the submit method of Jersey's ClientRuntime class, a SettableFuture is used.
> a. What is the motivation behind using a SettableFuture?
> b. Why block on the future at line 31?
> c. Isn't calling processResponse and processFailure methods from the response and failure methods respectively of the AsyncConnectorCallback a more appropriate approach than blocking on the future? This will make the API fully asynchronous.
>
> 1. public void submit(final ClientRequest request, final ResponseCallback callback) {
> submit(asyncExecutorsFactory.getExecutor(), new Runnable() {
>
> @Override
> public void run() {
> ClientRequest processedRequest;
> try {
> processedRequest = Stages.process(request, requestProcessingRoot);
> processedRequest = addUserAgent(processedRequest, connector.getName());
> 10. } catch (final AbortException aborted) {
> processResponse(aborted.getAbortResponse(), callback);
> return;
> }
>
> try {
> final SettableFuture<ClientResponse> responseFuture = SettableFuture.create();
> final AsyncConnectorCallback connectorCallback = new AsyncConnectorCallback() {
>
> @Override
> 20. public void response(final ClientResponse response) {
> responseFuture.set(response);
> }
>
> @Override
> public void failure(final Throwable failure) {
> responseFuture.setException(failure);
> }
> };
> connector.apply(processedRequest, connectorCallback);
> 30.
> 31. processResponse(responseFuture.get(), callback);
> } catch (final ExecutionException e) {
> processFailure(e.getCause(), callback);
> } catch (final Throwable throwable) {
> processFailure(throwable, callback);
> }
> }
> });
> }
>
> Thanks,
> Joel