66import java .util .Map ;
77
88import cn .jiguang .common .ServiceHelper ;
9+ import cn .jiguang .common .connection .ApacheHttpClient ;
910import cn .jiguang .common .connection .NativeHttpClient ;
1011import cn .jiguang .common .connection .NettyHttpClient ;
1112import cn .jiguang .common .resp .ResponseWrapper ;
@@ -43,15 +44,16 @@ public class PushExample {
4344 public static final String MSG_CONTENT = "Test from API Example - msgContent" ;
4445 public static final String REGISTRATION_ID = "0900e8d85ef" ;
4546 public static final String TAG = "tag_api" ;
47+ public static long sendCount = 0 ;
48+ private static long sendTotalTime = 0 ;
4649
4750 public static void main (String [] args ) {
4851// testSendPushWithCustomConfig();
4952// testSendIosAlert();
50- // testSendPush();
51- testSendPushes ();
53+ testSendPush ();
54+ // testSendPushes();
5255// testSendPush_fromJSON();
5356// testSendPushWithCallback();
54- // testSendPushesWithMultiCallback();
5557 }
5658
5759 // 使用 NettyHttpClient 异步接口发送请求
@@ -74,96 +76,69 @@ public void onSucceed(ResponseWrapper responseWrapper) {
7476 }
7577 }
7678
77- public static void testSendPushesWithMultiCallback () {
78- NettyHttpClient client = new NettyHttpClient (ServiceHelper .getBasicAuthorization (APP_KEY , MASTER_SECRET ),
79- null , ClientConfig .getInstance ());
80- String host = (String ) ClientConfig .getInstance ().get (ClientConfig .PUSH_HOST_NAME );
81- URI uri = null ;
82- try {
83- uri = new URI (host + (String ) ClientConfig .getInstance ().get (ClientConfig .PUSH_PATH ));
84- PushPayload payload = PushPayload .alertAll ("test" );
85- System .out .println (payload .toString ());
86- NettyHttpClient .BaseCallback callback1 = new NettyHttpClient .BaseCallback () {
87- @ Override
88- public void onSucceed (ResponseWrapper responseWrapper ) {
89- System .out .println ("callback1 Got result: " + responseWrapper .responseContent );
90- }
91- };
92- NettyHttpClient .BaseCallback callback2 = new NettyHttpClient .BaseCallback () {
93- @ Override
94- public void onSucceed (ResponseWrapper responseWrapper ) {
95- System .out .println ("callback2 Got result: " + responseWrapper .responseContent );
96- }
97- };
98- MyThread thread1 = new MyThread (client , callback1 );
99- MyThread thread2 = new MyThread (client , callback2 );
100- thread1 .start ();
101- thread2 .start ();
102- } catch (URISyntaxException e ) {
103- e .printStackTrace ();
104- }
105- }
106-
107- private static class MyThread extends Thread {
108-
109- private NettyHttpClient client ;
110- private NettyHttpClient .BaseCallback callback ;
111-
112- public MyThread (NettyHttpClient client , NettyHttpClient .BaseCallback callback ) {
113- this .client = client ;
114- this .callback = callback ;
115- }
116-
117- @ Override
118- public void run () {
119- // super.run();
120- System .out .println ("running send push" );
121- try {
122- String host = (String ) ClientConfig .getInstance ().get (ClientConfig .PUSH_HOST_NAME );
123- URI uri = new URI (host + (String ) ClientConfig .getInstance ().get (ClientConfig .PUSH_PATH ));
124- PushPayload payload = PushPayload .alertAll ("test" );
125- System .out .println (payload .toString ());
126- client .sendRequest (HttpMethod .POST , payload .toString (), uri , callback );
127- } catch (URISyntaxException e ) {
128- e .printStackTrace ();
129- }
130- }
131- }
132-
133-
13479 public static void testSendPush () {
13580 // HttpProxy proxy = new HttpProxy("localhost", 3128);
13681 // Can use this https proxy: https://github.com/Exa-Networks/exaproxy
13782 ClientConfig clientConfig = ClientConfig .getInstance ();
138- JPushClient jpushClient = new JPushClient (MASTER_SECRET , APP_KEY , null , clientConfig );
83+ final JPushClient jpushClient = new JPushClient (MASTER_SECRET , APP_KEY , null , clientConfig );
13984 String authCode = ServiceHelper .getBasicAuthorization (APP_KEY , MASTER_SECRET );
14085 // Here you can use NativeHttpClient or NettyHttpClient.
14186 NativeHttpClient httpClient = new NativeHttpClient (authCode , null , clientConfig );
14287 // Call setHttpClient to set httpClient,
14388 // If you don't invoke this method, default httpClient will use NativeHttpClient.
89+ // ApacheHttpClient httpClient = new ApacheHttpClient(authCode, null, clientConfig);
14490 jpushClient .getPushClient ().setHttpClient (httpClient );
145-
146-
147- // For push, all you need do is to build PushPayload object.
148- PushPayload payload = buildPushObject_all_alias_alert ();
149- try {
150- PushResult result = jpushClient .sendPush (payload );
151- LOG .info ("Got result - " + result );
152- // 如果使用 NettyHttpClient,需要手动调用 close 方法退出进程
153- // If uses NettyHttpClient, call close when finished sending request, otherwise process will not exit.
154- // jpushClient.close();
155- } catch (APIConnectionException e ) {
156- LOG .error ("Connection error. Should retry later. " , e );
157- LOG .error ("Sendno: " + payload .getSendno ());
158-
159- } catch (APIRequestException e ) {
160- LOG .error ("Error response from JPush server. Should review and fix it. " , e );
161- LOG .info ("HTTP Status: " + e .getStatus ());
162- LOG .info ("Error Code: " + e .getErrorCode ());
163- LOG .info ("Error Message: " + e .getErrorMessage ());
164- LOG .info ("Msg ID: " + e .getMsgId ());
165- LOG .error ("Sendno: " + payload .getSendno ());
91+ final PushPayload payload = buildPushObject_android_newly_support ();
92+ for (int i =0 ;i <10 ;i ++) {
93+ Thread thread = new Thread () {
94+ public void run () {
95+ for (int j = 0 ; j < 200 ; j ++) {
96+ long start = System .currentTimeMillis ();
97+ try {
98+ jpushClient .sendPush (payload );
99+ PushResult result = jpushClient .sendPush (payload );
100+ LOG .info ("Got result - " + result );
101+
102+ } catch (APIConnectionException e ) {
103+ LOG .error ("Connection error. Should retry later. " , e );
104+ LOG .error ("Sendno: " + payload .getSendno ());
105+
106+ } catch (APIRequestException e ) {
107+ LOG .error ("Error response from JPush server. Should review and fix it. " , e );
108+ LOG .info ("HTTP Status: " + e .getStatus ());
109+ LOG .info ("Error Code: " + e .getErrorCode ());
110+ LOG .info ("Error Message: " + e .getErrorMessage ());
111+ LOG .info ("Msg ID: " + e .getMsgId ());
112+ LOG .error ("Sendno: " + payload .getSendno ());
113+ }
114+
115+ System .out .println ("耗时" + (System .currentTimeMillis () - start ) + "毫秒 sendCount:" + (++sendCount ));
116+ }
117+ }
118+ };
119+ thread .start ();
166120 }
121+
122+ // // For push, all you need do is to build PushPayload object.
123+ // PushPayload payload = buildPushObject_all_alias_alert();
124+ // try {
125+ // PushResult result = jpushClient.sendPush(payload);
126+ // LOG.info("Got result - " + result);
127+ // // 如果使用 NettyHttpClient,需要手动调用 close 方法退出进程
128+ // // If uses NettyHttpClient, call close when finished sending request, otherwise process will not exit.
129+ // // jpushClient.close();
130+ // } catch (APIConnectionException e) {
131+ // LOG.error("Connection error. Should retry later. ", e);
132+ // LOG.error("Sendno: " + payload.getSendno());
133+ //
134+ // } catch (APIRequestException e) {
135+ // LOG.error("Error response from JPush server. Should review and fix it. ", e);
136+ // LOG.info("HTTP Status: " + e.getStatus());
137+ // LOG.info("Error Code: " + e.getErrorCode());
138+ // LOG.info("Error Message: " + e.getErrorMessage());
139+ // LOG.info("Msg ID: " + e.getMsgId());
140+ // LOG.error("Sendno: " + payload.getSendno());
141+ // }
167142 }
168143
169144 //use String to build PushPayload instance
0 commit comments