26
26
* <h3>KDB Data Consumer</h3>
27
27
* <p>Provides the ability to consume real-time streaming data from a KDB process
28
28
* into a {@link KdbTable} for use within a Java application</p>
29
- * (c) 2014 - 2015 Sport Trades Ltd
29
+ * (c) 2014 - 2019 Sport Trades Ltd
30
30
*
31
31
* @author Jas Rajasansir
32
- * @version 1.1 .0
32
+ * @version 1.2 .0
33
33
* @since 23 Apr 2014
34
34
*/
35
35
public class KdbConsumer extends KdbConnection {
@@ -62,6 +62,9 @@ public class KdbConsumer extends KdbConnection {
62
62
private KdbConsumer (KdbProcess server , List <String > tables , KdbDict subscriptionConfiguration , IKdbRawDataConsumer rawDataConsumer , IKdbTableConsumer tableConsumer ) throws KdbTargetProcessUnavailableException {
63
63
super (server );
64
64
65
+ if (tables == null && subscriptionConfiguration == null )
66
+ throw new NullPointerException ("Must provide at least one subscription method - list of tables or dictionary" );
67
+
65
68
if (rawDataConsumer == null && tableConsumer == null )
66
69
throw new NullPointerException ("Must provied either a raw data or KdbTable consuming object, or both to this object" );
67
70
@@ -74,15 +77,16 @@ private KdbConsumer(KdbProcess server, List<String> tables, KdbDict subscription
74
77
log .info ("Connected to kdb process [ Target: " + server .toString () + " ]" );
75
78
}
76
79
80
+
77
81
/**
78
- * Generates a new kdb consumer (which is generally a consumer from a kdb TickerPlant)
82
+ * Generates a new kdb consumer (which is generally a consumer from a kdb TickerPlant). By specifying a list of tables, the subscription API
83
+ * is assumed to be <code>.u.sub[tables; syms]</code> where <code>syms</code> is hardcoded to null symbol
79
84
* @param server The kdb process to connect to
80
85
* @param tables The list of tables that should be subscribed to. <b>NOTE</b>: This cannot be null, pass an empty list
81
86
* @param rawDataConsumer A listener object that will consume every message from the kdb process
82
87
* @param tableConsumer A listener object that will consume only table messages from the kdb process
83
88
* @throws KdbTargetProcessUnavailableException If the consumer cannot connect to the target kdb process
84
- * @throws NullPointerException If either <code>tables</code> or <code>syms</code> is null. Also if both <code>rawDataConsumer</code>
85
- * and <code>tableConsumer</code> null, the constructor must be passed one or the other
89
+ * @throws NullPointerException If <code>tables</code> is null or if both <code>rawDataConsumer</code> and <code>tableConsumer</code> are null
86
90
*/
87
91
protected KdbConsumer (KdbProcess server , List <String > tables , IKdbRawDataConsumer rawDataConsumer , IKdbTableConsumer tableConsumer ) throws KdbTargetProcessUnavailableException {
88
92
this (server , tables , null , rawDataConsumer , tableConsumer );
@@ -91,18 +95,41 @@ protected KdbConsumer(KdbProcess server, List<String> tables, IKdbRawDataConsume
91
95
throw new NullPointerException ("Tables for a consumer cannot be null. Provide an empty list for ALL tables." );
92
96
}
93
97
98
+ /**
99
+ * Generates a new kdb consumer (which is generally a consumer from a kdb TickerPlant). By specifying a dictionary subscription configuration
100
+ * the subscription API is assumed to be <code>.u.sub[subDict]</code>.
101
+ * @param server The kdb process to connect to
102
+ * @param subscriptionConfiguration The dictionary configuration for the subscription
103
+ * @param rawDataConsumer A listener object that will consume every message from the kdb process
104
+ * @param tableConsumer A listener object that will consume only table messages from the kdb process
105
+ * @throws KdbTargetProcessUnavailableException If the consumer cannot connect to the target kdb process
106
+ * @throws NullPointerException If dictionary configuration object is null or empty or if both <code>rawDataConsumer</code> and
107
+ * <code>tableConsumer</code> are null
108
+ */
94
109
protected KdbConsumer (KdbProcess server , KdbDict subscriptionConfiguration , IKdbRawDataConsumer rawDataConsumer , IKdbTableConsumer tableConsumer ) throws KdbTargetProcessUnavailableException {
95
110
this (server , null , subscriptionConfiguration , rawDataConsumer , tableConsumer );
96
111
97
112
if (subscriptionConfiguration == null || subscriptionConfiguration .isEmpty ())
98
113
throw new NullPointerException ("No subscription configuration supplied. Cannot subscribe to process" );
99
114
}
100
115
101
- /** @see #KdbConsumer(KdbProcess, List, IKdbRawDataConsumer, IKdbTableConsumer) */
116
+
117
+ /**
118
+ * Table list-based kdb consumer with only a table ({@link IKdbTableConsumer}) interface specified
119
+ * @see #KdbConsumer(KdbProcess, List, IKdbRawDataConsumer, IKdbTableConsumer)
120
+ */
102
121
public KdbConsumer (KdbProcess server , List <String > tables , IKdbTableConsumer tableConsumer ) throws KdbTargetProcessUnavailableException {
103
122
this (server , tables , null , tableConsumer );
104
123
}
105
124
125
+ /**
126
+ * Dict-based kdb consumer with only a table ({@link IKdbTableConsumer}) interface specified
127
+ * @see #KdbConsumer(KdbProcess, KdbDict, IKdbRawDataConsumer, IKdbTableConsumer)
128
+ */
129
+ public KdbConsumer (KdbProcess server , KdbDict subscriptionConfiguration , IKdbTableConsumer tableConsumer ) throws KdbTargetProcessUnavailableException {
130
+ this (server , subscriptionConfiguration , null , tableConsumer );
131
+ }
132
+
106
133
107
134
/**
108
135
* Once connection to the process has been established (performed during object construction), then this function
@@ -153,31 +180,34 @@ public void reconnect() {
153
180
* @return <code>True</code> if the subscription result from the kdb process is not null, <code>false</code> otherwise
154
181
*/
155
182
private Boolean subscribe () throws UnsupportedOperationException {
156
- Object subscribeObject = null ;
183
+ Object subscribeResult = null ;
157
184
158
185
if (subscriptionTables != null ) {
159
- if (subscriptionTables .isEmpty ())
160
- subscribeObject = "" ;
161
- else
162
- subscribeObject = subscriptionTables .toArray ();
186
+ // Assume subscription API is '.u.sub[tables; syms]' where syms is always a null symbol
187
+ Object tableSub = "" ;
163
188
189
+ if (! subscriptionTables .isEmpty ())
190
+ tableSub = subscriptionTables .toArray ();
191
+
164
192
log .info ("Attempting to subscribe to kdb process [ Process: {} ] [ Standard Table Subscription: {} ]" , getRemoteProcess (), Printers .listToString (subscriptionTables ));
165
- } else if (subscriptionConfiguration != null ) {
166
- subscribeObject = subscriptionConfiguration .convertToDict ();
167
193
194
+ try {
195
+ subscribeResult = getConnection ().k (SUB_FUNCTION , tableSub , "" );
196
+ } catch (KException | IOException e ) {
197
+ log .error ("Subscription to kdb process failed [ Process: {} ]. Error - {}" , getRemoteProcess (), e .getMessage ());
198
+ return false ;
199
+ }
200
+
201
+ } else if (subscriptionConfiguration != null ) {
202
+ // Assume subscription API is '.u.sub[subDict]' where subDict is a dictionary
168
203
log .info ("Attempting to subscribe to kdb process [ Process: {} ] [ Dict Config Subscription: {} ]" , getRemoteProcess (), subscriptionConfiguration );
169
- } else {
170
- log .error ("No subscription configuration or subscription tables specified. Cannot subscribe to process!" );
171
- throw new UnsupportedOperationException ("No subscription configuration or subscription tables" );
172
- }
173
-
174
- Object subscribeResult = null ;
175
-
176
- try {
177
- subscribeResult = getConnection ().k (SUB_FUNCTION , subscribeObject );
178
- } catch (KException | IOException e ) {
179
- log .error ("Subscription to kdb process failed [ Process: {} ]. Error - {}" , getRemoteProcess (), e .getMessage ());
180
- return false ;
204
+
205
+ try {
206
+ subscribeResult = getConnection ().k (SUB_FUNCTION , subscriptionConfiguration .convertToDict ());
207
+ } catch (KException | IOException e ) {
208
+ log .error ("Subscription to kdb process failed [ Process: {} ]. Error - {}" , getRemoteProcess (), e .getMessage ());
209
+ return false ;
210
+ }
181
211
}
182
212
183
213
if (subscribeResult instanceof Dict ) {
@@ -193,7 +223,7 @@ private Boolean subscribe() throws UnsupportedOperationException {
193
223
}
194
224
}
195
225
196
- return subscribeResult != null ;
226
+ return ( subscribeResult instanceof Boolean ) || ( subscribeResult instanceof Dict ) ;
197
227
}
198
228
199
229
/**
0 commit comments