Skip to content

Commit

Permalink
add logger Closes#4
Browse files Browse the repository at this point in the history
  • Loading branch information
yildizsc committed Dec 14, 2023
1 parent b2e70e3 commit c845e35
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 4 deletions.
10 changes: 10 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,16 @@
</properties>

<dependencies>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.36</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.30</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
Expand Down
29 changes: 25 additions & 4 deletions src/main/java/com/webex/events/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.webex.events.exceptions.*;
import io.github.resilience4j.core.IntervalFunction;
import io.github.resilience4j.core.functions.CheckedSupplier;
import io.github.resilience4j.retry.Retry;
import io.github.resilience4j.retry.RetryRegistry;
import io.github.resilience4j.retry.RetryConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.net.http.HttpClient;
Expand All @@ -19,10 +22,11 @@
import java.util.stream.IntStream;

public class Client {

final static Logger logger = LoggerFactory.getLogger(Client.class);
public static final int[] RETRIABLE_HTTP_STATUSES = {408, 409, 429, 502, 503, 504};

public static String doIntrospectQuery(Configuration configuration) throws Exception {
logger.debug("Doing introspection query...");
Response response = query(
Helpers.getIntrospectionQuery(),
"IntrospectionQuery",
Expand Down Expand Up @@ -73,26 +77,38 @@ public static Response query(

long startTime = System.currentTimeMillis();
HttpClient httpClient = HttpClient.newHttpClient();
Response response = doOrRetryTheRequest(config, httpClient, request);
Response response = doOrRetryTheRequest(config, httpClient, request, operationName);
long endTime = System.currentTimeMillis();

response.setTimeSpentInMs((int) (endTime - startTime));
response.setRequestBody(json);
response.setRateLimiter(new RateLimiter(response));

logger.info(
"Executing {} query is finished with {} status code. It took {} ms and retried {} times.",
operationName,
response.status(),
response.getTimeSpendInMs(),
response.getRetryCount()
);

if (response.status() > 299) {
logger.error("Executing {} query is failed. Received status code is {}", operationName, response.status());
manageErrorState(response);
}

return response;
}

private static Response doOrRetryTheRequest(Configuration config, HttpClient httpClient, HttpRequest request) {
private static Response doOrRetryTheRequest(Configuration config, HttpClient httpClient, HttpRequest request, String operationName) {
logger.info("Executing {} query for the first time to {}",operationName,Helpers.getUri(config.getAccessToken()));
IntervalFunction intervalFn = IntervalFunction.ofExponentialBackoff(250, 1.4);
RetryConfig retryConfig = RetryConfig.custom()
.maxAttempts(config.getMaxRetries())
.waitDuration(Duration.ofMillis(500))
.intervalFunction(intervalFn)
.failAfterMaxAttempts(false)
.retryOnResult(response -> {
logger.error("{} http status received for {} query.", ((Response)response).status(), operationName);
ObjectMapper mapper = new ObjectMapper();
ErrorResponse errorResponse = null;
try {
Expand Down Expand Up @@ -121,6 +137,11 @@ private static Response doOrRetryTheRequest(Configuration config, HttpClient htt
AtomicInteger retryCount = new AtomicInteger();
retryWithDefaultConfig.getEventPublisher().onRetry(retryEvent -> {
retryCount.incrementAndGet();
logger.info(
"The HTTP request is being restarted for {} query. Waiting for {} ms...",
operationName,
retryEvent.getWaitInterval().toMillis()
);
});
CheckedSupplier<Response> retryableSupplier = Retry
.decorateCheckedSupplier(retryWithDefaultConfig, () -> {
Expand Down

0 comments on commit c845e35

Please sign in to comment.