com.jslsolucoes.jax.rs.client.se.api.impl.DefaultJaxRsApiClientRequestAsync Maven / Gradle / Ivy
package com.jslsolucoes.jax.rs.client.se.api.impl;
import java.net.SocketTimeoutException;
import java.time.Duration;
import java.util.concurrent.CompletableFuture;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.Invocation.Builder;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.jslsolucoes.jax.rs.client.se.api.JaxRsApiClient;
import com.jslsolucoes.jax.rs.client.se.api.JaxRsApiClientRequestAsync;
import com.jslsolucoes.jax.rs.client.se.api.JaxRsApiClientResponse;
import io.github.resilience4j.retry.Retry;
import io.github.resilience4j.retry.RetryConfig;
import io.vavr.CheckedFunction0;
import io.vavr.control.Try;
public class DefaultJaxRsApiClientRequestAsync implements JaxRsApiClientRequestAsync {
private Builder builder;
private JaxRsApiClient jaxRsApiClient;
private Retry retry;
private final static Logger logger = LoggerFactory.getLogger(DefaultJaxRsApiClientRequestAsync.class);
private Integer retryAttempts = 3;
private Integer retryDelay = 3000;
public DefaultJaxRsApiClientRequestAsync(JaxRsApiClient jaxRsApiClient, Builder builder) {
this.jaxRsApiClient = jaxRsApiClient;
this.builder = builder;
this.retry = retry();
}
private Retry retry() {
Retry retry = Retry.of("c1", retryConfig());
retry.getEventPublisher().onEvent(event -> logger.debug("retry event: {}", event));
return retry;
}
private RetryConfig retryConfig() {
return RetryConfig.custom().maxAttempts(retryAttempts)
.retryOnException(throwable -> throwable.getCause().getClass().equals(SocketTimeoutException.class))
.waitDuration(Duration.ofMillis(retryDelay)).build();
}
@Override
public JaxRsApiClientRequestAsync retryAttempts(Integer retryAttempts) {
this.retryAttempts = retryAttempts;
return this;
}
@Override
public JaxRsApiClientRequestAsync retryDelay(Integer retryDelay) {
this.retryDelay = retryDelay;
return this;
}
@Override
public CompletableFuture get() {
return CompletableFuture.supplyAsync(() -> {
return new DefaultJaxRsApiClientResponse(jaxRsApiClient, retryOf(HttpMethod.GET));
});
}
@Override
public CompletableFuture delete() {
return CompletableFuture.supplyAsync(() -> {
return new DefaultJaxRsApiClientResponse(jaxRsApiClient, retryOf(HttpMethod.DELETE));
});
}
@Override
public CompletableFuture post(Entity> entity) {
return CompletableFuture.supplyAsync(() -> {
return new DefaultJaxRsApiClientResponse(jaxRsApiClient, retryOf(HttpMethod.POST, entity));
});
}
@Override
public CompletableFuture put(Entity> entity) {
return CompletableFuture.supplyAsync(() -> {
return new DefaultJaxRsApiClientResponse(jaxRsApiClient, retryOf(HttpMethod.PUT, entity));
});
}
@Override
public CompletableFuture post(Object object) {
return CompletableFuture.supplyAsync(() -> {
return new DefaultJaxRsApiClientResponse(jaxRsApiClient, retryOf(HttpMethod.POST, entity(object)));
});
}
@Override
public CompletableFuture put(Object object) {
return CompletableFuture.supplyAsync(() -> {
return new DefaultJaxRsApiClientResponse(jaxRsApiClient, retryOf(HttpMethod.PUT, entity(object)));
});
}
private Response retryOf(HttpMethod httpMethod) {
return retryOf(httpMethod, null);
}
@SuppressWarnings("serial")
private Response retryOf(HttpMethod httpMethod, Entity> entity) {
CheckedFunction0 retryableSupplier = Retry.decorateCheckedSupplier(retry,
new CheckedFunction0() {
@Override
public Response apply() throws Throwable {
return execute(httpMethod, entity);
}
});
return Try.of(retryableSupplier).recover(throwable -> new ServiceUnavailableResponse()).get();
}
private Response execute(HttpMethod httpMethod, Entity> entity) {
if (httpMethod.equals(HttpMethod.GET)) {
return builder.get();
} else if (httpMethod.equals(HttpMethod.DELETE)) {
return builder.delete();
} else if (httpMethod.equals(HttpMethod.POST)) {
return builder.post(entity);
} else if (httpMethod.equals(HttpMethod.PUT)) {
return builder.put(entity);
} else {
return builder.get();
}
}
private Entity> entity(Object object) {
return Entity.entity(object, MediaType.APPLICATION_JSON);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy