Skip to content

Commit de12788

Browse files
authored
Merge pull request #78 from jpush/dev
PushClient can switch to NettyHttpClient or NativeHttpClient
2 parents 161c6a0 + 54706cc commit de12788

File tree

4 files changed

+39
-18
lines changed

4 files changed

+39
-18
lines changed

example/main/java/cn/jpush/api/examples/PushExample.java

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import java.util.Map;
77

88
import cn.jiguang.common.ServiceHelper;
9+
import cn.jiguang.common.connection.NativeHttpClient;
910
import cn.jiguang.common.connection.NettyHttpClient;
1011
import cn.jiguang.common.resp.ResponseWrapper;
1112
import cn.jpush.api.push.model.notification.*;
@@ -46,7 +47,8 @@ public class PushExample {
4647
public static void main(String[] args) {
4748
// testSendPushWithCustomConfig();
4849
// testSendIosAlert();
49-
testSendPush();
50+
// testSendPush();
51+
testSendPushes();
5052
// testSendPush_fromJSON();
5153
// testSendPushWithCallback();
5254
// testSendPushesWithMultiCallback();
@@ -134,15 +136,22 @@ public static void testSendPush() {
134136
// Can use this https proxy: https://github.com/Exa-Networks/exaproxy
135137
ClientConfig clientConfig = ClientConfig.getInstance();
136138
JPushClient jpushClient = new JPushClient(MASTER_SECRET, APP_KEY, null, clientConfig);
137-
139+
String authCode = ServiceHelper.getBasicAuthorization(APP_KEY, MASTER_SECRET);
140+
// Here you can use NativeHttpClient or NettyHttpClient.
141+
NativeHttpClient httpClient = new NativeHttpClient(authCode, null, clientConfig);
142+
// Call setHttpClient to set httpClient,
143+
// If you don't invoke this method, default httpClient will use NativeHttpClient.
144+
jpushClient.getPushClient().setHttpClient(httpClient);
145+
146+
138147
// For push, all you need do is to build PushPayload object.
139148
PushPayload payload = buildPushObject_all_alias_alert();
140149
try {
141150
PushResult result = jpushClient.sendPush(payload);
142151
LOG.info("Got result - " + result);
143-
Thread.sleep(5000);
144-
//如果使用 NettyHttpClient,需要手动调用 close 方法退出进程
145-
// jpushClient.close();
152+
// 如果使用 NettyHttpClient,需要手动调用 close 方法退出进程
153+
// If uses NettyHttpClient, call close when finished sending request, otherwise process will not exit.
154+
// jpushClient.close();
146155
} catch (APIConnectionException e) {
147156
LOG.error("Connection error. Should retry later. ", e);
148157
LOG.error("Sendno: " + payload.getSendno());
@@ -154,8 +163,6 @@ public static void testSendPush() {
154163
LOG.info("Error Message: " + e.getErrorMessage());
155164
LOG.info("Msg ID: " + e.getMsgId());
156165
LOG.error("Sendno: " + payload.getSendno());
157-
} catch (InterruptedException e) {
158-
e.printStackTrace();
159166
}
160167
}
161168

@@ -185,6 +192,10 @@ public static void testSendPush_fromJSON() {
185192
LOG.info("Msg ID: " + e.getMsgId());
186193
LOG.error("Sendno: " + payload.getSendno());
187194
}
195+
}
196+
197+
public static void testSendPushes() {
198+
188199
}
189200

190201
public static PushPayload buildPushObject_all_all_alert() {

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
<dependency>
4343
<groupId>cn.jpush.api</groupId>
4444
<artifactId>jiguang-common</artifactId>
45-
<version>1.0.2</version>
45+
<version>1.0.3</version>
4646
</dependency>
4747
<dependency>
4848
<groupId>io.netty</groupId>

src/main/java/cn/jpush/api/JPushClient.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,11 @@ public JPushClient(String masterSecret, String appKey, boolean apnsProduction, l
175175
_deviceClient = new DeviceClient(masterSecret, appKey);
176176
_scheduleClient = new ScheduleClient(masterSecret, appKey);
177177
}
178+
179+
public PushClient getPushClient() {
180+
return _pushClient;
181+
}
182+
178183
// ----------------------------- Push API
179184

180185
/**
@@ -1179,8 +1184,8 @@ private ScheduleResult createPeriodicalSchedule(String name, String start, Strin
11791184
return _scheduleClient.createSchedule(payload);
11801185
}
11811186

1182-
// public void close() {
1183-
// _pushClient.close();
1184-
// }
1187+
public void close() {
1188+
_pushClient.close();
1189+
}
11851190
}
11861191

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

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package cn.jpush.api.push;
22

3+
import cn.jiguang.common.connection.IHttpClient;
4+
import cn.jiguang.common.connection.NativeHttpClient;
35
import cn.jiguang.common.connection.NettyHttpClient;
46
import com.google.gson.JsonParseException;
57
import com.google.gson.JsonParser;
@@ -9,7 +11,6 @@
911
import cn.jiguang.common.utils.Preconditions;
1012
import cn.jiguang.common.utils.StringUtils;
1113
import cn.jiguang.common.connection.HttpProxy;
12-
import cn.jiguang.common.connection.NativeHttpClient;
1314
import cn.jiguang.common.resp.APIConnectionException;
1415
import cn.jiguang.common.resp.APIRequestException;
1516
import cn.jiguang.common.resp.BaseResult;
@@ -28,7 +29,7 @@
2829
*/
2930
public class PushClient {
3031

31-
private final NativeHttpClient _httpClient;
32+
private IHttpClient _httpClient;
3233
private String _baseUrl;
3334
private String _pushPath;
3435
private String _pushValidatePath;
@@ -210,12 +211,16 @@ public PushResult sendPushValidate(String payloadString) throws APIConnectionExc
210211
return BaseResult.fromResponse(response, PushResult.class);
211212
}
212213

214+
public void setHttpClient(IHttpClient client) {
215+
this._httpClient = client;
216+
}
217+
213218
// 如果使用 NettyHttpClient,在发送请求后需要手动调用 close 方法
214-
// public void close() {
215-
// if (_httpClient instanceof NettyHttpClient) {
216-
// _httpClient.close();
217-
// }
218-
// }
219+
public void close() {
220+
if (_httpClient != null && _httpClient instanceof NettyHttpClient) {
221+
((NettyHttpClient) _httpClient).close();
222+
}
223+
}
219224
}
220225

221226

0 commit comments

Comments
 (0)