forked from segmentio/analytics-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBlockingRequester.java
More file actions
127 lines (90 loc) · 3.44 KB
/
Copy pathBlockingRequester.java
File metadata and controls
127 lines (90 loc) · 3.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package com.github.segmentio.request;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.github.segmentio.AnalyticsClient;
import com.github.segmentio.Constants;
import com.github.segmentio.models.BasePayload;
import com.github.segmentio.models.Batch;
import com.github.segmentio.models.Callback;
import com.github.segmentio.stats.AnalyticsStatistics;
import com.github.segmentio.utils.GSONUtils;
import com.google.gson.Gson;
public class BlockingRequester implements IRequester {
private static final Logger logger = LoggerFactory
.getLogger(Constants.LOGGER);
private AnalyticsClient client;
private Gson gson;
private HttpClient httpClient;
public BlockingRequester(AnalyticsClient client) {
this.client = client;
final HttpParams httpParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParams, client.getOptions().getTimeout());
this.httpClient = new DefaultHttpClient(httpParams);
this.gson = GSONUtils.BUILDER.create();
}
public void send(Batch batch) {
AnalyticsStatistics statistics = client.getStatistics();
String json = gson.toJson(batch);
try {
long start = System.currentTimeMillis();
HttpPost post = new HttpPost(client.getOptions().getHost()
+ "/v1/import");
post.addHeader("Content-Type", "application/json; charset=utf-8");
post.setEntity(new ByteArrayEntity(json.getBytes("UTF-8")));
HttpResponse response = httpClient.execute(post);
BufferedReader rd = new BufferedReader(new InputStreamReader(
response.getEntity().getContent()));
StringBuilder responseBuilder = new StringBuilder();
String line;
while ((line = rd.readLine()) != null) {
responseBuilder.append(line);
}
String responseBody = responseBuilder.toString();
int statusCode = response.getStatusLine().getStatusCode();
long duration = System.currentTimeMillis() - start;
statistics.updateRequestTime(duration);
if (statusCode == 200) {
String message = "Successful analytics request. [code = "
+ statusCode + "]. Response = " + responseBody;
logger.debug(message);
report(statistics, batch, true, message);
} else {
String message = "Failed analytics response [code = " + statusCode +
"]. Response = " + responseBody;
logger.error(message);
report(statistics, batch, false, message);
}
} catch (IOException e) {
String message = "Failed analytics response." + e.getMessage();
logger.error(message, e);
report(statistics, batch, false, message);
}
}
private void report(AnalyticsStatistics statistics, Batch batch, boolean success, String message) {
for (BasePayload payload : batch.getBatch()) {
Callback callback = payload.getCallback();
if (success) {
statistics.updateSuccessful(1);
} else {
statistics.updateFailed(1);
}
if (callback != null) {
callback.onResponse(success, message);
}
}
}
public void close() {
httpClient.getConnectionManager().shutdown();
}
}