private void executeRequest(ProjectCollection collection, List<SchemaField> schema, List<Event> events, CompletableFuture<Void> future, boolean tried) {
HttpResponseFuture<StringResponse> f = HTTP_CLIENT.executeAsync(Request.builder()
.setUri(buildInsertUri(collection, schema))
.setMethod("POST")
.setBodyGenerator(new BinaryRawGenerator(events, schema))
.build(), createStringResponseHandler());
f.addListener(() -> {
try {
StringResponse stringResponse = f.get(1L, MINUTES);
if (stringResponse.getStatusCode() == 200) {
future.complete(null);
}
else {
RuntimeException ex = new RuntimeException(stringResponse.getStatusMessage() + " : "
+ stringResponse.getBody().split("\n", 2)[0]);
future.completeExceptionally(ex);
}
}
catch (InterruptedException|ExecutionException|TimeoutException e) {
if(!tried) {
executeRequest(collection, schema, events, future, true);
} else {
future.completeExceptionally(e);
LOGGER.error(e);
}
}
}, Runnable::run);
}