-
Notifications
You must be signed in to change notification settings - Fork 1
/
OpenfeedClientEventHandlerImpl.java
123 lines (114 loc) · 5.22 KB
/
OpenfeedClientEventHandlerImpl.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
package org.openfeed.client.examples;
import org.openfeed.Service;
import org.openfeed.SubscriptionType;
import org.openfeed.client.api.*;
import org.openfeed.client.api.impl.ConnectionStats;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;
public class OpenfeedClientEventHandlerImpl implements OpenfeedClientEventHandler {
private static final Logger log = LoggerFactory.getLogger(OpenfeedClientEventHandler.class);
private final String clientId;
private final OpenfeedClientConfig config;
private final InstrumentCache instrumentCache;
private final ConnectionStats connectionStats;
public OpenfeedClientEventHandlerImpl(OpenfeedClientConfig config, InstrumentCache instrumentCache, ConnectionStats stats) {
this.clientId = config.getClientId();
this.config = config;
this.instrumentCache = instrumentCache;
this.connectionStats = stats;
}
@Override
public void onEvent(OpenfeedClient client, OpenfeedEvent event) {
log.info("{}: Event: {} - {}", config.getClientId(), event.getType(), event.getMessage());
switch (event.getType()) {
case Connected:
if (config.getStatsDisplaySeconds() > 0) {
client.scheduleAtFixedRate(() -> {
log.info("{}: connected: {} {}", this.clientId, client.isConnected(), this.connectionStats);
}, 5, config.getStatsDisplaySeconds(), TimeUnit.SECONDS);
}
break;
case Login:
executeCommands(client);
break;
case Disconnected:
case Logout:
default:
break;
}
}
private void executeCommands(OpenfeedClient client) {
if (client.isReConnect()) {
// The client will re-subscribe on re-connect.
return;
}
if (config.isInstrumentRequest()) {
if (config.getSymbols() != null) {
client.instrument(config.getSymbols());
}
if (config.getMarketIds() != null) {
client.instrumentMarketId(config.getMarketIds());
}
if (config.getExchanges() != null) {
for (String e : config.getExchanges()) {
client.instrumentExchange(e);
}
}
if (config.getChannelIds() != null) {
for (int chId : config.getChannelIds()) {
client.instrumentChannel(chId);
}
}
} else if (config.isInstrumentCrossReferenceRequest()) {
if (config.getSymbols() != null) {
client.instrumentReference(config.getSymbols());
}
if (config.getMarketIds() != null) {
client.instrumentReferenceMarketId(config.getMarketIds());
}
if (config.getExchanges() != null) {
for (String exchange : config.getExchanges()) {
client.instrumentReferenceExchange(exchange);
}
}
if (config.getChannelIds() != null) {
for (int channelId : config.getChannelIds()) {
client.instrumentReferenceChannel(channelId);
}
}
} else if (config.isExchangeRequest()) {
client.exchangeRequest();
}
else if (config.getSymbols() != null) {
Set<SubscriptionType> subTypes = new HashSet<>();
if (config.getSubscriptionTypes().length > 0) {
subTypes.addAll(List.of(config.getSubscriptionTypes()));
} else {
subTypes.add(SubscriptionType.QUOTE);
}
if (config.getService() == Service.REAL_TIME_SNAPSHOT || config.getService() == Service.DELAYED_SNAPSHOT) {
subTypes.forEach( type -> client.subscribeSnapshot(config.getService(), type, config.getSymbols(), config.getSnapshotIntervalSec()));
} else {
client.subscribe(config.getService(), subTypes.toArray(new SubscriptionType[subTypes.size()]), config.getSymbols());
}
} else if (config.getMarketIds() != null) {
if (config.getSubscriptionTypes().length > 0) {
client.subscribe(config.getService(), config.getSubscriptionTypes(), config.getMarketIds());
} else {
client.subscribe(config.getService(), SubscriptionType.QUOTE, config.getMarketIds());
}
} else if (config.getExchanges() != null && config.getExchanges().length > 0) {
client.subscribeExchange(config.getService(), config.getSubscriptionTypes(), config.getInstrumentTypes(), config.getExchanges(), config.getBulkSubscriptionFilters());
} else if (config.getChannelIds() != null && config.getChannelIds().length > 0) {
client.subscribeChannel(config.getService(), config.getSubscriptionTypes(), config.getInstrumentTypes(), config.getChannelIds(), config.getBulkSubscriptionFilters());
}
// List Subscriptions
if(config.isListSubscriptionsRequest()) {
client.listSubscriptionsRequest();
}
}
}