Skip to content
Snippets Groups Projects
Commit 7e05ff57 authored by preeti singh[Microsoft]'s avatar preeti singh[Microsoft]
Browse files

Merge branch 'hdhoot-adding-ret' into 'master'

Adding retry for 502 and 503 bad gateway error

See merge request !450
parents a0d85e72 c676d589
No related branches found
No related tags found
1 merge request!450Adding retry for 502 and 503 bad gateway error
Pipeline #164870 failed
......@@ -14,10 +14,15 @@
package org.opengroup.osdu.util;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
import lombok.ToString;
import lombok.extern.java.Log;
import javax.ws.rs.core.Response;
import java.net.SocketTimeoutException;
import java.util.Map;
@Log
@ToString
public class AzureHTTPClient extends HTTPClient {
......@@ -35,4 +40,43 @@ public class AzureHTTPClient extends HTTPClient {
}
return token;
}
}
\ No newline at end of file
public ClientResponse send(String httpMethod, String url, String payLoad, Map<String, String> headers, String token) {
ClientResponse response;
System.out.println("in Azure send method");
String correlationId = java.util.UUID.randomUUID().toString();
log.info(String.format("Request correlation id: %s", correlationId));
headers.put(HEADER_CORRELATION_ID, correlationId);
Client client = getClient();
client.setReadTimeout(300000);
client.setConnectTimeout(300000);
log.info(String.format("httpMethod: %s", httpMethod));
log.info(String.format("payLoad: %s", payLoad));
log.info(String.format("headers: %s", headers));
log.info(String.format("URL: %s", url));
WebResource webResource = client.resource(url);
log.info("waiting on response in azure send");
int retryCount = 2;
try{
response = this.getClientResponse(httpMethod, payLoad, webResource, headers, token);
while (retryCount > 0) {
if (response.getStatusInfo().getFamily().equals(Response.Status.Family.valueOf("SERVER_ERROR"))) {
log.info(String.format("got resoponse : %s", response.getStatusInfo()));
Thread.sleep(5000);
log.info(String.format("Retrying --- "));
response = this.getClientResponse(httpMethod, payLoad, webResource, headers, token);
} else
break;
retryCount--;
}
System.out.println("sending response from azure send method");
return response;
} catch (Exception e) {
if (e.getCause() instanceof SocketTimeoutException) {
System.out.println("Retrying in case of socket timeout exception");
return this.getClientResponse(httpMethod, payLoad, webResource, headers, token);
}
e.printStackTrace();
throw new AssertionError("Error: Send request error", e);
}
}
}
......@@ -24,11 +24,11 @@ public abstract class HTTPClient {
private static Random random = new Random();
private final int MAX_ID_SIZE = 50;
private static final String HEADER_CORRELATION_ID = "correlation-id";
protected static final String HEADER_CORRELATION_ID = "correlation-id";
public abstract String getAccessToken();
private static Client getClient() {
protected static Client getClient() {
TrustManager[] trustAllCerts = new TrustManager[]{new X509TrustManager() {
@Override
public X509Certificate[] getAcceptedIssuers() {
......@@ -73,7 +73,7 @@ public abstract class HTTPClient {
return response;
}
private ClientResponse getClientResponse(String httpMethod, String requestBody, WebResource webResource, Map<String, String> headers, String token) {
protected ClientResponse getClientResponse(String httpMethod, String requestBody, WebResource webResource, Map<String, String> headers, String token) {
final WebResource.Builder builder = webResource.accept(MediaType.APPLICATION_JSON).type(MediaType.APPLICATION_JSON).header("Authorization", token);
headers.forEach(builder::header);
log.info("making request to datalake api");
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment