forked from rnewson/couchdb-lucene
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Ning.java
75 lines (61 loc) · 2.26 KB
/
Ning.java
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
package com.github.rnewson.couchdb.lucene;
import org.json.JSONObject;
import com.ning.http.client.AsyncHandler;
import com.ning.http.client.AsyncHttpClient;
import com.ning.http.client.AsyncHttpClientConfig;
import com.ning.http.client.AsyncHttpClientConfig.Builder;
import com.ning.http.client.AsyncHttpProvider;
import com.ning.http.client.HttpResponseBodyPart;
import com.ning.http.client.HttpResponseHeaders;
import com.ning.http.client.HttpResponseStatus;
import com.ning.http.client.Response;
import com.ning.http.client.providers.jdk.JDKAsyncHttpProvider;
public class Ning {
private static class MyHandler implements AsyncHandler<Response> {
private String url;
public MyHandler(final String url) {
this.url = url;
}
public STATE onBodyPartReceived(final HttpResponseBodyPart part) throws Exception {
final String line = new String(part.getBodyPartBytes(), "UTF-8");
if ("\n".equals(line)) {
System.err.println(url + ": heartbeat");
return STATE.CONTINUE;
} else {
final JSONObject json = new JSONObject(line);
if (json.has("last_seq")) {
System.err.println(url + ": response ended at sequence " + json.getLong("last_seq"));
return STATE.ABORT;
}
if (json.has("error")) {
System.err.println(url + ": response encountered error: " + json.getString("reason"));
return STATE.ABORT;
}
System.out.println(url + ": " + json);
return STATE.CONTINUE;
}
}
public Response onCompleted() throws Exception {
System.out.println(url + " complete");
return null;
}
public STATE onHeadersReceived(final HttpResponseHeaders headers) throws Exception {
return STATE.CONTINUE;
}
public STATE onStatusReceived(final HttpResponseStatus status) throws Exception {
System.err.println(url + ": " + status.getStatusCode());
return status.getStatusCode() == 200 ? STATE.CONTINUE : STATE.ABORT;
}
public void onThrowable(final Throwable t) {
System.err.println(url + " threw;");
t.printStackTrace();
}
}
public static void main(final String[] args) throws Exception {
AsyncHttpClient asyncHttpClient = new AsyncHttpClient();
for (int i = 0; i < args.length; i++) {
asyncHttpClient.prepareGet(args[i] + "/_changes?feed=continuous&heartbeat=65000").execute(
new MyHandler(args[i]));
}
}
}