Skip to content

Commit 8c2f38c

Browse files
update exception handling in RetryHandler
1 parent 3d9f6ab commit 8c2f38c

File tree

2 files changed

+65
-47
lines changed

2 files changed

+65
-47
lines changed

examples/src/main/java/examples/basic/BasicSendWithRetry.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ public SendResponse RunExample () throws Exception {
2727
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("127.0.0.1", 4433));
2828

2929
// create the client
30-
SocketLabsClient client = new SocketLabsClient(ExampleConfig.ServerId, ExampleConfig.ApiKey, proxy);
30+
// SocketLabsClient client = new SocketLabsClient(37371, "q2QFw9b3KAr6e4S7DsWo");
31+
SocketLabsClient client = new SocketLabsClient(ExampleConfig.ServerId, ExampleConfig.ApiKey);
32+
client.setEndPointUrl("http://127.0.0.1:5000/");
3133
client.setRequestTimeout(5);
3234
client.setNumberOfRetries(2);
3335

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

Lines changed: 62 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
import java.io.IOException;
1212
import java.io.InterruptedIOException;
1313
import java.net.SocketTimeoutException;
14-
//import javax.ws.rs.ServerErrorException;
1514
import java.time.Duration;
1615
import java.util.Arrays;
1716
import java.util.HashSet;
@@ -52,16 +51,15 @@ public SendResponse send() throws IOException, InterruptedException {
5251
return parser.Parse(response);
5352
}
5453

55-
// int attempts = 0;
56-
5754
do {
55+
5856
Duration waitInterval = retrySettings.getNextWaitInterval(attempts);
5957

6058
try{
6159
Response response = httpRequest.SendRequest();
6260
System.out.println("RESPONSE : " + response.networkResponse().code());
6361
if (ErrorStatusCodes.contains(response.networkResponse().code()))
64-
throw new IOException();
62+
throw new IOException("Received Http Status Code : " + response.networkResponse().code());
6563
return parser.Parse(response);
6664
}
6765

@@ -87,7 +85,7 @@ public SendResponse send() throws IOException, InterruptedException {
8785
{
8886
attempts++;
8987
System.out.println("Exception Occured in IOException : " + exception + " Attempt : " + attempts);
90-
if (attempts > retrySettings.getMaximumNumberOfRetries()) throw new IOException();
88+
if (attempts > retrySettings.getMaximumNumberOfRetries()) throw new IOException(exception.getMessage());
9189
TimeUnit.MILLISECONDS.sleep(waitInterval.toMillis());
9290
}
9391

@@ -96,56 +94,74 @@ public SendResponse send() throws IOException, InterruptedException {
9694
}
9795

9896
public void sendAsync (final SendAsyncCallback callback) throws IOException, InterruptedException{
97+
9998
InjectionResponseParser parser = new InjectionResponseParser();
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-
}
99+
Duration waitInterval = retrySettings.getNextWaitInterval(attempts);
100+
final SendResponse[] sendResp = {new SendResponse()};
101+
102+
httpRequest.SendAsyncRequest(new Callback() {
103+
@Override
104+
public void onFailure(Call call, IOException e) {
105+
if(Exceptions.contains(e.getClass()) && attempts <= retrySettings.getMaximumNumberOfRetries()) {
106+
107+
System.out.println("Attempt : " + attempts);
108+
attempts++;
109+
System.out.println("onFailure Exception : " + e.getClass());
110+
111+
try {
112+
113+
TimeUnit.MILLISECONDS.sleep(waitInterval.toMillis());
114+
sendAsync(callback);
115+
116+
}
117+
118+
catch (IOException exception) {
119+
exception.printStackTrace();
120+
}
122121

122+
catch (InterruptedException interruptedException) {
123+
interruptedException.printStackTrace();
123124
}
124125

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-
}
126+
}
127+
128+
else {
129+
130+
attempts = retrySettings.getMaximumNumberOfRetries() + 1;
131+
System.out.println("Different Exception with attempt : " + attempts);
132+
callback.onError(e);
133+
134+
}
135+
136+
}
141137

138+
@Override
139+
public void onResponse(Call call, Response response) throws IOException {
142140

141+
if (ErrorStatusCodes.contains(response.networkResponse().code()) && attempts <= retrySettings.getMaximumNumberOfRetries()){
142+
143+
System.out.println("Attempt : " + attempts);
144+
attempts++;
145+
System.out.println("Response code : " + response.networkResponse().code());
146+
try {
147+
TimeUnit.MILLISECONDS.sleep(waitInterval.toMillis());
148+
sendAsync(callback);
149+
}
150+
catch (InterruptedException e) {
151+
e.printStackTrace();
143152
}
144-
});
145-
System.out.println("Attempt : " + attempts);
153+
154+
}
155+
156+
else {
157+
sendResp[0] = parser.Parse(response);
158+
System.out.println("Response : " + sendResp[0]);
159+
callback.onResponse(sendResp[0]);
160+
}
161+
146162
}
163+
});
147164

148-
} while (false);
149165
}
150166

151167
}

0 commit comments

Comments
 (0)