forked from uber/tchannel-java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AsyncRequest.java
146 lines (121 loc) · 5.45 KB
/
AsyncRequest.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
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/*
* Copyright (c) 2015 Uber Technologies, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.uber.tchannel.basic;
import com.uber.tchannel.api.ResponseCode;
import com.uber.tchannel.api.SubChannel;
import com.uber.tchannel.api.TChannel;
import com.uber.tchannel.api.TFuture;
import com.uber.tchannel.api.handlers.RawRequestHandler;
import com.uber.tchannel.api.handlers.TFutureCallback;
import com.uber.tchannel.messages.RawRequest;
import com.uber.tchannel.messages.RawResponse;
import java.net.InetAddress;
import java.util.concurrent.CountDownLatch;
public final class AsyncRequest {
private AsyncRequest() {}
public static void main(String[] args) throws Exception {
TChannel server = createServer();
TChannel client = createClient();
SubChannel subChannel = client.makeSubChannel("server");
final long start = System.currentTimeMillis();
final CountDownLatch done = new CountDownLatch(3);
TFutureCallback<RawResponse> callback = new TFutureCallback<RawResponse>() {
@Override
public void onResponse(RawResponse response) {
// when using callback, resource associated with response is released by the the TChannel library
if (!response.isError()) {
System.out.println(String.format("Response received: response code: %s, header: %s, body: %s",
response.getResponseCode(),
response.getHeader(),
response.getBody()));
} else {
System.out.println(String.format("Got error response: %s",
response.toString()));
}
done.countDown();
}
};
// send three requests
for (int i = 0; i < 3; i++) {
RawRequest request = new RawRequest.Builder("server", "pong")
.setHeader("Marco")
.setBody("Ping!")
.build();
TFuture<RawResponse> future = subChannel.send(request,
InetAddress.getByName(null),
8888
);
future.addCallback(callback);
}
done.await();
System.out.println(String.format("%nTime cost: %dms", System.currentTimeMillis() - start));
// close channels asynchronously
server.shutdown(false);
client.shutdown(false);
}
protected static TChannel createServer() throws Exception {
// create TChannel
TChannel tchannel = new TChannel.Builder("server")
.setServerHost(InetAddress.getByName(null))
.setServerPort(8888)
.build();
// create sub channel to register the service and endpoint handler
tchannel.makeSubChannel("server")
.register("pong", new RawRequestHandler() {
private int count = 0;
@Override
public RawResponse handleImpl(RawRequest request) {
System.out.println(String.format("Request received: header: %s, body: %s",
request.getHeader(),
request.getBody()));
count++;
switch (count) {
case 1:
return new RawResponse.Builder(request)
.setTransportHeaders(request.getTransportHeaders())
.setHeader("Polo")
.setBody("Pong!")
.build();
case 2:
return new RawResponse.Builder(request)
.setTransportHeaders(request.getTransportHeaders())
.setResponseCode(ResponseCode.Error)
.setHeader("Polo")
.setBody("I feel bad ...")
.build();
default:
throw new UnsupportedOperationException("I feel very bad!");
}
}
});
tchannel.listen();
return tchannel;
}
protected static TChannel createClient() throws Exception {
// create TChannel
TChannel tchannel = new TChannel.Builder("client")
.build();
// create sub channel to talk to server
tchannel.makeSubChannel("server");
return tchannel;
}
}