Skip to content

Commit 5a12eb9

Browse files
author
Javen
committed
Distinguish 'read timed out', and add a state for APIConnectionExcepiton
1 parent a366fff commit 5a12eb9

File tree

3 files changed

+41
-10
lines changed

3 files changed

+41
-10
lines changed
Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,31 @@
11
package cn.jpush.api.common;
22

33
/**
4-
* Should retry for encountering this exception
4+
* Should retry for encountering this exception basically.
5+
* Normally it is due to:
6+
* 1. Connect timed out.
7+
* 2. Read timed out.
8+
* 3. Cannot parse domain.
9+
*
10+
* For Push action, if the exception is "Read timed out" you may not want to retry it.
511
*/
612
public class APIConnectionException extends Exception {
713
private static final long serialVersionUID = -2615370590441195647L;
14+
private boolean readTimedout = false;
815

916
public APIConnectionException(String message, Throwable e) {
1017
super(message, e);
1118
}
1219

13-
public APIConnectionException(Throwable e) {
14-
super(e);
20+
public APIConnectionException(String message, Throwable e, boolean readTimedout) {
21+
super(message, e);
22+
this.readTimedout = readTimedout;
1523
}
1624

17-
25+
public boolean isReadTimedout() {
26+
return readTimedout;
27+
}
28+
1829
}
1930

31+

src/cn/jpush/api/common/IHttpClient.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,16 @@ public enum RequestMethod {
2727
+ "Please ensure your internet connection is ok. \n"
2828
+ "If the problem persists, please let us know at [email protected].";
2929

30+
public static final String CONNECT_TIMED_OUT_MESSAGE = "connect timed out. \n"
31+
+ "Connect to JPush Server timed out, and already retried three times. "
32+
+ "Please ensure your internet connection is ok. \n"
33+
+ "If the problem persists, please let us know at [email protected].";
34+
35+
public static final String READ_TIMED_OUT_MESSAGE = "Read timed out. \n"
36+
+ "Read response from JPush Server timed out. For Push action, maybe you could not retry. "
37+
+ "Maybe JPush server is a little slowly. \n"
38+
+ "If the problem persists, please let us know at [email protected].";
39+
3040
public static Gson _gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();
3141

3242

src/cn/jpush/api/common/NativeHttpClient.java

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222

2323
public class NativeHttpClient implements IHttpClient {
2424
private static final Logger LOG = LoggerFactory.getLogger(NativeHttpClient.class);
25+
private static final String KEYWORDS_CONNECT_TIMED_OUT = "connect timed out";
26+
private static final String KEYWORDS_READ_TIMED_OUT = "Read timed out";
2527

2628
private int _maxRetryTimes = 0;
2729

@@ -53,11 +55,16 @@ public ResponseWrapper sendRequest(String url, String content,
5355
try {
5456
response = _sendRequest(url, content, method, authCode);
5557
break;
56-
} catch (SocketTimeoutException e) { // connect timed out
57-
if (retryTimes >= _maxRetryTimes) {
58-
throw new APIConnectionException(e);
59-
} else {
60-
LOG.debug("connect timed out - retry again - " + (retryTimes + 1));
58+
} catch (SocketTimeoutException e) {
59+
if (KEYWORDS_READ_TIMED_OUT.equals(e.getMessage())) {
60+
// Read timed out. For push, maybe should not re-send.
61+
throw new APIConnectionException(READ_TIMED_OUT_MESSAGE, e, true);
62+
} else { // connect timed out
63+
if (retryTimes >= _maxRetryTimes) {
64+
throw new APIConnectionException(CONNECT_TIMED_OUT_MESSAGE, e);
65+
} else {
66+
LOG.debug("connect timed out - retry again - " + (retryTimes + 1));
67+
}
6168
}
6269
}
6370
}
@@ -169,8 +176,10 @@ private ResponseWrapper _sendRequest(String url, String content,
169176
}
170177

171178
} catch (SocketTimeoutException e) {
172-
if (e.getMessage().contains("connect timed out")) {
179+
if (e.getMessage().contains(KEYWORDS_CONNECT_TIMED_OUT)) {
173180
throw e;
181+
} else if (e.getMessage().contains(KEYWORDS_READ_TIMED_OUT)) {
182+
throw new SocketTimeoutException(KEYWORDS_READ_TIMED_OUT);
174183
}
175184
LOG.debug(IO_ERROR_MESSAGE, e);
176185
throw new APIConnectionException(IO_ERROR_MESSAGE, e);

0 commit comments

Comments
 (0)