Skip to content

Commit e1b801c

Browse files
author
Javen
committed
New feature: can set max retry times for API request. Default is 0.
1 parent 010474e commit e1b801c

File tree

7 files changed

+98
-24
lines changed

7 files changed

+98
-24
lines changed

src/cn/jpush/api/JPushClient.java

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ public JPushClient(String masterSecret, String appKey) {
2929
_reportClient = new ReportClient(masterSecret, appKey);
3030
}
3131

32+
public JPushClient(String masterSecret, String appKey, int maxRetryTimes) {
33+
_pushClient = new PushClient(masterSecret, appKey, maxRetryTimes);
34+
_reportClient = new ReportClient(masterSecret, appKey, maxRetryTimes);
35+
}
36+
3237
/**
3338
* Create a JPush Client with global settings.
3439
*
@@ -43,27 +48,43 @@ public JPushClient(String masterSecret, String appKey, boolean apnsProduction, l
4348
_pushClient = new PushClient(masterSecret, appKey, apnsProduction, timeToLive);
4449
_reportClient = new ReportClient(masterSecret, appKey);
4550
}
46-
51+
4752
/**
48-
* Send a push
53+
* Send a push with object.
4954
*
50-
* @param pushPayload payload of a push.
51-
* @return PushResult. Can be printed to a JSON.
55+
* @param pushPayload payload object of a push.
56+
* @return PushResult The result object of a Push. Can be printed to a JSON.
57+
* @throws APIConnectionException
58+
* @throws APIRequestException
5259
*/
5360
public PushResult sendPush(PushPayload pushPayload) throws APIConnectionException, APIRequestException {
5461
return _pushClient.sendPush(pushPayload);
5562
}
5663

57-
public PushResult sendPush(String payloadString) throws APIConnectionException, APIRequestException {
58-
return _pushClient.sendPush(payloadString);
59-
}
60-
6164
/**
62-
* Get received report.
65+
* Send a push with JSON string.
66+
*
67+
* You can send a push JSON string directly with this method.
6368
*
64-
* @param msgIds 100 msgids to batch getting is supported.
65-
* @return ReceivedResult. Can be printed to JSON.
69+
* Attention: globally settings cannot be affect this type of Push.
70+
*
71+
* @param pushPayload payload of a push.
72+
* @return PushResult. Can be printed to a JSON.
73+
* @throws APIConnectionException
74+
* @throws APIRequestException
6675
*/
76+
public PushResult sendPush(String payloadString) throws APIConnectionException, APIRequestException {
77+
return _pushClient.sendPush(payloadString);
78+
}
79+
80+
/**
81+
* Get received report.
82+
*
83+
* @param msgIds 100 msgids to batch getting is supported.
84+
* @return ReceivedResult. Can be printed to JSON.
85+
* @throws APIConnectionException
86+
* @throws APIRequestException
87+
*/
6788
public ReceivedsResult getReportReceiveds(String msgIds) throws APIConnectionException, APIRequestException {
6889
return _reportClient.getReceiveds(msgIds);
6990
}

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,14 @@
66
public class APIConnectionException extends Exception {
77
private static final long serialVersionUID = -2615370590441195647L;
88

9-
public APIConnectionException(String message) {
10-
super(message);
11-
}
12-
139
public APIConnectionException(String message, Throwable e) {
1410
super(message, e);
1511
}
1612

13+
public APIConnectionException(Throwable e) {
14+
super(e);
15+
}
16+
17+
1718
}
1819

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,12 @@ public enum RequestMethod {
3131

3232

3333
//设置连接超时时间
34-
public final int DEFAULT_CONNECTION_TIMEOUT = (5 * 1000); // milliseconds
34+
public static final int DEFAULT_CONNECTION_TIMEOUT = (5 * 1000); // milliseconds
3535

3636
//设置读取超时时间
37-
public final int DEFAULT_READ_TIMEOUT = (30 * 1000); // milliseconds
37+
public static final int DEFAULT_READ_TIMEOUT = (30 * 1000); // milliseconds
38+
39+
public static final int DEFAULT_MAX_RETRY_TIMES = 0;
3840

3941
public ResponseWrapper sendGet(String url, String params,
4042
String authCode) throws APIConnectionException, APIRequestException;

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

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,17 @@
2121
public class NativeHttpClient implements IHttpClient {
2222
private static final Logger LOG = LoggerFactory.getLogger(NativeHttpClient.class);
2323

24-
24+
private int _maxRetryTimes = 0;
25+
26+
public NativeHttpClient() {
27+
this(DEFAULT_MAX_RETRY_TIMES);
28+
}
29+
30+
public NativeHttpClient(int maxRetryTimes) {
31+
this._maxRetryTimes = maxRetryTimes;
32+
LOG.info("Created instance with _maxRetryTimes = " + _maxRetryTimes);
33+
}
34+
2535
public ResponseWrapper sendGet(String url, String params,
2636
String authCode) throws APIConnectionException, APIRequestException {
2737
return sendRequest(url, params, RequestMethod.GET, authCode);
@@ -34,6 +44,24 @@ public ResponseWrapper sendPost(String url, String content,
3444

3545
public ResponseWrapper sendRequest(String url, String content,
3646
RequestMethod method, String authCode) throws APIConnectionException, APIRequestException {
47+
ResponseWrapper response = null;
48+
for (int retryTimes = 0; ; retryTimes++) {
49+
try {
50+
response = _sendRequest(url, content, method, authCode);
51+
break;
52+
} catch (APIConnectionException e) {
53+
if (retryTimes >= _maxRetryTimes) {
54+
throw new APIConnectionException(e);
55+
} else {
56+
LOG.debug("Retry again - " + (retryTimes + 1));
57+
}
58+
}
59+
}
60+
return response;
61+
}
62+
63+
private ResponseWrapper _sendRequest(String url, String content,
64+
RequestMethod method, String authCode) throws APIConnectionException, APIRequestException {
3765
LOG.debug("Send request to - " + url);
3866
if (null != content) {
3967
LOG.debug("Request Content - " + content);

src/cn/jpush/api/examples/PushExample.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public static void main(String[] args) {
3535

3636

3737
public static void testSendPush() {
38-
JPushClient jpushClient = new JPushClient(masterSecret, appKey);
38+
JPushClient jpushClient = new JPushClient(masterSecret, appKey, 3);
3939

4040
// For push, all you need do is to build PushPayload object.
4141
PushPayload payload = buildPushObject_all_all_alert();

src/cn/jpush/api/push/PushClient.java

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import cn.jpush.api.common.APIConnectionException;
44
import cn.jpush.api.common.APIRequestException;
5+
import cn.jpush.api.common.IHttpClient;
56
import cn.jpush.api.common.NativeHttpClient;
67
import cn.jpush.api.common.ResponseWrapper;
78
import cn.jpush.api.common.ServiceHelper;
@@ -21,7 +22,7 @@ public class PushClient {
2122
public static String HOST_NAME_SSL = "https://api.jpush.cn";
2223
public static final String PUSH_PATH = "/v3/push";
2324

24-
private NativeHttpClient _httpClient = new NativeHttpClient();;
25+
private final NativeHttpClient _httpClient;
2526

2627
// The API secret of the appKey. Please get it from JPush Web Portal
2728
private final String _masterSecret;
@@ -49,14 +50,27 @@ public class PushClient {
4950
* @param appKey The KEY of one application on JPush.
5051
*/
5152
public PushClient(String masterSecret, String appKey) {
53+
this(masterSecret, appKey, IHttpClient.DEFAULT_MAX_RETRY_TIMES);
54+
}
55+
56+
/**
57+
* Create a Push Client with max retry times.
58+
*
59+
* @param masterSecret API access secret of the appKey.
60+
* @param appKey The KEY of one application on JPush.
61+
* @param masxRetryTimes
62+
*/
63+
public PushClient(String masterSecret, String appKey, int masxRetryTimes) {
5264
this._masterSecret = masterSecret;
5365
this._appKey = appKey;
5466

67+
ServiceHelper.checkBasic(appKey, masterSecret);
68+
5569
this._authCode = ServiceHelper.getAuthorizationBase64(_appKey, _masterSecret);
5670
this._baseUrl = HOST_NAME_SSL + PUSH_PATH;
57-
ServiceHelper.checkBasic(appKey, masterSecret);
71+
this._httpClient = new NativeHttpClient(masxRetryTimes);
5872
}
59-
73+
6074
/**
6175
* Create a Push Client with global settings.
6276
*

src/cn/jpush/api/report/ReportClient.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import cn.jpush.api.common.APIConnectionException;
77
import cn.jpush.api.common.APIRequestException;
8+
import cn.jpush.api.common.IHttpClient;
89
import cn.jpush.api.common.NativeHttpClient;
910
import cn.jpush.api.common.ResponseWrapper;
1011
import cn.jpush.api.common.ServiceHelper;
@@ -17,17 +18,24 @@ public class ReportClient {
1718
private static final String REPORT_USER_PATH = "/v3/users";
1819
private static final String REPORT_MESSAGE_PATH = "/v3/messages";
1920

20-
private NativeHttpClient _httpClient = new NativeHttpClient();;
21+
private final NativeHttpClient _httpClient;
2122

2223
private String _masterSecret;
2324
private String _appKey;
2425

25-
public ReportClient(String masterSecret, String appKey) {
26+
public ReportClient(String masterSecret, String appKey, int maxRetryTimes) {
2627
this._masterSecret = masterSecret;
2728
this._appKey = appKey;
29+
2830
ServiceHelper.checkBasic(appKey, masterSecret);
31+
32+
_httpClient = new NativeHttpClient(maxRetryTimes);
2933
}
3034

35+
public ReportClient(String masterSecret, String appKey) {
36+
this(masterSecret, appKey, IHttpClient.DEFAULT_MAX_RETRY_TIMES);
37+
}
38+
3139

3240
public ReceivedsResult getReceiveds(String[] msgIdArray) throws APIConnectionException, APIRequestException {
3341
return getReceiveds(StringUtils.arrayToString(msgIdArray));

0 commit comments

Comments
 (0)