POST API request timeout issue - java

I'm trying to make a POST API call to our external API via the API PROXY. I'm facing some timeout issues while making a API call. I used below restTemplate to make a API request. But at the same time I've implemented the retry for the template in case of any timeouts. I can avoid this issue by retrying that api request again.But I would like to find out the root cause for that to completely resolve the issue.Can anyone help me with this
StackTrace:
org.springframework.web.client.ResourceAccessException: I/O error on POST request for "https://localhost:8080/test": Operation timed out (Read failed); nested exception is java.net.SocketException: Operation timed out (Read failed)
private static final int HTTP_CLIENT_RETRY_COUNT = 3;
private static final int MAXIMUM_TOTAL_CONNECTION = 10;
private static final int MAXIMUM_CONNECTION_PER_ROUTE = 5;
private static final int CONNECTION_VALIDATE_AFTER_INACTIVITY_MS = 10 * 1000;
public static RestTemplate createRestTemplate(int connectionTimeoutMs, int readTimeoutMs, ObjectMapper objectMapper) {
HttpClientBuilder clientBuilder = HttpClients.custom();
PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager();
// Set the maximum number of total open connections.
connectionManager.setMaxTotal(MAXIMUM_TOTAL_CONNECTION);
// Set the maximum number of concurrent connections per route, which is 2 by default.
connectionManager.setDefaultMaxPerRoute(MAXIMUM_CONNECTION_PER_ROUTE);
connectionManager.setValidateAfterInactivity(CONNECTION_VALIDATE_AFTER_INACTIVITY_MS);
clientBuilder.setConnectionManager(connectionManager);
clientBuilder.setRetryHandler(new DefaultHttpRequestRetryHandler(HTTP_CLIENT_RETRY_COUNT, true, new ArrayList<>()) {
#Override
public boolean retryRequest(IOException exception, int executionCount, HttpContext context) {
LOGGER.info("Retry request, execution count: {}, exception: {}", executionCount, exception);
return super.retryRequest(exception, executionCount, context);
}
});
HttpComponentsClientHttpRequestFactory httpRequestFactory = new HttpComponentsClientHttpRequestFactory(clientBuilder.build());
httpRequestFactory.setConnectTimeout(connectionTimeoutMs);
httpRequestFactory.setConnectionRequestTimeout(readTimeoutMs);
httpRequestFactory.setReadTimeout(readTimeoutMs);
RestTemplate restTemplate = new RestTemplate(httpRequestFactory);
List<ClientHttpRequestInterceptor> interceptors = new ArrayList<ClientHttpRequestInterceptor>();
interceptors.add(new LoggingRequestInterceptor());
restTemplate.setInterceptors(interceptors);
restTemplate.setRequestFactory(new BufferingClientHttpRequestFactory(httpRequestFactory));
MappingJackson2HttpMessageConverter messageConverter = restTemplate.getMessageConverters().stream().filter(MappingJackson2HttpMessageConverter.class::isInstance)
.map(MappingJackson2HttpMessageConverter.class::cast).findFirst().orElseThrow(() -> new RuntimeException("MappingJackson2HttpMessageConverter not found"));
messageConverter.setObjectMapper(objectMapper);
restTemplate.getMessageConverters().stream().filter(StringHttpMessageConverter.class::isInstance).map(StringHttpMessageConverter.class::cast).forEach(a -> {
a.setWriteAcceptCharset(false);
a.setDefaultCharset(StandardCharsets.UTF_8);
});
return restTemplate;
}

Related

O365 Modern authentication with Java Spring Context

I have to use oAuth2 "modern authentication" in Java Spring Context and found here. Java code I could use.
The getAccessToken() method has however a parameter of type ManagedExecutorService and I don't have any idea, what kind of value it should be.
Here is the code:
#Override
public String getAccessToken(final String clientId, final String clientSecret, final ManagedExecutorService service)
throws MalformedURLException, ExecutionException, InterruptedException, TimeoutException {
final long now = System.currentTimeMillis();
if (accessToken != null && now < expiryTimeMs - REFRESH_BEFORE_EXPIRY_MS) {
final AuthenticationContext context = new AuthenticationContext(AUTHORITY, false, service);
final AuthenticationCallback<AuthenticationResult> callback = new AuthenticationCallback<>() {
#Override
public void onSuccess(final AuthenticationResult result) {
log.info("received token");
}
#Override
public void onFailure(final Throwable exc) {
throw new RuntimeException(exc);
}
};
log.info("requesting token");
final Future<AuthenticationResult> future = context.acquireToken(RESOUCE,
new ClientCredential(clientId, clientSecret), callback);
// wait for access token
final AuthenticationResult result = future.get(30, TimeUnit.SECONDS);
// cache token and expiration
accessToken = result.getAccessToken();
expiryTimeMs = result.getExpiresAfter();
}
return accessToken;
}
I put already all necessary dependencies into the project but can't test the method until I know what ManagedExecutorServiceis and where do I get the parameter from.
Why not using one of Spring REST clients which can handle OAuth2 out of the box?
All of #FeignClient, RestTemplate and WebClient expose configuration properties for acquiring access-token from OAuth2 authorization-server using client-credentials (or directly set authorization header if you are processing an authorized request and want to forward incoming access-token).
Choose one of existing clients and read its manual.

Increase Hystrix Circuit Breaker Timeout?

I have an implementation of Hystrix Circuit Breaker and when testing I'm getting a Hystrix Runtime Exception with the error that The CircuitBreker timed-out and fallback failed. Do I need to increase a timeout on the CircutBreaker? Should it just trip the circuit breaker if the code times out?
My junit test is as follows:
#Test
public void test4(){
client = new DefaultHttpClient();
httpget = new HttpGet("http://www.google.com:81");
resp = new CircuitBreaker(client, "test4", httpget).execute();
//assertEquals(HttpStatus.SC_GATEWAY_TIMEOUT, resp.getStatusLine().getStatusCode());
System.out.println(resp.getStatusLine().getStatusCode());
}
My Class is just to run web gets/puts/etc using the CircuitBreaker in case of failure somehow. My class is as follows:
public class CircuitBreaker extends HystrixCommand<HttpResponse> {
private HttpClient client;
private HttpRequestBase req;
protected String key;
//Set up logger
private static final Logger logger = (Logger)LoggerFactory.getLogger(CircuitBreaker.class);
/*
* This method is a constructor and sets http client based on provides args.
* This version accepts user input Hystrix key.
*/
public CircuitBreaker (HttpClient client, String key, HttpRequestBase req, int threshold) {
super(HystrixCommandGroupKey.Factory.asKey(key));
this.client = client;
this.key = key;
this.req = req;
logger.info("Hystrix Circut Breaker with Hystrix key:" + key);
logger.setLevel(Level.DEBUG);
HystrixCommandProperties.Setter().withCircuitBreakerEnabled(true);
HystrixCommandProperties.Setter().withCircuitBreakerErrorThresholdPercentage(threshold);
//HystrixCommandProperties.Setter().withCircuitBreakerRequestVolumeThreshold(50);
}
/*
* This method is a constructor and sets http client based on provides args.
* This version uses the default threshold of 50% failures if one isn't provided.
*/
public CircuitBreaker (HttpClient client,String key, HttpRequestBase req){
this(client, key, req, 50);
}
/*
* This method runs the command and returns the response.
*/
#Override
protected HttpResponse run() throws Exception {
HttpResponse resp = null;
resp = client.execute(req);
if (resp != null)
logger.info("Request to " + req.getURI() + " succeeded!");
return resp;
}
/*
* Fallback method in in the event the circuit breaker is tripped.
* Overriding the default fallback implemented by Hystrix that just throws an exception.
* #see com.netflix.hystrix.HystrixCommand#getFallback()
*/
#Override
protected HttpResponse getFallback() {
//For later expansion as needed.
logger.error("Circuit Breaker has " + getExecutionEvents() + ". Reason: "+ getFailedExecutionException().getMessage());
return null;
}
}
You can try to increase the timeout on your CircuitBreaker and see what happens:
HystrixCommandProperties.Setter().withExecutionTimeoutInMilliseconds(5000)
Because according to the Hystrix Wiki, the default timeout of HystrixCommand is 1 second, and it might take more than 1 second for your HttpGet return something.
You shouldn't need to increase the timeout to make a simple get request to google. Try this.
public class HttpCommand extends HystrixCommand<HttpResponse> {
private final HttpClient client;
private final HttpRequestBase req;
public HttpCommand(HttpClient client, HttpRequestBase req) {
super(HystrixCommandGroupKey.Factory.asKey("HttpCommandGroup"));
this.client = client;
this.req = req;
}
#Override
protected HttpResponse run() throws Exception {
return client.execute(req);
}
}
And a simple test
#Test
public void executeCommandTest(){
HttpClient client = HttpClientBuilder.create().build();
HttpGet httpget = new HttpGet("http://www.google.com");
HttpResponse resp = new HttpCommand(client, httpget).execute();
assertEquals(HttpStatus.SC_OK, resp.getStatusLine().getStatusCode());
}

Retry java RestTemplate HTTP request if host offline

Hi I'm using the spring RestTemplate for calling a REST API. The API can be very slow or even offline. My application is building the cache by sending thousands of requests one after the other. The responses can be very slow too, because they contains a lot of data.
I have already increased the Timeout to 120 seconds. My problem now it that the API can be offline and I get a org.apache.http.conn.ConnectionPoolTimeoutException: Timeout waiting for connection from pool exception.
In the case when the API ist offline, the application should wait and try again until the API is online again.
Can I achieve this in RestTemplate out of the box without building exception-loops on my own?
Thanks!
I had same situation and done some googling found the solution. Giving answer in hope it help someone else. You can set max try and time interval for each try.
#Bean
public RetryTemplate retryTemplate() {
int maxAttempt = Integer.parseInt(env.getProperty("maxAttempt"));
int retryTimeInterval = Integer.parseInt(env.getProperty("retryTimeInterval"));
SimpleRetryPolicy retryPolicy = new SimpleRetryPolicy();
retryPolicy.setMaxAttempts(maxAttempt);
FixedBackOffPolicy backOffPolicy = new FixedBackOffPolicy();
backOffPolicy.setBackOffPeriod(retryTimeInterval); // 1.5 seconds
RetryTemplate template = new RetryTemplate();
template.setRetryPolicy(retryPolicy);
template.setBackOffPolicy(backOffPolicy);
return template;
}
And my rest service that i want to execute is below.
retryTemplate.execute(context -> {
System.out.println("inside retry method");
ResponseEntity<?> requestData = RestTemplateProvider.getInstance().postAsNewRequest(bundle, ServiceResponse.class, serivceURL,
CommonUtils.getHeader("APP_Name"));
_LOGGER.info("Response ..."+ requestData);
throw new IllegalStateException("Something went wrong");
});
You can also tackle this annotation-driven using Spring Retry. This way you will avoid to implement the template.
Add it to your pom.xml
<dependency>
<groupId>org.springframework.retry</groupId>
<artifactId>spring-retry</artifactId>
<version>1.1.2.RELEASE</version>
</dependency>
Enable it for your application/configuration
#SpringBootApplication
#EnableRetry
public class MyApplication {
//...
}
Guard methods that are in danger of failure with #Retryable
#Service
public class MyService {
#Retryable(maxAttempts=5, value = RuntimeException.class,
backoff = #Backoff(delay = 15000, multiplier = 2))
public List<String> doDangerousOperationWithExternalResource() {
// ...
}
}
Use Spring Retry project (https://dzone.com/articles/spring-retry-ways-integrate, https://github.com/spring-projects/spring-retry).
It is designed to solve problems like yours.
This approach saved my day.!!!Resilience saved my day.[Resilience retry][1]
RetryConfig config = RetryConfig.custom()
.maxAttempts(4)
.waitDuration(Duration.ofMillis(2000))
.failAfterMaxAttempts(true)
.build();
RetryRegistry registry = RetryRegistry.of(config);
HttpEntity<String> request =
new HttpEntity<>(body, headers);
Retry retry = registry.retry("notification-endpoint-"+System.currentTimeMillis());
AtomicReference<Integer> retries = new AtomicReference<>(0);
retry.getEventPublisher().onRetry(e -> {
log.info("Retrying here!!!. Count: {}", retries.updateAndGet(v -> v + 1));
}).onError(e->{
log.error("Failed to get to client.");
});
if(requestPojo.getMethod().equalsIgnoreCase("GET")) {
response = Retry.decorateCheckedSupplier(
retry,
() -> restTemplateConfig.restTemplate()
.exchange(url, HttpMethod.GET, request, String.class)).unchecked().get();
}
else if(requestPojo.getMethod().equalsIgnoreCase("POST")) {
response = Retry.decorateCheckedSupplier(
retry,
() -> restTemplateConfig.restTemplate()
.exchange(url, HttpMethod.POST, request, String.class)).unchecked().get();
}```
[1]: https://resilience4j.readme.io/docs/retry

How to use RestTemplate efficiently with RequestFactory?

I am working on a project in which I need to make a HTTP URL call to my server which is running Restful Service which returns back the response as a JSON String. I am using RestTemplate here along with HttpComponentsClientHttpRequestFactory to execute an url.
I have setup a http request timeout (READ and CONNECTION time out) on my RestTemplate by using HttpComponentsClientHttpRequestFactory.
Below is my Interface:
public interface Client {
// for synchronous
public String getSyncData(String key, long timeout);
// for asynchronous
public String getAsyncData(String key, long timeout);
}
Below is my implementation of Client interface -
public class DataClient implements Client {
private final RestTemplate restTemplate = new RestTemplate();
private ExecutorService executor = Executors.newFixedThreadPool(10);
// for synchronous call
#Override
public String getSyncData(String key, long timeout) {
String response = null;
try {
Task task = new Task(key, restTemplate, timeout);
// direct call, implementing sync call as async + waiting is bad idea.
// It is meaningless and consumes one thread from the thread pool per a call.
response = task.call();
} catch (Exception ex) {
PotoLogging.logErrors(ex, DataErrorEnum.CLIENT_ERROR, key);
}
return response;
}
// for asynchronous call
#Override
public Future<String> getAsyncData(String key, long timeout) {
Future<String> future = null;
try {
Task task = new Task(key, restTemplate, timeout);
future = executor.submit(task);
} catch (Exception ex) {
PotoLogging.logErrors(ex, DataErrorEnum.CLIENT_ERROR, key);
}
return future;
}
}
And below is my simple Task class
class Task implements Callable<String> {
private RestTemplate restTemplate;
private String key;
private long timeout; // in milliseconds
public Task(String key, RestTemplate restTemplate, long timeout) {
this.key = key;
this.restTemplate = restTemplate;
this.timeout = timeout;
}
public String call() throws Exception {
String url = "some_url_created_by_using_key";
// does this looks right the way I am setting request factory?
// or is there any other effficient way to do this?
restTemplate.setRequestFactory(clientHttpRequestFactory());
String response = restTemplate.exchange(url, HttpMethod.GET, null, String.class);
return response;
}
private static ClientHttpRequestFactory clientHttpRequestFactory() {
// is it ok to create a new instance of HttpComponentsClientHttpRequestFactory everytime?
HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory();
factory.setReadTimeout(timeout); // setting timeout as read timeout
factory.setConnectTimeout(timeout); // setting timeout as connect timeout
return factory;
}
}
Now my question is - Does the way I am using RestTemplate along with setRequestFactory in the call method of Task class everytime is efficient? Since RestTemplate is very heavy to be created so not sure whether I got it right.
And is it ok to create a new instance of HttpComponentsClientHttpRequestFactory everytime? Will it be expensive?
What is the right and efficient way to use RestTemplate if we need to setup Read and Connection timeout on it.
This library will be used like this -
String response = DataClientFactory.getInstance().getSyncData(keyData, 100);
From what I can tell, you're reusing the same RestTemplate object repeatedly, but each Task is performing this line: restTemplate.setRequestFactory(clientHttpRequestFactory());. This seems like it can have race conditions, e.g. one Task can set the RequestFactory that another Task will then accidentally use.
Otherwise, it seems like you're using RestTemplate correctly.
How often do your timeouts change? If you mostly use one or two timeouts, you can create one or two RestTemplates using the RequestFactory constructor with the pre-loaded timeout. If you're a stickler for efficiency, create a HashMap<Integer, RestTemplate> that caches a RestTemplate with a particular timeout each time a new timeout is requested.
Otherwise, looking at the code for RestTemplate's constructor, and for HttpComponentsClientHttpRequestFactory's constructor, they don't look exceptionally heavy, so calling them repeatedly probably won't be much of a bottleneck.

Intercept and retry call by means of OkHttp Interceptors

I need to retry request inside of OkHttp Interceptor. For example there is incoming request which needs Authorization token. If Authorization token is expired, server returns response with 403 code. In this case I am retrieving a new token and trying to make call again by using the same chain object.
But OkHttp throws an exception, which states that you cannot make two requests with the same chain object.
java.lang.IllegalStateException: network interceptor org.app.api.modules.ApplicationApiHeaders#559da2 must call proceed() exactly once
I wonder if there is a clean solution to this problem of retrying network request inside of OkHttp Interceptor?
public final class ApplicationApiHeaders implements Interceptor {
private static final String AUTHORIZATION = "Authorization";
private TokenProvider mProvider;
public ApplicationApiHeaders(TokenProvider provider) {
mProvider = provider;
}
#Override
public Response intercept(Chain chain) throws IOException {
Token token = mProvider.getApplicationToken();
String bearerToken = "Bearer " + token.getAccessToken();
System.out.println("Token: " + bearerToken);
Request request = chain.request();
request = request.newBuilder()
.addHeader(AUTHORIZATION, bearerToken)
.build();
Response response = chain.proceed(request);
if (!response.isSuccessful() && isForbidden(response.code())) {
Token freshToken = mProvider.invalidateAppTokenAndGetNew();
String freshBearerToken = freshToken.getAccessToken();
Request newRequest = chain.request();
newRequest = newRequest.newBuilder()
.addHeader(AUTHORIZATION, freshBearerToken)
.build();
response = chain.proceed(newRequest);
}
return response;
}
private static boolean isForbidden(int code) {
return code == HttpURLConnection.HTTP_FORBIDDEN;
}
}
Use .interceptors() instead of .networkInterceptors() which are allowed to call .proceed() more than once.
For more information see: https://square.github.io/okhttp/interceptors/

Categories

Resources