Spring 4 AsyncClientHttpRequestFactory and AsyncClientHttpRequest Example

By Arvind Rai, August 23, 2014
Spring 4 has provided many API for asynchronous execution of HTTP request. In this series we will learn AsyncClientHttpRequestFactory and AsyncClientHttpRequest and their usability by example. Both API are interface. HttpComponentsAsyncClientHttpRequestFactory is the implementation class of AsyncClientHttpRequestFactory .

AsyncClientHttpRequestFactory

AsyncClientHttpRequestFactory is a factory to create AsyncClientHttpRequest. AsyncClientHttpRequestFactory is an interface. The implementation of this interface is HttpComponentsAsyncClientHttpRequestFactory in Spring 4.

HttpComponentsAsyncClientHttpRequestFactory

HttpComponentsAsyncClientHttpRequestFactory has a method createAsyncRequest(URI, HttpMethod) that returns the instance of AsyncClientHttpRequest. We will see the usability in our example.

org.springframework.http.client.AsyncClientHttpRequest

AsyncClientHttpRequest represents client side asynchronous HTTP request. AsyncClientHttpRequest has a method executeAsync() that returns the ListenableFuture of ClientHttpResponse class.
AsyncRequestDemo.java
package com.concretepage;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.concurrent.ExecutionException;
import org.springframework.http.HttpMethod;
import org.springframework.http.client.AsyncClientHttpRequest;
import org.springframework.http.client.AsyncClientHttpRequestFactory;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.http.client.HttpComponentsAsyncClientHttpRequestFactory;
import org.springframework.util.concurrent.ListenableFuture;
public class AsyncRequestDemo {
	public static void main(String[] args) throws IOException, URISyntaxException, InterruptedException, ExecutionException {
		AsyncClientHttpRequestFactory  asyncFactory = new HttpComponentsAsyncClientHttpRequestFactory();
		URI uri = new URI("http://google.com");
		AsyncClientHttpRequest asynReq = asyncFactory.createAsyncRequest(uri, HttpMethod.GET);
		ListenableFuture<ClientHttpResponse> future = asynReq.executeAsync();
		ClientHttpResponse response = future.get();
		System.out.println(response.getStatusCode());
	}
} 

Maven Dependency

Find the Maven Dependency to run the example.
pom.xml
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
        </dependency>
        <dependency>
	      <groupId>org.apache.httpcomponents</groupId>
	      <artifactId>httpclient</artifactId>
	      <version>4.4-alpha1</version>
        </dependency>
        <dependency>
		  <groupId>org.apache.httpcomponents</groupId>
		  <artifactId>httpasyncclient</artifactId>
		  <version>4.0.2</version>
		</dependency>
    </dependencies>
 

Output
Run the example as java application and check the output.
20:04:53.508 [I/O dispatcher 2] DEBUG o.a.h.i.n.c.ManagedNHttpClientConnectionImpl - http-outgoing-1 192.168.0.101:55790<->173.194.121.24:80[ACTIVE][r:r]: Remove attribute http.nio.exchange-handler
20:04:53.508 [I/O dispatcher 2] DEBUG o.a.h.i.n.c.PoolingNHttpClientConnectionManager - Releasing connection: [id: http-outgoing-1][route: {}->http://www.google.co.in:80][total kept alive: 1; route allocated: 1 of 5; total allocated: 2 of 10]
20:04:53.508 [I/O dispatcher 2] DEBUG o.a.h.i.n.c.PoolingNHttpClientConnectionManager - Connection [id: http-outgoing-1][route: {}->http://www.google.co.in:80] can be kept alive indefinitely
20:04:53.508 [I/O dispatcher 2] DEBUG o.a.h.i.n.c.PoolingNHttpClientConnectionManager - Connection released: [id: http-outgoing-1][route: {}->http://www.google.co.in:80][total kept alive: 2; route allocated: 1 of 5; total allocated: 2 of 10]
20:04:53.510 [I/O dispatcher 2] DEBUG o.a.h.n.p.HttpAsyncRequestExecutor - http-outgoing-1 [ACTIVE] [chunk-coded; completed: true]
200 
POSTED BY
ARVIND RAI
ARVIND RAI
LEARN MORE








©2024 concretepage.com | Privacy Policy | Contact Us