Skip to content

Add AsyncRestTemplate - #336

Closed
poutsma wants to merge 1 commit into
spring-projects:masterfrom
poutsma:async-rest-template
Closed

poutsma wants to merge 1 commit into
spring-projects:masterfrom
poutsma:async-rest-template

Conversation

@poutsma

@poutsma poutsma commented Aug 15, 2013

Copy link
Copy Markdown
Contributor

Added AsyncRestTemplate, the asynchronous counterpart to the
RestTemplate that was introduced in Spring 3. All methods on the
AsyncRestTemplate are similar to those found on the synchronous
RestTemplatem, except that they return Future wrappers instead of
concrete results.

To enable this, this commit introduces the AsyncClientHttpRequest and
AsyncClientHttpRequestFactory, similar to the ClientHttpRequest and
ClientHttpRequestFactory, except that ClientHttpRequest returns a
Future for the execute method. Two implementations
of these interfaces are provided, one based on the HttpURLConnection
incombination with a Spring AsyncTaskExecutor and one based on Apache
HttpComponents HttpAsyncClient.

Issue: SPR-8804

Added AsyncRestTemplate, the asynchronous counterpart to the
RestTemplate that was introduced in Spring 3. All methods on the
AsyncRestTemplate are similar to those found on the synchronous
RestTemplatem, except that they return Future wrappers instead of
concrete results.

To enable this, this commit introduces the AsyncClientHttpRequest and
AsyncClientHttpRequestFactory, similar to the ClientHttpRequest and
ClientHttpRequestFactory, except that ClientHttpRequest returns a
Future<ClientHttpResponse> for the execute method. Two implementations
of these interfaces are provided, one based on the HttpURLConnection
incombination with a Spring AsyncTaskExecutor and one based on Apache
HttpComponents HttpAsyncClient.

Issue: SPR-8804
@poutsma

poutsma commented Aug 15, 2013

Copy link
Copy Markdown
Contributor Author

@rstoyanchev and @jhoeller, could you please review this before merging? Thanks.

@rstoyanchev

Copy link
Copy Markdown
Contributor

Thanks Arjen. We'll have a look.

@eeichinger

Copy link
Copy Markdown

how about a callback approach? Scenario: web request comes in, needs to submit 1-n requests to backends e.g. using AsyncRestTemplate. Would be easy to return a DeferredResult and set the result once the backend request completes:

@RequestMapping("doSomething") 
public DeferredResult<String> doSomething() {   
     DeferrredResult<> pendingResult = new DeferredResult<String>();

    myAsyncTemplate.get( "http://.....", new ResultHandler<String>() {
               public void processResult(String result) {
                     pendingResult.setResult( result );
               }
          });   

    return pendingResult;
 }

stupid idea? Is there another way/better way to do this that I am not aware of?

@rstoyanchev

Copy link
Copy Markdown
Contributor

There are some further changes. See pull 351.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants