Skip to content

Commit 25f26f8

Browse files
committed
docs: add more concrete Java examples
1 parent 2962760 commit 25f26f8

File tree

2,904 files changed

+297
-608349
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

2,904 files changed

+297
-608349
lines changed

README.md

+6-6
Original file line numberDiff line numberDiff line change
@@ -331,16 +331,16 @@ This is used to match a particular request or subscription with its returned dat
331331

332332
Send a `std::vector<Request>`.
333333
```
334-
Request request_1(Request::Operation::GET_RECENT_TRADES, "coinbase", "BTC-USD");
334+
Request request_1(Request::Operation::GET_RECENT_TRADES, "coinbase", "BTC-USD", "cool correlation id for BTC");
335335
request_1.appendParam(...);
336-
Request request_2(Request::Operation::GET_RECENT_TRADES, "coinbase", "ETH-USD");
336+
Request request_2(Request::Operation::GET_RECENT_TRADES, "coinbase", "ETH-USD", "cool correlation id for ETH");
337337
request_2.appendParam(...);
338338
session.sendRequest({request_1, request_2});
339339
```
340340
Subscribe a `std::vector<Subscription>`.
341341
```
342-
Subscription subscription_1("coinbase", "BTC-USD", "MARKET_DEPTH");
343-
Subscription subscription_2("binance-us", "ethusd", "MARKET_DEPTH");
342+
Subscription subscription_1("coinbase", "BTC-USD", "MARKET_DEPTH", "", "cool correlation id for coinbase BTC-USD");
343+
Subscription subscription_2("binance-us", "ethusd", "MARKET_DEPTH", "", "cool correlation id for binance-us ethusd");
344344
session.subscribe({subscription_1, subscription_2});
345345
```
346346

@@ -660,9 +660,9 @@ This is used to match a particular request or subscription with its returned dat
660660

661661
Send a `std::vector<Request>`.
662662
```
663-
Request request_1(Request::Operation::CREATE_ORDER, "binance-us", "BTCUSD");
663+
Request request_1(Request::Operation::CREATE_ORDER, "binance-us", "BTCUSD", "cool correlation id for BTC");
664664
request_1.appendParam(...);
665-
Request request_2(Request::Operation::CREATE_ORDER, "binance-us", "ETHUSD");
665+
Request request_2(Request::Operation::CREATE_ORDER, "binance-us", "ETHUSD", "cool correlation id for ETH");
666666
request_2.appendParam(...);
667667
session.sendRequest({request_1, request_2});
668668
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import com.cryptochassis.ccapi.Logger;
2+
import com.cryptochassis.ccapi.Session;
3+
import com.cryptochassis.ccapi.SessionConfigs;
4+
import com.cryptochassis.ccapi.SessionOptions;
5+
import com.cryptochassis.ccapi.Subscription;
6+
import java.util.concurrent.locks.ReentrantLock;
7+
8+
public class Main {
9+
static class MyLogger extends Logger {
10+
@Override
11+
public void logMessage(String severity, String threadId, String timeISO, String fileName, String lineNumber, String message) {
12+
this.lock.lock();
13+
try {
14+
System.out.println(String.format("%s: [%s] {%s:%s} %s%s%s", threadId, timeISO, fileName, lineNumber, severity, " ".repeat(8), message));
15+
} finally {
16+
this.lock.unlock();
17+
}
18+
}
19+
ReentrantLock lock = new ReentrantLock();
20+
}
21+
public static void main(String[] args) {
22+
System.loadLibrary("ccapi_binding_java");
23+
var myLogger = new MyLogger();
24+
Logger.setLogger(myLogger);
25+
var option = new SessionOptions();
26+
var config = new SessionConfigs();
27+
var session = new Session(option, config);
28+
var subscription = new Subscription("okx", "BTC-USDT", "MARKET_DEPTH");
29+
session.subscribe(subscription);
30+
try {
31+
Thread.sleep(10000);
32+
} catch (InterruptedException e) {
33+
}
34+
session.stop();
35+
System.out.println("Bye");
36+
}
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
add_compile_definitions(CCAPI_ENABLE_LOG_TRACE)
2+
# usage: when generating the binding code, do cmake -DCMAKE_PROJECT_INCLUDE=<path-to-this-file> ..., see https://github.com/crypto-chassis/ccapi#non-c
3+
# see https://cmake.org/cmake/help/latest/variable/CMAKE_PROJECT_INCLUDE.html
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import com.cryptochassis.ccapi.Event;
2+
import com.cryptochassis.ccapi.EventHandler;
3+
import com.cryptochassis.ccapi.MapStringString;
4+
import com.cryptochassis.ccapi.Message;
5+
import com.cryptochassis.ccapi.Request;
6+
import com.cryptochassis.ccapi.Session;
7+
import com.cryptochassis.ccapi.SessionConfigs;
8+
import com.cryptochassis.ccapi.SessionOptions;
9+
import com.cryptochassis.ccapi.Subscription;
10+
import com.cryptochassis.ccapi.SubscriptionList;
11+
12+
public class Main {
13+
static class MyEventHandler extends EventHandler {
14+
@Override
15+
public boolean processEvent(Event event, Session session) {
16+
if (event.getType() == Event.Type.SUBSCRIPTION_STATUS) {
17+
System.out.println(String.format("Received an event of type SUBSCRIPTION_STATUS:\n%s", event.toStringPretty(2, 2)));
18+
var message = event.getMessageList().get(0);
19+
if (message.getType() == Message.Type.SUBSCRIPTION_STARTED) {
20+
var request = new Request(Request.Operation.CREATE_ORDER, "okx", "BTC-USDT");
21+
var param = new MapStringString();
22+
param.put("SIDE", "BUY");
23+
param.put("QUANTITY", "0.0005");
24+
param.put("LIMIT_PRICE", "20000");
25+
request.appendParam(param);
26+
session.sendRequest(request);
27+
}
28+
} else if (event.getType() == Event.Type.SUBSCRIPTION_DATA) {
29+
System.out.println(String.format("Received an event of type SUBSCRIPTION_DATA:\n%s", event.toStringPretty(2, 2)));
30+
}
31+
return true;
32+
}
33+
}
34+
public static void main(String[] args) {
35+
if (System.getenv("OKX_API_KEY") == null) {
36+
System.err.println("Please set environment variable OKX_API_KEY");
37+
return;
38+
}
39+
if (System.getenv("OKX_API_SECRET") == null) {
40+
System.err.println("Please set environment variable OKX_API_SECRET");
41+
return;
42+
}
43+
if (System.getenv("OKX_API_PASSPHRASE") == null) {
44+
System.err.println("Please set environment variable OKX_API_PASSPHRASE");
45+
return;
46+
}
47+
System.loadLibrary("ccapi_binding_java");
48+
var eventHandler = new MyEventHandler();
49+
var option = new SessionOptions();
50+
var config = new SessionConfigs();
51+
var session = new Session(option, config, eventHandler);
52+
var subscriptionList = new SubscriptionList();
53+
subscriptionList.add(new Subscription("okx", "BTC-USDT", "ORDER_UPDATE"));
54+
session.subscribe(subscriptionList);
55+
try {
56+
Thread.sleep(10000);
57+
} catch (InterruptedException e) {
58+
}
59+
session.stop();
60+
System.out.println("Bye");
61+
}
62+
}
+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import com.cryptochassis.ccapi.Event;
2+
import com.cryptochassis.ccapi.EventHandler;
3+
import com.cryptochassis.ccapi.Message;
4+
import com.cryptochassis.ccapi.PairIntString;
5+
import com.cryptochassis.ccapi.Request;
6+
import com.cryptochassis.ccapi.Session;
7+
import com.cryptochassis.ccapi.SessionConfigs;
8+
import com.cryptochassis.ccapi.SessionOptions;
9+
import com.cryptochassis.ccapi.Subscription;
10+
import com.cryptochassis.ccapi.SubscriptionList;
11+
import com.cryptochassis.ccapi.VectorPairIntString;
12+
13+
public class Main {
14+
static class MyEventHandler extends EventHandler {
15+
@Override
16+
public boolean processEvent(Event event, Session session) {
17+
if (event.getType() == Event.Type.AUTHORIZATION_STATUS) {
18+
System.out.println(String.format("Received an event of type AUTHORIZATION_STATUS:\n%s", event.toStringPretty(2, 2)));
19+
var message = event.getMessageList().get(0);
20+
if (message.getType() == Message.Type.AUTHORIZATION_SUCCESS) {
21+
var request = new Request(Request.Operation.FIX, "coinbase", "", "same correlation id for subscription and request");
22+
var param = new VectorPairIntString();
23+
param.add(new PairIntString(35, "D"));
24+
param.add(new PairIntString(11, "6d4eb0fb-2229-469f-873e-557dd78ac11e"));
25+
param.add(new PairIntString(55, "BTC-USD"));
26+
param.add(new PairIntString(54, "1"));
27+
param.add(new PairIntString(44, "20000"));
28+
param.add(new PairIntString(38, "0.001"));
29+
param.add(new PairIntString(40, "2"));
30+
param.add(new PairIntString(59, "1"));
31+
request.appendParamFix(param);
32+
session.sendRequest(request);
33+
}
34+
} else if (event.getType() == Event.Type.FIX) {
35+
System.out.println(String.format("Received an event of type FIX:\n%s", event.toStringPretty(2, 2)));
36+
}
37+
return true;
38+
}
39+
}
40+
public static void main(String[] args) {
41+
if (System.getenv("COINBASE_API_KEY") == null) {
42+
System.err.println("Please set environment variable COINBASE_API_KEY");
43+
return;
44+
}
45+
if (System.getenv("COINBASE_API_SECRET") == null) {
46+
System.err.println("Please set environment variable COINBASE_API_SECRET");
47+
return;
48+
}
49+
if (System.getenv("COINBASE_API_PASSPHRASE") == null) {
50+
System.err.println("Please set environment variable COINBASE_API_PASSPHRASE");
51+
return;
52+
}
53+
System.loadLibrary("ccapi_binding_java");
54+
var eventHandler = new MyEventHandler();
55+
var option = new SessionOptions();
56+
var config = new SessionConfigs();
57+
var session = new Session(option, config, eventHandler);
58+
var subscription = new Subscription("coinbase", "", "FIX", "", "same correlation id for subscription and request");
59+
session.subscribeByFix(subscription);
60+
try {
61+
Thread.sleep(10000);
62+
} catch (InterruptedException e) {
63+
}
64+
session.stop();
65+
System.out.println("Bye");
66+
}
67+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import com.cryptochassis.ccapi.Event;
2+
import com.cryptochassis.ccapi.EventHandler;
3+
import com.cryptochassis.ccapi.Session;
4+
import com.cryptochassis.ccapi.SessionConfigs;
5+
import com.cryptochassis.ccapi.SessionOptions;
6+
import com.cryptochassis.ccapi.Subscription;
7+
import com.cryptochassis.ccapi.SubscriptionList;
8+
9+
public class Main {
10+
static class MyEventHandler extends EventHandler {
11+
@Override
12+
public boolean processEvent(Event event, Session session) {
13+
try {
14+
throw new Exception("oops");
15+
} catch (Exception e) {
16+
e.printStackTrace();
17+
System.exit(0);
18+
}
19+
return true;
20+
}
21+
}
22+
public static void main(String[] args) {
23+
System.loadLibrary("ccapi_binding_java");
24+
var eventHandler = new MyEventHandler();
25+
var option = new SessionOptions();
26+
var config = new SessionConfigs();
27+
var session = new Session(option, config, eventHandler);
28+
var subscriptionList = new SubscriptionList();
29+
subscriptionList.add(new Subscription("okx", "BTC-USDT", "MARKET_DEPTH"));
30+
session.subscribe(subscriptionList);
31+
try {
32+
Thread.sleep(10000);
33+
} catch (InterruptedException e) {
34+
}
35+
session.stop();
36+
System.out.println("Bye");
37+
}
38+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import com.cryptochassis.ccapi.Event;
2+
import com.cryptochassis.ccapi.EventHandler;
3+
import com.cryptochassis.ccapi.Session;
4+
import com.cryptochassis.ccapi.SessionConfigs;
5+
import com.cryptochassis.ccapi.SessionOptions;
6+
import com.cryptochassis.ccapi.Subscription;
7+
import com.cryptochassis.ccapi.SubscriptionList;
8+
9+
public class Main {
10+
static class MyEventHandler extends EventHandler {
11+
@Override
12+
public boolean processEvent(Event event, Session session) {
13+
if (event.getType() == Event.Type.SUBSCRIPTION_DATA) {
14+
for (var message : event.getMessageList()) {
15+
var correlationId = message.getCorrelationIdList().get(0);
16+
System.out.println(String.format("%s: Best bid and ask at %s are:", correlationId, message.getTimeISO()));
17+
for (var element : message.getElementList()) {
18+
var elementNameValueMap = element.getNameValueMap();
19+
for (var entry : elementNameValueMap.entrySet()) {
20+
var name = entry.getKey();
21+
var value = entry.getValue();
22+
System.out.println(String.format(" %s = %s", name, value));
23+
}
24+
}
25+
}
26+
}
27+
return true;
28+
}
29+
}
30+
public static void main(String[] args) {
31+
System.loadLibrary("ccapi_binding_java");
32+
var eventHandler = new MyEventHandler();
33+
var option = new SessionOptions();
34+
var config = new SessionConfigs();
35+
var session = new Session(option, config, eventHandler);
36+
var subscriptionList = new SubscriptionList();
37+
subscriptionList.add(new Subscription("okx", "BTC-USDT", "MARKET_DEPTH", "", "BTC"));
38+
subscriptionList.add(new Subscription("okx", "ETH-USDT", "MARKET_DEPTH", "", "ETH"));
39+
session.subscribe(subscriptionList);
40+
try {
41+
Thread.sleep(10000);
42+
} catch (InterruptedException e) {
43+
}
44+
session.stop();
45+
System.out.println("Bye");
46+
}
47+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import com.cryptochassis.ccapi.Event;
2+
import com.cryptochassis.ccapi.EventHandler;
3+
import com.cryptochassis.ccapi.MapStringString;
4+
import com.cryptochassis.ccapi.Request;
5+
import com.cryptochassis.ccapi.Session;
6+
import com.cryptochassis.ccapi.SessionConfigs;
7+
import com.cryptochassis.ccapi.SessionOptions;
8+
9+
public class Main {
10+
static class MyEventHandler extends EventHandler {
11+
@Override
12+
public boolean processEvent(Event event, Session session) {
13+
System.out.println(String.format("Received an event:\n%s", event.toStringPretty(2, 2)));
14+
return true;
15+
}
16+
}
17+
public static void main(String[] args) {
18+
System.loadLibrary("ccapi_binding_java");
19+
var eventHandler = new MyEventHandler();
20+
var option = new SessionOptions();
21+
var config = new SessionConfigs();
22+
var session = new Session(option, config, eventHandler);
23+
var request = new Request(Request.Operation.GET_RECENT_TRADES, "okx", "BTC-USDT");
24+
var param = new MapStringString();
25+
param.put("LIMIT", "1");
26+
request.appendParam(param);
27+
session.sendRequest(request);
28+
try {
29+
Thread.sleep(10000);
30+
} catch (InterruptedException e) {
31+
}
32+
session.stop();
33+
System.out.println("Bye");
34+
}
35+
}
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
add_compile_definitions(CCAPI_ENABLE_LOG_TRACE)
2-
# usage: cmake -DCMAKE_PROJECT_INCLUDE=<path-to-this-file> ...
2+
# usage: when generating the binding code, do cmake -DCMAKE_PROJECT_INCLUDE=<path-to-this-file> ..., see https://github.com/crypto-chassis/ccapi#non-c
33
# see https://cmake.org/cmake/help/latest/variable/CMAKE_PROJECT_INCLUDE.html

binding/python/example/handle_exception/main.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ def __init__(self):
88
def processEvent(self, event: Event, session: Session) -> bool:
99
try:
1010
raise Exception('oops')
11-
return True # This line is needed.
1211
except Exception:
1312
print(traceback.format_exc())
1413
sys.exit(1)
14+
return True # This line is needed.
1515
if __name__ == '__main__':
1616
eventHandler = MyEventHandler()
1717
option = SessionOptions()

0 commit comments

Comments
 (0)