Skip to content

Latest commit

 

History

History
 
 

tchannel-example

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 

TChannel Examples

In TChannel-JAVA, the SubChannel.send(...) function returns a future object, which can either be used to access the response synchronously or asynchronously. They are the basic approaches of TChannel protocol communication in JAVA. The synchronous and asynchronous examples are below.

Run Examples

1. Requet Examples

Command:

mvn package
java -cp tchannel-example/target/tchannel-example.jar com.uber.tchannel.basic.SyncRequest

Output:

Request received: header: Marco, body: Ping!
Response received: response code: OK, header: Polo, body: Pong!

Request received: header: Marco, body: Ping!
Response received: response code: Error, header: Polo, body: I feel bad ...

Request received: header: Marco, body: Ping!
Got error response: <ErrorResponse id=3 errorType=BadRequest message=Failed to handle the request: I feel very bad!>

Time cost: 157ms

Command:

mvn package
java -cp tchannel-example/target/tchannel-example.jar com.uber.tchannel.basic.AsyncRequest

Output:

Request received: header: Marco, body: Ping!
Request received: header: Marco, body: Ping!
Request received: header: Marco, body: Ping!
Response received: response code: OK, header: Polo, body: Pong!
Response received: response code: Error, header: Polo, body: I feel bad ...
Got error response: <ErrorResponse id=3 errorType=BadRequest message=Failed to handle the request: I feel very bad!>

Time cost: 110ms

2. Ping Example

Command:

mvn package
java -cp tchannel-example/target/tchannel-example.jar com.uber.tchannel.ping.PingServer --port 8888

Command:

mvn package
java -cp tchannel-example/target/tchannel-example.jar com.uber.tchannel.ping.PingClient --port 8888

Output:

<JsonResponse responseCode=OK transportHeaders={as=json, re=c, cn=ping-client} headers=null body=null>
	count:10000
Stopping Client...

3. JSON Example

Command:

mvn package
java -cp tchannel-example/target/tchannel-example.jar com.uber.tchannel.json.JsonServer

Output after receiving a client request:

<JsonRequest id=1 service=json-server transportHeaders={as=json, re=c, cn=json-server} arg1=json-endpoint arg2={} arg3={"requestId":0,"requestMessage":"hello?"}>

Command:

mvn package
java -cp tchannel-example/target/tchannel-example.jar com.uber.tchannel.json.JsonClient

Output:

<JsonResponse responseCode=OK transportHeaders={as=json} headers=null body=null>

4. Thrift Example

Command:

mvn package
java -cp tchannel-example/target/tchannel-example.jar com.uber.tchannel.thrift.KeyValueServer

Command:

mvn package
java -cp tchannel-example/target/tchannel-example.jar com.uber.tchannel.thrift.KeyValueClient

Output:

setValue succeeded
getValue succeeded
{'foo' => 'bar'}
getValue succeeded
Key 'baz' not found.
Disconnected from KeyValue Server.

Command:

mvn package
node server.js --port 21300 2>&1 | jq .
java -cp tchannel-example/target/tchannel-example.jar com.uber.tchannel.hyperbahn.HyperbahnExample
tcurl -p 127.0.0.1:21300 javaServer ping -j -2 "{}" -3 '{"request":"hello"}' | jq .

Output of HyperbahnExample:

Got response. All set: <AdvertiseResponse connectionCount=1>

Output of tcurl:

{
  "ok": true,
  "head": {},
  "body": {
    "response": "pong!"
  },
  "headers": {
    "as": "json",
    "cn": "tcurl",
    "re": "c"
  }
}

MIT Licenced