Код: Выделить всё
sendClusterPost(restRouteOfNode, jwtToken, CLUSTER_POST_RETRY_ATTEMPT)
.thenAccept(success -> {
if (success) {
logger.info("Cluster POST redirection succeeded for {}", restRouteOfNode);
} else {
logger.error("Cluster POST redirection failed for {}. Local node {} will fetch the stream.",
restRouteOfNode, getServerSettings().getHostAddress());
// Handle failure here.
}
})
.exceptionally(ex -> {
logger.error("Cluster POST encountered an exception: {}", ExceptionUtils.getStackTrace(ex));
return null;
});
Код: Выделить всё
public CompletableFuture sendClusterPost(String url, String clusterCommunicationToken, int retryAttempts) {
CompletableFuture future = new CompletableFuture();
vertx.executeBlocking(promise -> {
try (CloseableHttpClient httpClient = getHttpClient()) {
HttpPost httpPost = new HttpPost(url);
RequestConfig requestConfig = RequestConfig.custom()
.setConnectTimeout(CLUSTER_POST_TIMEOUT_MS)
.setConnectionRequestTimeout(CLUSTER_POST_TIMEOUT_MS)
.setSocketTimeout(CLUSTER_POST_TIMEOUT_MS)
.build();
httpPost.setConfig(requestConfig);
httpPost.setHeader(TokenFilterManager.TOKEN_HEADER_FOR_NODE_COMMUNICATION, clusterCommunicationToken);
try (CloseableHttpResponse httpResponse = httpClient.execute(httpPost)) {
int statusCode = httpResponse.getStatusLine().getStatusCode();
logger.info("Cluster POST Response Status: {}", statusCode);
if (statusCode == HttpStatus.SC_OK) {
promise.complete(true);
} else if (retryAttempts > 0) {
logger.info("Retrying Cluster POST in {} ms due to non-200 response: {}",
appSettings.getWebhookRetryDelay(), statusCode);
retrySendClusterPostWithDelay(url, clusterCommunicationToken, retryAttempts - 1)
.thenAccept(promise::complete);
} else {
logger.info("No more retry attempts left. Giving up.");
promise.complete(false);
}
}
} catch (IOException e) {
if (retryAttempts > 0) {
logger.info("Retrying Cluster POST in {} ms due to IOException: {}",
appSettings.getWebhookRetryDelay(), ExceptionUtils.getStackTrace(e));
retrySendClusterPostWithDelay(url, clusterCommunicationToken, retryAttempts - 1)
.thenAccept(promise::complete);
} else {
logger.info("No more retry attempts left. Giving up.");
promise.complete(false);
}
}
}, result -> {
if (result.succeeded()) {
future.complete((Boolean) result.result());
} else {
future.completeExceptionally(result.cause());
}
});
return future;
}
public CompletableFuture retrySendClusterPostWithDelay(String url, String clusterCommunicationToken, int retryAttempts) {
CompletableFuture future = new CompletableFuture();
vertx.setTimer(appSettings.getWebhookRetryDelay(), timerId ->
sendClusterPost(url, clusterCommunicationToken, retryAttempts).thenAccept(future::complete)
);
return future;
}
Является ли это правильной неблокирующей реализацией логики повтора?
Есть ли в этом коде какие-либо избыточные или ненужные части, которые можно было бы упростить? без ущерба для функциональности?
Любые предложения по улучшению или упрощению приветствуются!
Подробнее здесь: https://stackoverflow.com/questions/792 ... ercomplica