Posted In: Spring, Spring MVC

Spring Asynch Rest Client – Using Netty library example2

Usecase

Application needs to use Netty library to call REST services. Spring provides Netty factory to do that. By default Spring will use default JDK capabilities to call REST services.

Maven entry for netty

<dependency>
    <groupId>io.netty</groupId>
    <artifactId>netty-all</artifactId>
    <version>4.1.13.Final</version>
</dependency>

 

package com.example;

import org.springframework.http.ResponseEntity;
import org.springframework.http.client.AsyncClientHttpRequestFactory;
import org.springframework.http.client.Netty4ClientHttpRequestFactory;
import org.springframework.util.concurrent.ListenableFuture;
import org.springframework.web.client.AsyncRestTemplate;

public class AsyncRestTmpltNetty2 {
	public static void main(String[] args) {
		new AsyncRestTmpltNetty2().proceed();
	}

	public void proceed() {
		System.out.println(
		        "AsyncRestTmpltNetty2::proceed() started");
		try {
			AsyncClientHttpRequestFactory acrf = new Netty4ClientHttpRequestFactory();
			AsyncRestTemplate art = new AsyncRestTemplate(acrf);
			ListenableFuture<ResponseEntity<String>> lf1 = art
			        .getForEntity(
			                "http://localhost:8080/Example201726/time/30",
			                String.class);
			ResponseEntity<String> entity1 = lf1.get();
			System.out.println(entity1.getBody());
		} catch (Exception e) {
			e.printStackTrace();
		}
		System.out.println(
		        "AsyncRestTmpltNetty2::proceed() ends");
	}
}

Tags:

by , on July 24th, 2017

  • Categories