Samples :: Multisegment

Getting Started

Description

CME Drop Copy Handler sample that connects to a set of pre-defined hosts and ports.

Usage

  • Run the sample:
  • win: Multisegment.bat
  • linux: Multisegment.sh
  • Clean everything:
  • win: clean.bat
  • linux: clean.sh

Directory Contents

File Location Description
conf/sample/Segment1.properties segment 1 configuration file
conf/sample/Segment2.properties segment 2 configuration file
conf/sample/CmeDropCopyFixDialect.xml dialect file
conf/logback.xml log configuration file
docs documentation including this file
libs all dependency libraries
src/main/java source files
clean.bat and clean.sh scripts used to clean log and storage directories which will be created while the client is running
Multisegment.bat and Multisegment.sh scripts used to run the client
OnixS.lic trial license file
pom.xml maven build configuration file

Source Code

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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
import biz.onixs.cme.dropcopy.handler.ErrorEventArgs;
import biz.onixs.cme.dropcopy.handler.Handler;
import biz.onixs.cme.dropcopy.handler.MessageEventArgs;
import biz.onixs.fix.parser.Group;
import biz.onixs.fix.parser.GroupInstance;
import biz.onixs.fix.parser.Message;
import biz.onixs.fix.tag.Tag;
import biz.onixs.util.settings.PropertyBasedSettings;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
  
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
  
public class Multisegment implements Handler.ErrorListener, Handler.WarningListener, Handler.HandlerListener {
    private static final Logger LOG = LoggerFactory.getLogger(Multisegment.class);
    private final ArrayList<Handler> handlers = new ArrayList<>();
    private final ArrayList<ConnectionParameters> connectionParameters = new ArrayList<>();
  
    private static class ConnectionParameters {
  
        ConnectionParameters(String host, int port, String accessKeyID, String secretKey) {
            this.host = host;
            this.port = port;
            this.accessKeyID = accessKeyID;
            this.secretKey = secretKey;
        }
  
        String host;
        int port;
        String accessKeyID;
        String secretKey;
    }
  
  
    public Multisegment() {
        System.out.println("CME Drop Copy Handler Multisegment Sample.");
    }
  
    private void run(String... settingsFiles) throws Exception {
  
        for (String settingsFile : settingsFiles) {
  
            final PropertyBasedSettings settings = new PropertyBasedSettings(settingsFile);
  
            final boolean isUpToDate = settings.getBoolean("UpToDate");
  
            if (!isUpToDate) {
                throw new Exception("Please update the configuration file (" + settingsFile + ") with up-to-date values");
            }
  
            Handler.setLicenseFile(settings.getString("LicenseFile"));
            Handler.setLogDirectory(settings.getString("LogDirectory"));
            Handler.setDialectFile("sample/CmeDropCopyFixDialect.xml");
  
            String senderCompId = settings.getString("SenderCompID");
            String targetCompId = settings.getString("TargetCompID");
            String senderSubId = settings.getString("SenderSubID");
            String targetSubId = settings.getString("TargetSubID");
            String senderLocationId = settings.getString("SenderLocationID");
  
            //Fill the values below with the data for your application:
            final String[] handlerVersion = Handler.getAppVersion().split("\\.");
            Handler.ApplicationSystemVendor = "OnixS";
            Handler.ApplicationSystemName = "OnixS Trading System";
            Handler.ApplicationSystemVersion = "J" + handlerVersion[0] + "." + handlerVersion[1];
  
            Handler handler = new Handler(senderCompId, targetCompId, senderSubId, targetSubId, senderLocationId);
  
            handler.setErrorListener(this);
            handler.setWarningListener(this);
            handler.setHandlerListener(this);
  
            handlers.add(handler);
  
            final String host = settings.getString("Host");
            final int port = settings.getInteger("Port");
  
            final String accessKeyID = settings.getString("AccessKeyID");
            final String secretKey = settings.getString("SecretKey");
  
            connectionParameters.add(new ConnectionParameters(host, port, accessKeyID, secretKey));
        }
  
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        printHelp();
  
        while (true) {
            System.out.print("> ");
            String s = br.readLine();
  
            boolean shouldExit = false;
  
            try {
                switch (s) {
                    case "exit":
                    case "1":
                        shouldExit = true;
                        break;
  
                    case "help":
                    case "2":
                        printHelp();
                        break;
  
                    case "logon":
                    case "3":
                        for (int i = 0; i < handlers.size(); i++) {
                            ConnectionParameters params = connectionParameters.get(i);
                            handlers.get(i).logon(params.host, params.port, params.accessKeyID, params.secretKey);
                        }
                        break;
  
                    case "logout":
                    case "4":
                        for (Handler handler : handlers) {
                            handler.logout();
                        }
                        break;
  
                    case "printsessionstate":
                    case "5":
                        for (Handler handler : handlers) {
                            System.out.printf("Session [%s]: %s\r\n", handler.getSession().getId(),
                                    handler.getSession().getState().toString());
                        }
                        break;
  
                    case "reset":
                    case "6":
                        for (Handler handler : handlers) {
                           handler.reset();
                        }
                        break;
                }
  
            } catch (Exception ex) {
                System.err.println(ex.toString());
            }
  
            if (shouldExit)
                break;
        }
  
        System.out.println("Clean-up resources.");
  
        for (Handler handler : handlers) {
            handler.dispose();
        }
  
        System.out.println("Done.");
    }
  
    private void printHelp() {
        System.out.println("  1. exit               - exit from application");
        System.out.println("  2. help               - print this help");
        System.out.println("  3. logon              - logon sessions");
        System.out.println("  4. logout             - logout sessions");
        System.out.println("  5. printsessionstate  - print sessions states");
        System.out.println("  6. reset              - reset MsgSeqNum");
    }
  
    public static void main(String[] args) {
        try {
            (new Multisegment()).run("config/Segment1.properties", "config/Segment2.properties");
        } catch (Exception e) {
            LOG.error(e.getMessage());
        }
    }
  
    @Override
    public void onExecutionReportReceived(Object sender, MessageEventArgs args) {
        final Message report =  args.getMessage();
        System.out.printf("Execution Report received: %s.\n", report);
        System.out.printf("ClOrdID = %s.\n", report.get(Tag.ClOrdID));
        if(report.contains(Tag.OrdStatus))
            System.out.printf("OrdStatus = %s.\n", report.get(Tag.OrdStatus));
        if(report.contains(Tag.ExecType))
            System.out.printf("ExecType = %s.\n", report.get(Tag.ExecType));
        if(report.contains(Tag.Price))
            System.out.printf("Price = %s.\n", report.get(Tag.Price));
        if(report.contains(Tag.OrderQty))
            System.out.printf("OrderQty = %s.\n", report.get(Tag.OrderQty));
        if(report.contains(Tag.LeavesQty))
            System.out.printf("LeavesQty = %s.\n", report.get(Tag.LeavesQty));
        if(report.contains(Tag.LastPx))
            System.out.printf("LastPx = %s.\n", report.get(Tag.LastPx));
        if(report.contains(Tag.SecurityDesc))
            System.out.printf("SecurityDesc = %s.\n", report.get(Tag.SecurityDesc));
    }
  
    @Override
    public void onMassOrderCancelReportReceived(Object sender, MessageEventArgs args) {
        final Message orderMassActionReport = args.getMessage();
        System.out.printf("Mass Order Cancel Report received: %s.\n", orderMassActionReport);
        if(orderMassActionReport.contains(Tag.MarketSegmentID))
            System.out.printf("MarketSegmentID = %s.\n", orderMassActionReport.get(Tag.MarketSegmentID));
        System.out.printf("MassActionScope = %s.\n", orderMassActionReport.get(Tag.MassActionScope));
        System.out.printf("MassActionResponse = %s.\n", orderMassActionReport.get(Tag.MassActionResponse));
        System.out.printf("TotalAffectedOrders = %s.\n", orderMassActionReport.get(Tag.TotalAffectedOrders));
        if(args.getMessage().hasGroup(Tag.NoAffectedOrders)) {
            System.out.printf("NoAffectedOrders = %s.\n", args.getMessage().get(Tag.NoAffectedOrders));
            final Group noAffectedOrdersGrp = orderMassActionReport.getGroup(Tag.NoAffectedOrders);
            for (final GroupInstance instance: noAffectedOrdersGrp) {
                System.out.printf("OrigClOrdID = %s.\n", instance.get(Tag.OrigClOrdID));
                System.out.printf("CxlQty = %s.\n", instance.get(Tag.CxlQty));
                System.out.printf("AffectedOrderID = %s.\n", instance.get(Tag.AffectedOrderID));
                System.out.println("---------------\n");
            }
        }
        System.out.printf("LastFragment = %s.\n", orderMassActionReport.get(Tag.LastFragment));
    }
  
    @Override
    public void onError(Object sender, ErrorEventArgs args) {
        LOG.error("{}", args);
    }
  
    @Override
    public void onWarning(Object sender, ErrorEventArgs args) {
        LOG.warn("{}", args);
    }
}