Skip to content

Commit 1204db1

Browse files
author
yhl
committed
update javasdk 4.1.2
1 parent 16d8283 commit 1204db1

File tree

12 files changed

+174
-115
lines changed

12 files changed

+174
-115
lines changed

AnalysysJavasdk/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<groupId>cn.com.analysys</groupId>
66
<name>javasdk</name>
77
<artifactId>javasdk</artifactId>
8-
<version>4.1.1</version>
8+
<version>4.1.2</version>
99
<packaging>jar</packaging>
1010
<description>Analysys Java SDK</description>
1111
<url>https://ark.analysys.cn</url>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package cn.com.analysys.javasdk;
2+
3+
public class AnalysysContext {
4+
5+
public static void setLogger(AnalysysJavaSdkLog logger){
6+
AnalysysLogger.setLogger(logger);
7+
}
8+
9+
}

AnalysysJavasdk/src/main/java/cn/com/analysys/javasdk/AnalysysEncoder.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public static byte[] compress(String str, String encoding) {
4949
gzip.write(str.getBytes(encoding));
5050
gzip.close();
5151
} catch (IOException e) {
52-
System.out.println(e);
52+
AnalysysLogger.print(String.format("AnalysysEncoder.compress error: %s", e.getMessage()));
5353
}
5454
return out.toByteArray();
5555
}
@@ -84,7 +84,7 @@ public static String ozBase64ToStr(String base64str){
8484
base64Codep = new String(AnalysysEncoder.decode(base64str));
8585
return base64Codep;
8686
} catch (UnsupportedEncodingException e) {
87-
System.out.println(e);
87+
AnalysysLogger.print(String.format("AnalysysEncoder.ozBase64ToStr error: %s", e.getMessage()));
8888
return null;
8989
}
9090
}

AnalysysJavasdk/src/main/java/cn/com/analysys/javasdk/AnalysysJavaSdk.java

Lines changed: 51 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
* Analysys Java SDK
1212
*/
1313
public class AnalysysJavaSdk {
14-
private final String SDK_VERSION = "4.1.1";
14+
private final String SDK_VERSION = "4.1.2";
1515
private final Collecter collecter;
1616
private final String appId;
1717
private final Map<String, Object> egBaseProperties;
@@ -49,8 +49,9 @@ public AnalysysJavaSdk(Collecter collecter, String appId, Boolean autoDelParam){
4949
* Debug模式
5050
* @param debug Debug级别
5151
*/
52-
public void setDebugMode(DEBUG debug) {
52+
public boolean setDebugMode(DEBUG debug) {
5353
this.debugMode = debug.getCode();
54+
return true;
5455
}
5556

5657
private boolean isDebug(){
@@ -70,7 +71,7 @@ public void initBaseProperties() {
7071
* 注册超级属性,注册后每次发送的消息体中都包含该属性值
7172
* @param params 属性
7273
*/
73-
public void registerSuperProperties(Map<String, Object> params) throws AnalysysException {
74+
public boolean registerSuperProperties(Map<String, Object> params) throws AnalysysException {
7475
int num = 100;
7576
if(params.entrySet().size() > num){
7677
throw new AnalysysException("Too many super properties. max number is 100.");
@@ -80,21 +81,23 @@ public void registerSuperProperties(Map<String, Object> params) throws AnalysysE
8081
this.xcontextSuperProperties.put(key, params.get(key));
8182
}
8283
if(isDebug()){
83-
System.out.println("registerSuperProperties success");
84+
AnalysysLogger.print("registerSuperProperties success");
8485
}
86+
return true;
8587
}
8688

8789
/**
8890
* 移除超级属性
8991
* @param key 属性key
9092
*/
91-
public void unRegisterSuperProperty(String key) {
93+
public boolean unRegisterSuperProperty(String key) {
9294
if(this.xcontextSuperProperties.containsKey(key)){
9395
this.xcontextSuperProperties.remove(key);
9496
}
9597
if(isDebug()){
96-
System.out.println(String.format("unRegisterSuperProperty %s success", key));
98+
AnalysysLogger.print(String.format("unRegisterSuperProperty %s success", key));
9799
}
100+
return true;
98101
}
99102

100103
/**
@@ -120,18 +123,20 @@ public Map<String, Object> getSuperProperties(){
120123
/**
121124
* 清除超级属性
122125
*/
123-
public void clearSuperProperties(){
126+
public boolean clearSuperProperties(){
124127
this.xcontextSuperProperties.clear();
125128
if(isDebug()){
126-
System.out.println("clearSuperProperties success");
129+
AnalysysLogger.print("clearSuperProperties success");
127130
}
131+
return true;
128132
}
129133

130134
/**
131135
* 立即发送所有收集的信息到服务器
132136
*/
133-
public void flush() {
137+
public boolean flush() {
134138
this.collecter.flush();
139+
return true;
135140
}
136141

137142
public void shutdown() {
@@ -146,11 +151,11 @@ public void shutdown() {
146151
* @param platform 平台类型
147152
* @throws AnalysysException 自定义异常
148153
*/
149-
public void profileSet(String distinctId, boolean isLogin, Map<String, Object> properties, String platform) throws AnalysysException {
150-
upload(distinctId, isLogin, EventName.P_SET.getValue(), properties, platform, null);
154+
public boolean profileSet(String distinctId, boolean isLogin, Map<String, Object> properties, String platform) throws AnalysysException {
155+
return upload(distinctId, isLogin, EventName.P_SET.getValue(), properties, platform, null);
151156
}
152-
public void profileSet(String distinctId, boolean isLogin, Map<String, Object> properties, String platform, String xwhen) throws AnalysysException {
153-
upload(distinctId, isLogin, EventName.P_SET.getValue(), properties, platform, xwhen);
157+
public boolean profileSet(String distinctId, boolean isLogin, Map<String, Object> properties, String platform, String xwhen) throws AnalysysException {
158+
return upload(distinctId, isLogin, EventName.P_SET.getValue(), properties, platform, xwhen);
154159
}
155160

156161
/**
@@ -161,11 +166,11 @@ public void profileSet(String distinctId, boolean isLogin, Map<String, Object> p
161166
* @param platform 平台类型
162167
* @throws AnalysysException 自定义异常
163168
*/
164-
public void profileSetOnce(String distinctId, boolean isLogin, Map<String, Object> properties, String platform) throws AnalysysException {
165-
upload(distinctId, isLogin, EventName.P_SET_ONE.getValue(), properties, platform, null);
169+
public boolean profileSetOnce(String distinctId, boolean isLogin, Map<String, Object> properties, String platform) throws AnalysysException {
170+
return upload(distinctId, isLogin, EventName.P_SET_ONE.getValue(), properties, platform, null);
166171
}
167-
public void profileSetOnce(String distinctId, boolean isLogin, Map<String, Object> properties, String platform, String xwhen) throws AnalysysException {
168-
upload(distinctId, isLogin, EventName.P_SET_ONE.getValue(), properties, platform, xwhen);
172+
public boolean profileSetOnce(String distinctId, boolean isLogin, Map<String, Object> properties, String platform, String xwhen) throws AnalysysException {
173+
return upload(distinctId, isLogin, EventName.P_SET_ONE.getValue(), properties, platform, xwhen);
169174
}
170175

171176
/**
@@ -176,11 +181,11 @@ public void profileSetOnce(String distinctId, boolean isLogin, Map<String, Objec
176181
* @param platform 平台类型
177182
* @throws AnalysysException 自定义异常
178183
*/
179-
public void profileIncrement(String distinctId, boolean isLogin, Map<String, Object> properties, String platform) throws AnalysysException {
180-
upload(distinctId, isLogin, EventName.P_IN.getValue(), properties, platform, null);
184+
public boolean profileIncrement(String distinctId, boolean isLogin, Map<String, Object> properties, String platform) throws AnalysysException {
185+
return upload(distinctId, isLogin, EventName.P_IN.getValue(), properties, platform, null);
181186
}
182-
public void profileIncrement(String distinctId, boolean isLogin, Map<String, Object> properties, String platform, String xwhen) throws AnalysysException {
183-
upload(distinctId, isLogin, EventName.P_IN.getValue(), properties, platform, xwhen);
187+
public boolean profileIncrement(String distinctId, boolean isLogin, Map<String, Object> properties, String platform, String xwhen) throws AnalysysException {
188+
return upload(distinctId, isLogin, EventName.P_IN.getValue(), properties, platform, xwhen);
184189
}
185190

186191
/**
@@ -191,11 +196,11 @@ public void profileIncrement(String distinctId, boolean isLogin, Map<String, Obj
191196
* @param platform 平台类型
192197
* @throws AnalysysException 自定义异常
193198
*/
194-
public void profileAppend(String distinctId, boolean isLogin, Map<String, Object> properties, String platform) throws AnalysysException {
195-
upload(distinctId, isLogin, EventName.P_APP.getValue(), properties, platform, null);
199+
public boolean profileAppend(String distinctId, boolean isLogin, Map<String, Object> properties, String platform) throws AnalysysException {
200+
return upload(distinctId, isLogin, EventName.P_APP.getValue(), properties, platform, null);
196201
}
197-
public void profileAppend(String distinctId, boolean isLogin, Map<String, Object> properties, String platform, String xwhen) throws AnalysysException {
198-
upload(distinctId, isLogin, EventName.P_APP.getValue(), properties, platform, xwhen);
202+
public boolean profileAppend(String distinctId, boolean isLogin, Map<String, Object> properties, String platform, String xwhen) throws AnalysysException {
203+
return upload(distinctId, isLogin, EventName.P_APP.getValue(), properties, platform, xwhen);
199204
}
200205

201206
/**
@@ -206,15 +211,15 @@ public void profileAppend(String distinctId, boolean isLogin, Map<String, Object
206211
* @param platform 平台类型
207212
* @throws AnalysysException 自定义异常
208213
*/
209-
public void profileUnSet(String distinctId, boolean isLogin, String property, String platform) throws AnalysysException {
214+
public boolean profileUnSet(String distinctId, boolean isLogin, String property, String platform) throws AnalysysException {
210215
Map<String, Object> properties = new HashMap<String, Object>(2);
211216
properties.put(property, "");
212-
upload(distinctId, isLogin, EventName.P_UN.getValue(), properties, platform, null);
217+
return upload(distinctId, isLogin, EventName.P_UN.getValue(), properties, platform, null);
213218
}
214-
public void profileUnSet(String distinctId, boolean isLogin, String property, String platform, String xwhen) throws AnalysysException {
219+
public boolean profileUnSet(String distinctId, boolean isLogin, String property, String platform, String xwhen) throws AnalysysException {
215220
Map<String, Object> properties = new HashMap<String, Object>(2);
216221
properties.put(property, "");
217-
upload(distinctId, isLogin, EventName.P_UN.getValue(), properties, platform, xwhen);
222+
return upload(distinctId, isLogin, EventName.P_UN.getValue(), properties, platform, xwhen);
218223
}
219224

220225
/**
@@ -224,11 +229,11 @@ public void profileUnSet(String distinctId, boolean isLogin, String property, St
224229
* @param platform 平台类型
225230
* @throws AnalysysException 自定义异常
226231
*/
227-
public void profileDelete(String distinctId, boolean isLogin, String platform) throws AnalysysException {
228-
upload(distinctId, isLogin, EventName.P_DEL.getValue(), new HashMap<String, Object>(1), platform, null);
232+
public boolean profileDelete(String distinctId, boolean isLogin, String platform) throws AnalysysException {
233+
return upload(distinctId, isLogin, EventName.P_DEL.getValue(), new HashMap<String, Object>(1), platform, null);
229234
}
230-
public void profileDelete(String distinctId, boolean isLogin, String platform, String xwhen) throws AnalysysException {
231-
upload(distinctId, isLogin, EventName.P_DEL.getValue(), new HashMap<String, Object>(1), platform, xwhen);
235+
public boolean profileDelete(String distinctId, boolean isLogin, String platform, String xwhen) throws AnalysysException {
236+
return upload(distinctId, isLogin, EventName.P_DEL.getValue(), new HashMap<String, Object>(1), platform, xwhen);
232237
}
233238

234239
/**
@@ -238,15 +243,15 @@ public void profileDelete(String distinctId, boolean isLogin, String platform,
238243
* @param platform 平台类型
239244
* @throws AnalysysException 自定义异常
240245
*/
241-
public void alias(String aliasId, String distinctId, String platform) throws AnalysysException{
246+
public boolean alias(String aliasId, String distinctId, String platform) throws AnalysysException{
242247
Map<String, Object> param = new HashMap<String, Object>(2);
243248
param.put("$original_id", distinctId);
244-
upload(aliasId, true, EventName.ALIAS.getValue(), param, platform, null);
249+
return upload(aliasId, true, EventName.ALIAS.getValue(), param, platform, null);
245250
}
246-
public void alias(String aliasId, String distinctId, String platform, String xwhen) throws AnalysysException{
251+
public boolean alias(String aliasId, String distinctId, String platform, String xwhen) throws AnalysysException{
247252
Map<String, Object> param = new HashMap<String, Object>(2);
248253
param.put("$original_id", distinctId);
249-
upload(aliasId, true, EventName.ALIAS.getValue(), param, platform, xwhen);
254+
return upload(aliasId, true, EventName.ALIAS.getValue(), param, platform, xwhen);
250255
}
251256

252257
/**
@@ -258,11 +263,11 @@ public void alias(String aliasId, String distinctId, String platform, String xwh
258263
* @param platform 平台类型
259264
* @throws AnalysysException 自定义异常
260265
*/
261-
public void track(String distinctId, boolean isLogin, String eventName, Map<String, Object> properties, String platform) throws AnalysysException {
262-
upload(distinctId, isLogin, eventName, properties, platform, null);
266+
public boolean track(String distinctId, boolean isLogin, String eventName, Map<String, Object> properties, String platform) throws AnalysysException {
267+
return upload(distinctId, isLogin, eventName, properties, platform, null);
263268
}
264-
public void track(String distinctId, boolean isLogin, String eventName, Map<String, Object> properties, String platform, String xwhen) throws AnalysysException {
265-
upload(distinctId, isLogin, eventName, properties, platform, xwhen);
269+
public boolean track(String distinctId, boolean isLogin, String eventName, Map<String, Object> properties, String platform, String xwhen) throws AnalysysException {
270+
return upload(distinctId, isLogin, eventName, properties, platform, xwhen);
266271
}
267272

268273
/**
@@ -275,7 +280,7 @@ public void track(String distinctId, boolean isLogin, String eventName, Map<Stri
275280
* @param xwhen 时间戳
276281
* @throws AnalysysException 自定义异常
277282
*/
278-
private void upload(String distinctId, boolean isLogin, String eventName, Map<String, Object> properties, String platform, String xwhen) throws AnalysysException {
283+
private boolean upload(String distinctId, boolean isLogin, String eventName, Map<String, Object> properties, String platform, String xwhen) throws AnalysysException {
279284
HashMap<String, Object> targetProperties = new HashMap<String, Object>();
280285
if(properties != null){
281286
targetProperties.putAll(properties);
@@ -322,22 +327,23 @@ private void upload(String distinctId, boolean isLogin, String eventName, Map<St
322327
this.collecter.debug(isDebug());
323328
boolean ret = this.collecter.send(eventMap);
324329
if(eventName != null && eventName.startsWith(profile) && isDebug() && ret){
325-
System.out.println(String.format("%s success.", eventName));
330+
AnalysysLogger.print(String.format("%s success.", eventName));
326331
}
332+
return ret;
327333
}
328334

329335
private String getPlatForm(String platform){
330336
if(PlatForm.JS.getValue().equalsIgnoreCase(platform)) {return PlatForm.JS.getValue();}
331337
if(PlatForm.WeChat.getValue().equalsIgnoreCase(platform)) {return PlatForm.WeChat.getValue();}
332338
if(PlatForm.Android.getValue().equalsIgnoreCase(platform)) {return PlatForm.Android.getValue();}
333339
if(PlatForm.iOS.getValue().equalsIgnoreCase(platform)) {return PlatForm.iOS.getValue();}
334-
System.out.println(String.format("Warning: param platform:%s Your input are not:iOS/Android/JS/WeChat.", platform == null ? "" : platform));
340+
AnalysysLogger.print(String.format("Warning: param platform:%s Your input are not:iOS/Android/JS/WeChat.", platform == null ? "" : platform));
335341
if(PlatForm.Java.getValue().equalsIgnoreCase(platform)) {return PlatForm.Java.getValue();}
336342
if(PlatForm.python.getValue().equalsIgnoreCase(platform)) {return PlatForm.python.getValue();}
337343
if(PlatForm.Node.getValue().equalsIgnoreCase(platform)) {return PlatForm.Node.getValue();}
338344
if(PlatForm.PHP.getValue().equalsIgnoreCase(platform)) {return PlatForm.PHP.getValue();}
339345
if(platform == null || platform.trim().length() == 0) {
340-
System.out.println(String.format("Warning: param platform is empty, will use default value: %s.", PlatForm.Java.getValue()));
346+
AnalysysLogger.print(String.format("Warning: param platform is empty, will use default value: %s.", PlatForm.Java.getValue()));
341347
return PlatForm.Java.getValue();
342348
}
343349
return platform;
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package cn.com.analysys.javasdk;
2+
3+
public interface AnalysysJavaSdkLog {
4+
5+
/**
6+
* 日志打印
7+
* @param msg
8+
*/
9+
void print(String msg);
10+
11+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package cn.com.analysys.javasdk;
2+
3+
public class AnalysysLogger {
4+
private static AnalysysJavaSdkLog logger;
5+
6+
public static void print(String msg){
7+
if(logger != null){
8+
logger.print(String.format("[AnalysysJavaSdk]: %s", msg));
9+
} else {
10+
System.out.println(String.format("[AnalysysJavaSdk]: %s", msg));
11+
}
12+
}
13+
14+
public static void setLogger(AnalysysJavaSdkLog logger) {
15+
AnalysysLogger.logger = logger;
16+
}
17+
18+
}

AnalysysJavasdk/src/main/java/cn/com/analysys/javasdk/BatchCollecter.java

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
* @author admin
1717
*/
1818
public class BatchCollecter implements Collecter {
19+
private final MessageSender sender;
1920
private long sendTimer = -1;
2021
private boolean isListen = true;
2122
private final String serverUrl;
@@ -85,6 +86,7 @@ public BatchCollecter(String serverUrl, int batchNum, long batchSec, boolean int
8586
this.batchSec = batchSec * 1000;
8687
this.batchMsgList = new ArrayList<Map<String, Object>>(this.batchNum);
8788
this.singleThread = new ThreadPoolExecutor(1, 1, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>());
89+
this.sender = new MessageSender(this.serverUrl);
8890
init();
8991
}
9092

@@ -93,7 +95,7 @@ private void init(){
9395
@Override
9496
public void run() {
9597
while(isListen){
96-
try { Thread.sleep(1000); } catch (Exception e1) {System.out.println(e1);}
98+
try { Thread.sleep(1000); } catch (Exception e1) {AnalysysLogger.print(e1.getMessage());}
9799
if (sendTimer != -1 && (System.currentTimeMillis() - sendTimer >= batchSec)) {
98100
try {
99101
upload();
@@ -128,35 +130,36 @@ public void upload() {
128130
jsonData = ValidHandle.getEgJsonMapper().writeValueAsString(batchMsgList);
129131
Map<String, String> headParam = new HashMap<String, String>(1);
130132
if(debug){
131-
System.out.println(String.format("Send message to server: %s \ndata: %s", serverUrl, jsonData));
133+
AnalysysLogger.print(String.format("Send message to server: %s data: %s", serverUrl, jsonData));
132134
}
133-
String retMsg = new MessageSender(serverUrl, headParam, jsonData).send();
135+
String retMsg = this.sender.send(headParam, jsonData);
134136
if(debug){
135-
System.out.println(String.format("Send message success,response: %s", retMsg));
137+
AnalysysLogger.print(String.format("Send message success,response: %s", retMsg));
136138
}
137139
} catch (JsonProcessingException e) {
140+
AnalysysLogger.print("Json Serialization Fail: " + batchMsgList);
138141
if(interrupt){
139142
shutdown();
140143
throw new RuntimeException("Json Serialize Error", e);
141144
} else {
142-
System.out.println("Json Serialize Error" + e);
145+
AnalysysLogger.print("Json Serialize Error" + e);
143146
}
144147
} catch (AnalysysException e) {
145148
if(interrupt){
146149
shutdown();
147150
throw new RuntimeException("Upload Data Error", e);
148151
} else {
149-
System.out.println("Upload Data Error" + e);
152+
AnalysysLogger.print("Upload Data Error" + e);
150153
}
151154
} catch (IOException e) {
152155
if(interrupt){
153156
shutdown();
154157
throw new RuntimeException("Connect Server Error", e);
155158
} else {
156-
System.out.println("Connect Server Error" + e);
159+
AnalysysLogger.print("Connect Server Error" + e);
157160
}
158161
} catch (Exception e) {
159-
System.out.println("Send Data Error" + e);
162+
AnalysysLogger.print("Batch Send Data Error" + e);
160163
} finally {
161164
batchMsgList.clear();
162165
resetTimer();
@@ -182,7 +185,7 @@ private void shutdown(){
182185
this.singleThread.shutdown();
183186
this.singleThread.awaitTermination(5, TimeUnit.SECONDS);
184187
} catch (Exception e) {
185-
System.out.println(e);
188+
AnalysysLogger.print(e.getMessage());
186189
this.singleThread = null;
187190
}
188191
}

0 commit comments

Comments
 (0)