Skip to content
Snippets Groups Projects
Commit 886385ed authored by Harshika Dhoot's avatar Harshika Dhoot Committed by David Diederich
Browse files

Adding retry for 502 and 503 bad gateway error

(cherry picked from commit c676d589)
parent bd27a5fa
No related branches found
No related tags found
5 merge requests!604Merge Delta changes from M16 master to M18 master,!600Merge Delta changes from M16 master to M18 master,!599Merge Delta changes from M16 to M18 master,!598Merged from M16,!476Cherry-pick 'Adding retry for 502 and 503 bad gateway error' into release/0.19
Pipeline #165155 skipped
......@@ -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