Skip to content
This repository was archived by the owner on Mar 7, 2025. It is now read-only.

Commit 9a74701

Browse files
committed
first version of http request retry system
1 parent 52e01a8 commit 9a74701

File tree

2 files changed

+39
-3
lines changed

2 files changed

+39
-3
lines changed

src/main/java/com/rollbar/android/http/HttpRequest.java

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,17 @@ public class HttpRequest implements Runnable {
2424

2525
private String method;
2626
private byte[] body;
27+
28+
private int attempt;
2729

2830
public HttpRequest(URL url, String method, HttpResponseHandler handler) {
2931
this.url = url;
3032
this.method = method;
3133
this.handler = handler;
3234

3335
this.requestProperties = new HashMap<String, String>();
36+
37+
attempt = 1;
3438
}
3539

3640
public void setMethod(String method) {
@@ -54,7 +58,7 @@ public void run() {
5458
try {
5559
connection = (HttpURLConnection) url.openConnection();
5660
} catch (IOException e) {
57-
handler.onFailure(new HttpResponse(e.toString()));
61+
onFailure(new HttpResponse(e.toString()));
5862
return;
5963
}
6064

@@ -88,14 +92,28 @@ public void run() {
8892
if (responseCode == 200) {
8993
handler.onSuccess(response);
9094
} else {
91-
handler.onFailure(response);
95+
onFailure(response);
9296
}
9397
} catch (IOException e) {
94-
handler.onFailure(new HttpResponse(e.toString()));
98+
onFailure(new HttpResponse(e.toString()));
9599
} finally {
96100
connection.disconnect();
97101
}
98102
}
103+
104+
private void onFailure(HttpResponse response) {
105+
if (attempt < HttpRequestManager.MAX_RETRIES) {
106+
attempt++;
107+
108+
HttpRequestManager.getInstance().retryRequest(this);
109+
} else {
110+
handler.onFailure(response);
111+
}
112+
}
113+
114+
public int getAttempt() {
115+
return attempt;
116+
}
99117

100118
private String getResponseText(InputStream in) throws IOException {
101119
byte[] contents = new byte[1024];

src/main/java/com/rollbar/android/http/HttpRequestManager.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,22 @@
33
import java.net.MalformedURLException;
44
import java.net.URL;
55
import java.util.concurrent.Executors;
6+
import java.util.concurrent.ScheduledExecutorService;
67
import java.util.concurrent.ThreadPoolExecutor;
8+
import java.util.concurrent.TimeUnit;
79

810
import org.json.JSONObject;
911

1012
public class HttpRequestManager {
13+
public static final int MAX_RETRIES = 5;
14+
1115
private static HttpRequestManager instance = null;
1216
private ThreadPoolExecutor executor;
17+
private ScheduledExecutorService service;
1318

1419
private HttpRequestManager() {
1520
executor = (ThreadPoolExecutor) Executors.newCachedThreadPool();
21+
service = Executors.newSingleThreadScheduledExecutor();
1622
}
1723

1824
public static HttpRequestManager getInstance() {
@@ -41,4 +47,16 @@ public void postJson(String urlString, JSONObject json, HttpResponseHandler hand
4147

4248
executor.execute(request);
4349
}
50+
51+
public void retryRequest(final HttpRequest request) {
52+
int retryDelay = request.getAttempt();
53+
54+
service.schedule(new Runnable() {
55+
56+
@Override
57+
public void run() {
58+
executor.execute(request);
59+
}
60+
}, retryDelay, TimeUnit.SECONDS);
61+
}
4462
}

0 commit comments

Comments
 (0)