Skip to content

Commit 3d9f6ab

Browse files
update sendAsync to retry
1 parent 93a7865 commit 3d9f6ab

File tree

1 file changed

+51
-23
lines changed

1 file changed

+51
-23
lines changed

injectionApi/src/main/java/com/socketLabs/injectionApi/core/RetryHandler.java

Lines changed: 51 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ public class RetryHandler {
2323
private HttpRequest httpRequest;
2424
private String endPointUrl;
2525
private RetrySettings retrySettings;
26+
private int attempts = 0;
2627

2728
private Set<Integer> ErrorStatusCodes = new HashSet<>(Arrays.asList(500, 502, 503, 504));
2829
private Set<Class< ? extends Exception>> Exceptions = new HashSet<Class< ? extends Exception>>(Arrays.asList(
@@ -51,7 +52,7 @@ public SendResponse send() throws IOException, InterruptedException {
5152
return parser.Parse(response);
5253
}
5354

54-
int attempts = 0;
55+
// int attempts = 0;
5556

5657
do {
5758
Duration waitInterval = retrySettings.getNextWaitInterval(attempts);
@@ -61,7 +62,6 @@ public SendResponse send() throws IOException, InterruptedException {
6162
System.out.println("RESPONSE : " + response.networkResponse().code());
6263
if (ErrorStatusCodes.contains(response.networkResponse().code()))
6364
throw new IOException();
64-
// javax.ws.rs.ServerErrorException
6565
return parser.Parse(response);
6666
}
6767

@@ -97,27 +97,55 @@ public SendResponse send() throws IOException, InterruptedException {
9797

9898
public void sendAsync (final SendAsyncCallback callback) throws IOException, InterruptedException{
9999
InjectionResponseParser parser = new InjectionResponseParser();
100-
if (retrySettings.getMaximumNumberOfRetries() == 0){
101-
System.out.println("Without retries");
102-
final SendResponse[] sendResp = {new SendResponse()};
103-
httpRequest.SendAsyncRequest(new Callback() {
104-
@Override
105-
public void onFailure(Call call, IOException e) {
106-
System.out.println("onFailure Call : " + call);
107-
if(Exceptions.contains(e.getClass()))
108-
System.out.println("onFailure Exception : " + e.getClass());
109-
callback.onError(e);
110-
}
111-
112-
@Override
113-
public void onResponse(Call call, Response response) throws IOException {
114-
sendResp[0] = parser.Parse(response);
115-
callback.onResponse(sendResp[0]);
116-
117-
118-
}
119-
});
120-
}
100+
do {
101+
if(attempts <= retrySettings.getMaximumNumberOfRetries()){
102+
final SendResponse[] sendResp = {new SendResponse()};
103+
httpRequest.SendAsyncRequest(new Callback() {
104+
@Override
105+
public void onFailure(Call call, IOException e) {
106+
if(Exceptions.contains(e.getClass())){
107+
attempts++;
108+
System.out.println("onFailure Exception : " + e.getClass());
109+
try {
110+
sendAsync(callback);
111+
} catch (IOException exception) {
112+
exception.printStackTrace();
113+
} catch (InterruptedException interruptedException) {
114+
interruptedException.printStackTrace();
115+
}
116+
}
117+
else{
118+
attempts = retrySettings.getMaximumNumberOfRetries() + 1;
119+
System.out.println("Different Exception with attempt : " + attempts);
120+
callback.onError(e);
121+
}
122+
123+
}
124+
125+
@Override
126+
public void onResponse(Call call, Response response) throws IOException {
127+
System.out.println("Response code : " + response.networkResponse().code());
128+
if (ErrorStatusCodes.contains(response.networkResponse().code())){
129+
attempts++;
130+
try {
131+
sendAsync(callback);
132+
} catch (InterruptedException e) {
133+
e.printStackTrace();
134+
}
135+
}
136+
else {
137+
sendResp[0] = parser.Parse(response);
138+
System.out.println("Response : " + sendResp[0]);
139+
callback.onResponse(sendResp[0]);
140+
}
141+
142+
143+
}
144+
});
145+
System.out.println("Attempt : " + attempts);
146+
}
147+
148+
} while (false);
121149
}
122150

123151
}

0 commit comments

Comments
 (0)