This plugin provides thre REST Clients
- NIORestClient - which is a non-blocking REST client using NIO, wrapper over Apache Async HTTP. Modified to return Java 8 CompletableFuture objects.
- AsyncRestClient - a wrapper over the Async Jersey Rest Client (it is asyncrhonous, but makes use of threads rather than NIO). Modified to return Java 8 CompletableFuture objects, and enhanced with withXXXX methods for configuration purposes.
- RestClient - which syncrhonous wrapper over the Jersey Rest Client, enhanced with withXXXX methods for configuration purposes
The NIORestClient is available as Spring bean. AsyncRestClient & RestClient can simply be instantiated via the new keyword.
public class Example{
private final int readTimeout = 10_000;
private final int connnectionTimeout = 1_000;
private final AsyncRestClient<Result> restClient = new AsyncRestClient<>(readTimeout,connectionTimeout);;
private final String url;
private final String ACCEPT_HEADERS;
private final String CONTENT_HEADERS;
public CompletableFuture<Result> query(Payload payload){
return this.restClient.withAccept(ACCEPT_HEADERS)
.withContentType(CONTENT_HEADERS)
.withResponse(RESULT.class)
.post(url,payload);
}
}Example using simple-react to manage the returned CompletableFuture object from the NIO Rest Client.
@Path("/query")
@Rest
public class EndPoint {
private final Logger logger = LoggerFactory.getLogger(this.getClass());
private final EventBus bus;
private final NIORestClient restClient;
private final String URL;
private final AtomicLong correlationId = new AtomicLong(0);
@Autowired
public EndPoint(NIORestClient restClient,EventBus bus, @Value("${url:}") String URL){
this.bus = bus;
this.URL = URL;
this.restClient=restClient;
}
private final SimpleReact builder = new SimpleReact();
@POST
@Consumes("application/json")
@Produces("application/json")
public void async(RequestType query,@Suspended AsyncResponse asyncResponse){
final long correlationId = this.correlationId.incrementAndGet();
bus.post(RequestEvents.start(query, correlationId,"standard-query",HashMapBuilder.of("ip",QueryIPRetriever.getIpAddress())));
builder.from(this.restClient.postForEntity(URL, new HttpEntity(JacksonUtil.serializeToJson(convertList(query)),headers),String.class))
.sync()
.capture(e->logger.error(e.getMessage(), e))
.then(re-> JacksonUtil.convertFromJson(re.getBody(), Result.class))
.then(this::process)
.then(action->asyncResponse.resume(action))
.onFail(error-> asyncResponse.resume(error.getCause()))
.peek( status->bus.post(RequestEvents.finish(query, correlationId)));
}
}public class Example{
private final int readTimeout = 10_000;
private final int connnectionTimeout = 1_000;
private final RestClient<Result> restClient = new RestClient<>(readTimeout,connectionTimeout);
private final String url;
private final String ACCEPT_HEADERS;
private final String CONTENT_HEADERS;
public Result query(Payload payload){
return this.restClient.withAccept(ACCEPT_HEADERS)
.withContentType(CONTENT_HEADERS)
.withResponse(RESULT.class)
.post(url,payload);
}
}The following properties & defaults are available to set values on the underlying Apache Async Http Client.
Set the timeout in milliseconds used when requesting a connection from the connection manager using the underlying HttpClient. A timeout value of 0 specifies an infinite timeout.
nio.rest.connection.request.timeout=10000
Set the socket read timeout for the underlying HttpClient in millis. A timeout value of 0 specifies an infinite timeout.
nio.rest.connection.read.timeout=0
The connection timeout for the underlying HttpClient in millis. A timeout value of 0 specifies an infinite timeout.
nio.rest.connection.connect.timeout:2000
Simply add to the classpath
Maven
<dependency>
<groupId>com.aol.microservices</groupId>
<artifactId>micro-client</artifactId>
<version>x.yz</version>
</dependency>
Gradle
compile 'com.aol.microservices:micro-client:x.yz'