How to use futures in Vertx routers with WebClient in Java - java

I have a Vertx application with a router endpoint:
router.route(HttpMethod.GET, Constants.ENDPOINT).blockingHandler(this::getItems);
This router calls a method, that is supposed to return a JSON object in the browser, or whatever client is calling this endpoint. The JSON object actually comes from a completely different service. I am using Vert.x's WebClient library to call this service.
private void getItems(RoutingContext routingContext) {
HttpServerResponse response = routingContext.response();
response.setChunked(true);
response.putHeader("content-type", "text/plain");
response.putHeader("Access-Control-Allow-Origin", "*");
JsonObject data = new JsonObject();
WebClient webClient = WebClient.create(vertx);
webClient.post(80, "my-site.com", "/api/items")
.as(BodyCodec.jsonArray())
.putHeader("Accept", "application/json")
.putHeader("Content-Type", "application/json")
.sendJsonObject(new JsonObject().put("mutator", "*"), ar -> {
if (ar.succeeded()) {
HttpResponse<JsonArray> result = ar.result();
JsonArray body = result.body();
System.out.println(body);
data.put("data", body.getJsonObject(0));
} else {
data.put("data", ar.cause().getMessage());
}
});
response.write(data.encode());
routingContext.response().end();
}
The data I get from my-site.com is fine and displays in the console with my System.out command. The problem is that I cannot get it into response.write.
Reading up, I see that this is related to futures. I don't really understand the concept, so I've been doing a lot of reading but cannot find any examples that fit my particular code.
How would I go about implementing futures so that the data I receive from my-site.com gets put into my Json object (data), and then can be used in response.write?

In your impl data will be an empty JSON object because Webclient is async. You are writing the response to the client before the response from Webclient is ready.
Move the write into the webclient response and end the context there. E.g:
...
if (ar.succeeded()) {
HttpResponse<JsonArray> result = ar.result();
JsonArray body = result.body();
System.out.println(body);
data.put("data", body.getJsonObject(0));
} else {
data.put("data", ar.cause().getMessage());
}
response.write(data.encode());
routingContext.response().end();
...

The Vert.x documentation about async coordination is very good and uses futures in the examples. Here is the way I would implement it using Vert.x futures:
private void getItems(RoutingContext routingContext) {
HttpServerResponse response = routingContext.response();
response.setChunked(true);
response.putHeader("content-type", "text/plain");
response.putHeader("Access-Control-Allow-Origin", "*");
// init a future that should hold a JsonObject result
Future<JsonObject> future = Future.future();
JsonObject data = new JsonObject();
WebClient webClient = WebClient.create(vertx);
webClient.post(80, "my-site.com", "/api/items")
.as(BodyCodec.jsonArray())
.putHeader("Accept", "application/json")
.putHeader("Content-Type", "application/json")
.sendJsonObject(new JsonObject().put("mutator", "*"), ar -> {
if (ar.succeeded()) {
HttpResponse<JsonArray> result = ar.result();
JsonArray body = result.body();
System.out.println(body);
data.put("data", body.getJsonObject(0));
// set future to be completed, with data object as its JsonObject result
future.complete(data);
} else {
data.put("data", ar.cause().getMessage());
future.complete(data);
// we can also set the future as failed and give it a Throwable
// future.fail(ar.cause());
}
});
// handle when the future is completed
future.setHandler(jsonObjectAsyncResult -> {
if(jsonObjectAsyncResult.succeeded()) {
response.write(data.encode());
routingContext.response().end();
}
});
}

Related

Throws java.lang.IllegalStateException: block()/blockFirst()/blockLast() when retrying invalid token

I am trying to update the token when I get a response with 401 status code.
In order to do that, I used web client. I know that this mainly used to do reactive development but since resttemplate will soon be deprecated I went for this option.
The issue I am facing is that when it does call the api endpoint to get the new token, it throws a 'java.lang.IllegalStateException: block()/blockFirst()/blockLast() '. And make sense as it stated in the exception message It is not supported in thread reactor-http-nio-3.
I saw that there is a map and flatmap option, but I couldn't figure out how to use it inside the doBeforeRetry() to make it process in a different stream.
I need to have that new token before retrying.
So the question is : How can I get the token via another call and then still do the retry ?
I was able to make it work by using a try catch but I would like to find the solution how to use it inside that retry method.
I also try to block the token request by replacing the token response by a Mono and block it by using myMono.toFuture().get() as stated here block()/blockFirst()/blockLast() are blocking error when calling bodyToMono AFTER exchange()
Here is the code :
Method responsible for the call :
public String getValueFromApi(HashMap<String, Object> filter) {
String response = "";
response = webclient
.post()
.uri(endpoint)
.header("token", token.getToken())
.bodyValue(filter)
.retrieve()
.bodyToMono(String.class)
.retryWhen(Retry.max(3).doBeforeRetry(
retrySignal -> tokenService.getTokenFromApi(env)
).filter(InvalidTokenException.class::isInstance))
.block();
return response;
}
Method that retrieve the token :
public void getTokenFromApi(Environment env) {
HashMap<String, String> requestBody = new HashMap<>();
requestBody.put("name", "name");
requestBody.put("password", "password");
String response = WebClient
.builder()
.baseUrl(BASE_PATH)
.defaultHeader(HttpHeaders.CONTENT_TYPE, "application/json")
.build()
.post()
.uri(tokenUri)
.body(BodyInserters.fromValue(requestBody))
.retrieve()
.bodyToMono(String.class)
.block();
getTokenFromResponse(response);
}
private void getTokenFromResponse(String reponse) {
JsonObject tokenObject = new Gson().fromJson(reponse, JsonObject.class);
setToken(tokenObject.get("token").getAsString());
}
WebClient Builder :
#Bean
public WebClient webClientForApi(WebClient.Builder webClientBuilder) {
return webClientBuilder
.clientConnector(new ReactorClientHttpConnector(httpClient))
.filter(errorHandler())
.filter(logRequest())
.clone()
.baseUrl(BASE_PATH)
.defaultHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
.defaultHeader(HttpHeaders.ACCEPT, "application/json")
.build();
}
public ExchangeFilterFunction errorHandler() {
return ExchangeFilterFunction.ofResponseProcessor(clientResponse -> {
if (clientResponse.statusCode().equals(HttpStatus.UNAUTHORIZED)) {
return Mono.error(InvalidTokenException::new);
} else if (clientResponse.statusCode() == HttpStatus.INTERNAL_SERVER_ERROR) {
return Mono.error(ApiInternalServerException::new);
} else {
return Mono.just(clientResponse);
}
});
}
private ExchangeFilterFunction logRequest() {
return ExchangeFilterFunction.ofRequestProcessor(clientRequest -> {
logger.info("Request: {} {}", clientRequest.method(), clientRequest.url());
clientRequest.headers().forEach((name, values) -> values.forEach(value -> logger.info("{}={}", name, value)));
return Mono.just(clientRequest);
});
}

WebClient how to make calls in Parallel and wait for response

First time trying Webclient and a bit lost. I'm trying to call an API potentially up to 20 times, and I want the calls to happen in parallel and process the response objects as they come in. Then returning the response. I have it almost working, the API is properly iterating through all the responses as they come in and building my response object. However it's not blocking, meaning when the response is finished building, my API has already returned an empty response such as: {}
API:
public GetHistoricalRes getHistoricalDaily(GetHistoricalReq getHistoricalReq) {
GetHistoricalRes historicalDailyQuotesRes = new GetHistoricalRes();
List<Mono<GenHistoricalRes>> genHistoricalDailyQuotes = new ArrayList<>();
for (String ticker : getHistoricalReq.getTickers()) {
genHistoricalDailyQuotes.add(MrMarketClient.getHistoricalDailyQuotes(ticker, getHistoricalReq.getTo(), getHistoricalReq.getFrom()));
}
Flux.merge(genHistoricalDailyQuotes).subscribe((genHistoricalRes) -> {
historicalDailyQuotesRes.getQuotes().put(genHistoricalRes.getSymbol(), genHistoricalRes);
});
return historicalDailyQuotesRes;
}
Webclient:
public Mono<GenHistoricalRes> getHistoricalDailyQuotes(String ticker, String to, String from) {
String historicalPricePath = "/historical-price-full/" + ticker;
return this.getClient()
.get()
.uri(builder -> builder
.path(historicalPricePath)
.queryParam("apikey", apiKey)
.queryParam("from", from)
.queryParam("to", to)
.build())
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
.accept(MediaType.APPLICATION_JSON)
.exchangeToMono(
response -> {
if (response.statusCode().equals(HttpStatus.OK)) {
return response.bodyToMono(GenHistoricalRes.class)
.log();
} else {
return response.createException()
.flatMap(Mono::error);
}
});
}

Spring Boot WebClient Connection and Read Timeout

Currently my post and get requests are handled through WebClients which has a common connection and read timeout in Spring Boot. I have 5 different classes each requiring its own set of connection and read timeout. I don't want to create 5 different WebClients, rather use the same Webclient but while sending a post or a get request from a particular class, specify the required connection and read timeout. Is there any way to implement this?
My current WebClient:
#Bean
public WebClient getWebClient(WebClient.Builder builder){
HttpClient httpClient = HttpClient.newConnection()
.tcpConfiguration(tcpClient -> {
tcpClient = tcpClient.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, connectionTimeout*1000);
tcpClient = tcpClient.doOnConnected(conn -> conn
.addHandlerLast(new ReadTimeoutHandler(readTimeout, TimeUnit.SECONDS)));
return tcpClient;
}).wiretap(true);
ClientHttpConnector connector = new ReactorClientHttpConnector(httpClient);
return builder.clientConnector(connector).build();
}
A post request I'm using:
public WebClientResponse httpPost(String endpoint, String requestData, Map<String, Object> requestHeader) {
ClientResponse res = webClient.post().uri(endpoint)
.body(BodyInserters.fromObject(requestData))
.headers(x -> {
if(requestHeader != null && !requestHeader.isEmpty()) {
for (String s : requestHeader.keySet()) {
x.set(s, String.valueOf(requestHeader.get(s)));
}
}
})
.exchange()
.doOnSuccess(x -> log.info("response code = " + x.statusCode()))
.block();
return convertWebClientResponse(res);
}
You can configure request-level timeout in WebClient.
webClient.get()
.uri("https://baeldung.com/path")
.httpRequest(httpRequest -> {
HttpClientRequest reactorRequest = httpRequest.getNativeRequest();
reactorRequest.responseTimeout(Duration.ofSeconds(2));
});
Now what you can do is that based on the request you can add those values either from the properties file or you can hard code them.
Reference:- https://www.baeldung.com/spring-webflux-timeout
You can try timeout with webClient like below,
webClient.post()
.uri(..)
.body(..)
.retrieve()
.
.
.
.timeout(Duration.ofMillis(30);
30 is just example.
#Bean
public WebClient getWebClient() {
HttpClient httpClient = HttpClient.create()
.tcpConfiguration(client ->
client.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 4000)
.doOnConnected(conn -> conn
.addHandlerLast(new ReadTimeoutHandler(4))
.addHandlerLast(new WriteTimeoutHandler(4))));
ClientHttpConnector connector = new ReactorClientHttpConnector(httpClient.wiretap(true));
return WebClient.builder()
.clientConnector(connector)
.defaultHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE) // if you need a default header
.build();
}
or you can use what #Amol has suggested

Get http response code and all available body

I want ot implement WebFlux example client which can make request with http params and get the response body and http response code. I tried this:
public ClientResponse execute(NotificationMessage nm)
Mono<String> transactionMono = Mono.just(convertedString);
return client.post().uri(builder -> builder.build())
.header(HttpHeaders.USER_AGENT, "agent")
.body(transactionMono, String.class).exchange().block();
}
private static String convert(Map<String, String> map) throws UnsupportedEncodingException {
String result = map.entrySet().stream().map(e -> encode(e.getKey()) + "=" + encode(e.getValue()))
.collect(Collectors.joining("&"));
return result;
}
private static String encode(String s) {
try {
return URLEncoder.encode(s, "UTF-8");
} catch (UnsupportedEncodingException e) {
throw new IllegalStateException(e);
}
}
Can you give me some advice after .exchange() how I can get the http status code and all available body.
From the ClientResponse object returned by exchange you can use response.statusCode() to get the status and use response.bodyToMono() or bodyToFlux() to get the actual body. You should avoid using .block() in reactive programming and use .subscribe() or .flatMap() or other operators to get the data from Mono or Flux objects. Read more about reactive programming and Project reactor (used by spring webflux) here.
For eg:
public Mono<Data> execute(NotificationMessage nm)
return client.post().uri(builder -> builder.build())
.header(HttpHeaders.USER_AGENT, "agent")
.body(transactionMono, String.class).exchange()
.flatMap(response -> {
HttpStatus code = response.statusCode();
Data data = response.bodyToMono(Data.class);
return data;
});
}

vertx get body of a simple post reques

Their doc on vertx website isn't quite clear on how to receive the body of a request.
var vertx = Vertx.vertx();
var server = vertx.createHttpServer();
var Router = require("vertx-web-js/router");
var BodyHandler = require("vertx-web-js/body_handler");
var router = Router.router(vertx);
router.route().handler(BodyHandler.create().handle);
router.route('POST', "/a").handler(function (routingContext) {
var response = routingContext.response();
response.setChunked(true);
response.write("a json received");
var str = routingContext.getBodyAsJson()
console.log(str);
// Now end the response
routingContext.response().end();
});
I get the error:
vertx-js/util/console.js:9 ReferenceError: "inspect" is not defined
How am I supposed to know what to call if they don't even put it in their doc..
Paulo said my version of vertx was outdated and this was a bug. I'll take his word for it. In the meantime I tried doing it in Java like in the answer I got. However I had more success doing it like this:
router.route().handler(BodyHandler.create());
router.route(HttpMethod.POST, "/iamonline").handler(rc -> {
JsonObject json = rc.getBodyAsJson();
System.out.println(json.getString("id"));
HttpServerResponse response = rc.response();
response.putHeader("content-type", "application/json");
// Write to the response and end it
response.end("{\"status\": 200}");
});
I ran into the same the first time. Use the .bodyHandler which is a convenience method for receiving the entire request body in one piece.
As a reference, I'll give you an example in Java (you can easily "transform" it into ECMAScript):
public void login(final RoutingContext routingContext) {
routingContext.request().bodyHandler(bodyHandler -> {
final JsonObject body = bodyHandler.toJsonObject();
// `body` now contains you what you POST'ed
});
}

Categories