in tchannel-example/src/main/java/com/uber/tchannel/ping/PingClient.java [80:119]
public void run() throws Exception {
TChannel tchannel = new TChannel.Builder("ping-client").build();
SubChannel subChannel = tchannel.makeSubChannel("ping-server");
final ConcurrentHashMap<String, AtomicInteger> msgs = new ConcurrentHashMap<>();
final CountDownLatch done = new CountDownLatch(requests);
for (int i = 0; i < requests; i++) {
JsonRequest<Ping> request = new JsonRequest.Builder<Ping>("ping-server", "ping")
.setBody(new Ping("{'key': 'ping?'}"))
.setHeader("some", "header")
.setTimeout(100 + i)
.build();
TFuture<JsonResponse<Pong>> f = subChannel.send(
request,
InetAddress.getByName(host),
port
);
f.addCallback(new TFutureCallback<JsonResponse<Pong>>() {
@Override
public void onResponse(JsonResponse<Pong> pongResponse) {
done.countDown();
String msg = pongResponse.toString();
AtomicInteger count = msgs.putIfAbsent(msg, new AtomicInteger(1));
if (count != null) {
count.incrementAndGet();
}
}
});
}
done.await();
for (Map.Entry<String, AtomicInteger> stringIntegerEntry : msgs.entrySet()) {
System.out.println(String.format("%s%n\tcount:%s",
stringIntegerEntry.getKey(), stringIntegerEntry.getValue()
));
}
tchannel.shutdown(false);
}