Samples :: Getting Started

Getting Started

Description

CME Drop Copy Handler sample that connects to the pre-defined host and port.

Usage

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

Directory Contents

File Location Description
conf/GettingStarted.properties configuration file
conf/CmeDropCopyFixDialect.xml dialect file
conf/logback.xml log configuration file
docs documentation including this file
libs all dependency libraries
src/main/java source files
target/classes already built classes
build.bat and build.sh scripts used to re-build the client
clean.bat and clean.sh scripts used to clean log and storage directories which will be created while the client is running
runSample.bat and runSample.sh scripts used to run the client
OnixS.lic trial license file
pom.xml Maven build configuration file

Source Code

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.scheduler.InitiatorConnection;
import biz.onixs.fix.scheduler.MultiDayLengthSchedule;
import biz.onixs.fix.scheduler.SequenceNumberResetPolicy;
import biz.onixs.fix.scheduler.SessionSchedule;
import biz.onixs.fix.scheduler.SessionScheduler;
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.net.InetSocketAddress;
import java.time.DayOfWeek;
import java.time.LocalTime;

public class GettingStarted implements Handler.ErrorListener, Handler.WarningListener, Handler.HandlerListener {
    private static final Logger LOG = LoggerFactory.getLogger(GettingStarted.class);
    private Handler handler;
    private static final String SETTINGS_RESOURCE = "sample/GettingStarted.properties";

    public GettingStarted() {
        System.out.println("CME Drop Copy Handler Getting Started Sample.");
    }

    private void run() throws Exception {
        final PropertyBasedSettings settings = new PropertyBasedSettings(SETTINGS_RESOURCE);

        final boolean isUpToDate = settings.getBoolean("UpToDate");

        if (!isUpToDate) {
            throw new Exception("Please update the configuration file (" + SETTINGS_RESOURCE + ") with up-to-date values");
        }

        Handler.setLicenseFile(settings.getString("LicenseFile"));
        Handler.setLogDirectory(settings.getString("LogDirectory"));
        Handler.setDialectFile("sample/CmeDropCopyFixDialect.xml");

        //Update defaults if necessary
        LOG.info("Using defaults: ApplicationSystemVendor = {}, ApplicationSystemName = {}, ApplicationSystemVersion = {}",
                Handler.ApplicationSystemVendor, Handler.ApplicationSystemName, Handler.ApplicationSystemVersion);

        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");

        handler = new Handler(senderCompId, targetCompId, senderSubId, targetSubId, senderLocationId);

        handler.setErrorListener(this);
        handler.setWarningListener(this);
        handler.setHandlerListener(this);

        handler.setProcessNextExpectedSeqNumFromCmeLogout(true);

        final String host = settings.getString("Host");
        final int port = settings.getInteger("Port");
        final String backupHost = settings.getString("BackupHost", "");
        final int backupPort = settings.getInteger("BackupPort", 0);

        final String accessKeyID = settings.getString("AccessKeyID", "");
        final String secretKey = settings.getString("SecretKey", "");

        final boolean useScheduler = settings.getBoolean("UseScheduler");
        SessionScheduler sessionScheduler = null;

        if (useScheduler) {
            sessionScheduler = new SessionScheduler();
            sessionScheduler.start();
            final SessionSchedule schedule = createSchedule();
            final InitiatorConnection connection = new InitiatorConnection();
            connection.setInitiatorConnectionListener(handler); // This is required to process switching between primary and backup hosts correctly.
            final InetSocketAddress address = InetSocketAddress.createUnresolved(host, port);
            connection.addAddress(address);
            if (backupHost != null) {
                final InetSocketAddress address2 = InetSocketAddress.createUnresolved(backupHost, backupPort);
                connection.addAddress(address2);
            }

            Message customLogon = handler.createLogonMessage(accessKeyID, secretKey);

            connection.setCustomLogonMessage(customLogon.toString());
            sessionScheduler.register(handler.getSession(), schedule, connection);
        }