Samples :: Dialect

Dialect Buy Side Sample

Description

This is a FIX initiator that connects to the pre-defined host and port. When the session is established, the “MarketDataIncrementalRefresh”(MsgType=‘X’) FIX message is sent to the counterparty. This message contains 2 custom tags defined in the dialect file.

This sample can be run together with the “DialectSellSide” sample. The “DialectSellSide” must be started first.

Usage

  • Run the sample:
    • win: 2-DialectBuySide.bat
    • linux: 2-DialectBuySide.sh
  • Clean everything:
    • win: clean.bat
    • linux: clean.sh

Source Code

import biz.onixs.fix.dictionary.Version;
import biz.onixs.fix.engine.Engine;
import biz.onixs.fix.engine.Session;
import biz.onixs.fix.parser.Group;
import biz.onixs.fix.parser.Message;
import biz.onixs.fix.tag.FIX44.Tag;
import biz.onixs.util.Utils;
import biz.onixs.util.settings.PropertyBasedSettings;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * FIX Initiator - Buy Side. With dialect support.
 */
public class DialectBuySide implements Session.InboundApplicationMessageListener, Session.StateChangeListener {
    private static final Logger LOG = LoggerFactory.getLogger(DialectBuySide.class);

    public void run() {
        final PropertyBasedSettings settings = new PropertyBasedSettings("sample/DialectBuySide.properties");
        // Dialect configuration parameter is set in the properties file.
        LOG.info("Starting the Engine...");
        final Engine engine = Engine.init(settings);

        final Version fixVersion = Version.getById(settings.getString("FIXVersion"));
        final String senderCompID = settings.getString("SenderCompID");
        final String targetCompID = settings.getString("TargetCompID");
        final Session sn = new Session(senderCompID, targetCompID, fixVersion, false);

        sn.setSenderSubID("SenderSubID (50) field")
                .setInboundApplicationMessageListener(this)
                .addStateChangeListener(this);
        //
        Utils.waitForEnter("Press 'Enter' to establish connection." +
                " Make sure that the counterparty is ready, for instance 'DialectSellSide' application is started.");
        //
        sn.logonAsInitiator(settings.getString("CounterpartyHost"), settings.getInteger("CounterpartyPort"));

        Utils.waitForEnter("Press 'Enter' to send the message.");

        // Create and send message with custom tags.
        final Message messageX = Message.create("X", fixVersion);
        final Group group = messageX.setGroup(Tag.NoMDEntries, 1);
        group.set(Tag.MDUpdateAction, 0, 0 /* New */)
                .set(5450, 0, "customValue0").set(5451, 0, "customValue1");
        sn.send(messageX);
        LOG.info("Message sent: {}\n", messageX);

        Utils.waitForEnter("Press 'Enter' to disconnect the session and terminate the application.");

        sn.logout("The session is disconnected by BuySide");
        sn.dispose();
        //
        LOG.info("Engine shutdown...");
        engine.shutdown();
    }

    @Override
    public void onInboundApplicationMessage(final Object sender, final Session.InboundApplicationMessageArgs args) {
        LOG.info("Incoming application-level message:\n{}", args.getMsg());
        // Processing of the application-level incoming message ...
    }

    @Override
    public void onStateChange(final Object sender, final Session.StateChangeArgs args) {
        LOG.info("New session state: {}", args.getNewState());
    }

    public static void main(final String[] args) {
        try {
            LOG.info("DialectBuySide");
            LOG.info("The application is starting...");
            final DialectBuySide buySide = new DialectBuySide();
            buySide.run();
        } catch (final Throwable throwable) {
            LOG.error(throwable.getMessage(), throwable);
        } finally {
            LOG.info("The application is stopped.");
        }
    }
}

Dialect Sell Side Sample

Description

This is a FIX acceptor that waits for incoming connections on the pre-defined port (ListenPort). The acceptor session is configured with dialect file. If the incoming application-level message is not valid then the “Email” (MsgType=‘C’) message is sent back.

This sample can be run together with the “DialectBuySide” sample. The “DialectSellSide” must be started first.

Usage

  • Run the sample:
    • win: 1-DialectSellSide.bat
    • linux: 1-DialectSellSide.sh
  • Clean everything:
    • win: clean.bat
    • linux: clean.sh

Source Code

import biz.onixs.fix.dictionary.Version;
import biz.onixs.fix.engine.Engine;
import biz.onixs.fix.engine.Session;
import biz.onixs.fix.parser.Group;
import biz.onixs.fix.parser.Message;
import biz.onixs.fix.parser.MessageValidationFlags;
import biz.onixs.fix.tag.FIX44.Tag;
import biz.onixs.util.TimestampFormat;
import biz.onixs.util.Utils;
import biz.onixs.util.settings.PropertyBasedSettings;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Processes incoming messages.
 */
public class DialectSellSide implements Session.InboundApplicationMessageListener, Session.StateChangeListener,
        Session.ErrorListener, Session.WarningListener {
    private static final Logger LOG = LoggerFactory.getLogger(DialectSellSide.class);
    private int orderCounter = 0;
    private Version fixVersion = null;

    void run() {
        final PropertyBasedSettings settings = new PropertyBasedSettings("sample/DialectSellSide.properties");
        // Dialect configuration parameter is set in the properties file.
        LOG.info("Starting the Engine...");
        final Engine engine = Engine.init(settings);
        //
        fixVersion = Version.getById(settings.getString("FIXVersion"));
        final String senderCompID = settings.getString("SenderCompID");
        final String targetCompID = settings.getString("TargetCompID");
        final Session sn = new Session(senderCompID, targetCompID, fixVersion, false);
        //
        sn.setInboundApplicationMessageListener(this)
                .addStateChangeListener(this)
                .setErrorListener(this)
                .setWarningListener(this)
                .setSendingTimeFormat(TimestampFormat.YYYYMMDDHHMMSSMsec);
        //
        sn.logonAsAcceptor();
        LOG.info("Awaiting session-initiator...");
        //
        Utils.waitForEnterToTerminateApp();
        //
        sn.logout();
        sn.dispose();
        //
        LOG.info("Engine shutdown...");
        engine.shutdown();
    }

    @Override
    public void onInboundApplicationMessage(final Object sender, final Session.InboundApplicationMessageArgs args) {
        LOG.info("Incoming application-level message:\n{}", args.getMsg());
        try {
            args.getMsg().validate(MessageValidationFlags.Strict);
        } catch (final RuntimeException ex) {
            final Message email = Message.create("C", fixVersion);
            email.set(Tag.EmailType, "0");
            final Group group = email.setGroup(Tag.NoLinesOfText, 1);

            final String errorMsg = "Exception during the processing of incoming message: " + ex;

            group.set(Tag.Text, 0, errorMsg);

            final Session sn = (Session) sender;
            sn.send(email);

            LOG.error(errorMsg);
        }
    }

    @Override
    public void onStateChange(final Object sender, final Session.StateChangeArgs args) {
        LOG.info("New session state: {}", args.getNewState());
    }

    @Override
    public void onError(final Object sender, final Session.ErrorArgs args) {
        LOG.error("Error condition: {}", args);
    }

    @Override
    public void onWarning(final Object sender, final Session.WarningArgs args) {
        LOG.warn("Warning condition: {}", args);
    }

    public static void main(final String[] args) {
        try {
            LOG.info("DialectSellSide");
            LOG.info("The application is starting...");
            final DialectSellSide dialectSellSide = new DialectSellSide();
            dialectSellSide.run();
        } catch (final Throwable throwable) {
            LOG.error(throwable.getMessage(), throwable);
        } finally {
            LOG.info("The application is stopped.");
        }
    }
}