Sample :: External Thread

External Thread

Description

This sample connects to the pre-defined host and port (acts as a FIX Initiator). When the session is established, the SingleOrder - New FIX message is sent to the counterparty. This is done using the ExternalThread threading model.

Usage

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

Source Code


import biz.onixs.fix.dictionary.Version;
import biz.onixs.fix.engine.*;
import biz.onixs.fix.parser.Message;
import biz.onixs.fix.tag.FIX40;
import biz.onixs.fix.tag.Tag;
import biz.onixs.util.Utils;
import biz.onixs.util.settings.PropertyBasedSettings;
import biz.onixs.util.settings.Settings;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.CompletableFuture;

/**
 * Processes incoming messages.
 */
public class ExternalThread implements Runnable {
    private static final Logger LOG = LoggerFactory.getLogger(ExternalThread.class);
    private static final String SETTINGS_RESOURCE = "sample/ExternalThread.properties";
    private Version fixVersion;

    @Override
    public void run() {
        try {
            LOG.info("ExternalThread");
            LOG.info("The application is starting...");
            //
            LOG.info("Loading settings from: {}", SETTINGS_RESOURCE);
            final Settings settings = new PropertyBasedSettings(SETTINGS_RESOURCE);
            fixVersion = Version.getByNumber(settings.getString("FIXVersion"));
            //
            final EngineSettings engineSettings = new EngineSettings();
            engineSettings.init(settings);
            engineSettings.setConnectionMode(ConnectionMode.EXTERNAL_THREAD);
            final SessionReactor stack = new TCPStandardStack();
            //
            LOG.info("Starting the Engine...");
            final Engine engine = Engine.init(engineSettings, stack);
            //
            final Session acceptor = new Session(stack, settings.getString("TargetCompID"),
                    settings.getString("SenderCompID"), fixVersion);
            acceptor.logonAsAcceptor();
            //
            final Session initiator = new Session(settings.getString("SenderCompID"),
                    settings.getString("TargetCompID"), fixVersion);
            final CompletableFuture<Void> logonFuture = initiator.logonAsInitiatorAsync(
                    settings.getString("CounterpartyHost"), settings.getInteger("CounterpartyPort"),
                    settings.getInteger("HeartBtInt"), true, null);
            //
            while (!logonFuture.isDone()) {
                stack.dispatchEvents();
            }
            //
            Utils.waitForEnter("Press 'Enter' to send an order, disconnect the session and terminate the application.");
            //
            initiator.send(createOrder());
            //
            final CompletableFuture<Void> logoutFuture = initiator.logoutAsync("The session is disconnected");
            //
            while (!logoutFuture.isDone()) {
                stack.dispatchEvents();
            }
            //
            initiator.dispose();
            acceptor.dispose();
            //
            LOG.info("Engine shutdown...");
            engine.shutdown();
        } catch (final Exception e) {
            LOG.error(e.getMessage(), e);
        } finally {
            LOG.info("The application is stopped.");
        }
    }

    private Message createOrder() {
        final Message order = Message.create(FIX40.MsgType.Order_Single, fixVersion);
        order.set(Tag.HandlInst, "1")
                .set(Tag.ClOrdID, "Unique identifier for Order")
                .set(Tag.Symbol, "IBM")
                .set(Tag.Side, "1")
                .set(Tag.OrderQty, 1000)
                .set(Tag.OrdType, "1");
        return order;
    }

    public static void main(final String[] args) {
        (new ExternalThread()).run();
    }
}