users@jersey.java.net

[Jersey] Jersey ClientRuntime submit strategy

From: Joel Joseph <joe.jose2706_at_gmail.com>
Date: Tue, 16 Jun 2015 12:01:12 -0700

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