Sample :: Extended Thread

Extended 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

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
import biz.onixs.fix.dictionary.Version;
import biz.onixs.fix.engine.ConnectionMode;
import biz.onixs.fix.engine.Engine;
import biz.onixs.fix.engine.EngineSettings;
import biz.onixs.fix.engine.Session;
import biz.onixs.fix.engine.SessionReactor;
import biz.onixs.fix.engine.TCPStandardStack;
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 ExtendedThread implements Runnable {
    private static final Logger LOG = LoggerFactory.getLogger(ExtendedThread.class);
    private static final String SETTINGS_RESOURCE = "sample/ExternalThread.properties";
    private Version fixVersion;
  
    @Override
    public void run() {
        try {
            LOG.info("ExtendedThread");
            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 ExtendedThread()).run();
    }
}