Samples :: Credential
Credential Buy Side Sample
Description
This is a FIX Initiator that connects to the pre-defined host and port. When the session is established, the “SingleOrder - New”(MsgType=‘D’) FIX message is sent to the counterparty.
This sample can be run together with the “CredentialSellSide” sample. The “CredentialSellSide” must be started first.
Usage
- Run the sample:
- Windows: 2-CredentialBuySide.bat
- Linux: 2-CredentialBuySide.sh
- Clean everything:
- Windows: 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.Message;
import biz.onixs.fix.tag.FIX43;
import biz.onixs.fix.tag.SessionLevelMsgType;
import biz.onixs.fix.tag.Tag;
import biz.onixs.util.GuidGenerator;
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;
/**
* FIX Initiator - Buy Side.
*/
public class CredentialBuySide implements Session.InboundApplicationMessageListener, Session.StateChangeListener {
private static final Logger LOG = LoggerFactory.getLogger(CredentialBuySide.class);
private Version fixVersion = null;
public void run() {
final PropertyBasedSettings settings
= new PropertyBasedSettings("sample/CredentialBuySide.properties");
LOG.info("Starting the Engine...");
final Engine engine = Engine.init(settings);
fixVersion = Version.getByNumber(settings.getString("FIXVersion"));
final String senderCompID = settings.getString("SenderCompID");
final String targetCompID = settings.getString("TargetCompID");
final Session sn = new Session(senderCompID, targetCompID, fixVersion);
sn.setSenderSubID("SenderSubID (50) field")
.setInboundApplicationMessageListener(this)
.addStateChangeListener(this);
// Create custom logon message with username and password specified
final Message logonMsg = createLogonMessage(settings);
//
Utils.waitForEnter("Press 'Enter' to establish connection." +
" Make sure that the counterparty is ready, for instance 'CredentialSellSide' application is started.");
//
// Do logon
sn.logonAsInitiator(settings.getString("CounterpartyHost"), settings.getInteger("CounterpartyPort"), logonMsg);
Utils.waitForEnter("\nPress 'Enter' to send the order.\n");
final Message order = createOrder();
sn.send(order);
LOG.info("The order ({}) was sent", order);
Utils.waitForEnter("\nPress 'Enter' to disconnect the session and terminate the application.\n");
sn.logout("The session is disconnected by CredentialBuySide");
sn.dispose();
//
LOG.info("Engine shutdown...");
engine.shutdown();
}
private Message createOrder() {
final Message order = Message.create(FIX43.MsgType.Order_Single, fixVersion);
final GuidGenerator guidGenerator = new GuidGenerator();
order.set(Tag.ClOrdID, guidGenerator.generate())
.set(Tag.HandlInst, "1")
.set(Tag.Symbol, "IBM")
.set(Tag.Side, "1")
.set(Tag.OrderQty, 1000)
.set(Tag.OrdType, "1");
final long nowMillis = System.currentTimeMillis();
order.setTimestamp(Tag.TransactTime, nowMillis);
return order;
}
/**
* Creates custom logon message with username and password specified in the settings
*
* @param settings application settings
* @return logon message
*/
private Message createLogonMessage(final Settings settings) {
final Message logon = Message.create(SessionLevelMsgType.Logon, fixVersion);
logon.set(Tag.Username, settings.getString("Username"))
.set(Tag.Password, settings.getString("Password"));
return logon;
}
@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("CredentialBuySide");
LOG.info("The application is starting...");
final CredentialBuySide credentialBuySide = new CredentialBuySide();
credentialBuySide.run();
} catch (final Throwable throwable) {
LOG.error(throwable.getMessage(), throwable);
} finally {
LOG.info("The application is stopped.");
}
}
}
Credential Sell Side Sample
Description
This is a FIX Acceptor that waits for incoming connections on the pre-defined port (ListenPort). It demonstrates how to process the username and password from the counterparty.
If the incoming application-level message is “SingleOrder - New” (MsgType=‘D’) then the “ExecutionReport” (MsgType=‘8’) message is sent to the counterparty. Otherwise the “Email” (MsgType=‘C’) message is sent back.
This sample can be run together with the “CredentialBuySide” sample. The “CredentialSellSide” must be started first.
Usage
- Run the sample:
- Windows: 1-CredentialSellSide.bat
- Linux: 1-CredentialSellSide.sh
- Clean everything:
- Windows: 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;
import biz.onixs.fix.tag.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 CredentialSellSide implements Session.InboundApplicationMessageListener, Session.StateChangeListener,
Session.ErrorListener, Session.WarningListener {
private static final Logger LOG = LoggerFactory.getLogger(CredentialSellSide.class);
private int orderCounter = 0;
private Version fixVersion = null;
public void run() {
final PropertyBasedSettings settings
= new PropertyBasedSettings("sample/CredentialSellSide.properties");
LOG.info("Starting the Engine...");
final Engine engine = Engine.init(settings);
//
fixVersion = Version.getByNumber(settings.getString("FIXVersion"));
final String senderCompID = settings.getString("SenderCompID");
final String targetCompID = settings.getString("TargetCompID");
final Session sn = new Session(senderCompID, targetCompID, fixVersion);
//
sn.setInboundApplicationMessageListener(this)
.addStateChangeListener(this)
.setErrorListener(this)
.setWarningListener(this)
.setInboundSessionMessageListener(new CredentialController(settings))
.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);
final Message reply;
if (args.getMsg().checkType('D')) {
// New Order - Single
++orderCounter;
final Message order = args.getMsg();
final Message execReport = Message.create("8", fixVersion);
execReport.set(Tag.OrderID, order.get(Tag.ClOrdID))
.set(Tag.ExecID, "ExecID_" + orderCounter)
.set(Tag.ExecTransType, "0")
.set(Tag.OrdStatus, "0")
.set(Tag.Symbol, order.get(Tag.Symbol))
.set(Tag.Side, order.get(Tag.Side))
.set(Tag.OrderQty, order.get(Tag.OrderQty))
.set(Tag.CumQty, order.get(Tag.OrderQty))
.set(Tag.AvgPx, "100.0");
reply = execReport;
} else {
final Message email = Message.create("C", fixVersion);
email.set(Tag.EmailType, "0");
final Group group = email.setGroup(FIX44.Tag.NoLinesOfText, 1);
group.set(Tag.Text, 0,
"The message with MsgType=" + args.getMsg().get(Tag.MsgType) + " was received");
reply = email;
}
final Session sn = (Session) sender;
sn.send(reply);
LOG.info("Sent to the counterparty:\n{}", reply);
} catch (final RuntimeException ex) {
final Message email = Message.create("C", fixVersion);
email.set(Tag.EmailType, "0");
final Group group = email.setGroup(FIX44.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.info(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("CredentialSellSide");
LOG.info("The application is starting...");
final CredentialSellSide credentialSellSide = new CredentialSellSide();
credentialSellSide.run();
} catch (final Throwable throwable) {
LOG.error(throwable.getMessage(), throwable);
} finally {
LOG.info("The application is stopped.");
}
}
}
The following class is used in the sample:
import biz.onixs.fix.engine.Session;
import biz.onixs.fix.parser.Message;
import biz.onixs.fix.tag.SessionLevelMsgType;
import biz.onixs.fix.tag.Tag;
import biz.onixs.util.settings.Settings;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class CredentialController implements Session.InboundSessionMessageListener {
private static final Logger LOG = LoggerFactory.getLogger(CredentialController.class);
private final String username;
private final String password;
public CredentialController(final Settings settings) {
username = settings.getString("Username");
password = settings.getString("Password");
}
@Override
public void onInboundSessionMessage(final Object sender, final Session.InboundSessionMessageArgs args) {
final Message message = args.getMsg();
if (message.checkType(SessionLevelMsgType.Logon)) {
if ((null != username) && !message.containsSame(Tag.Username, username)) {
LOG.error("Username is incorrect.");
// you can throw exception here to logout the session
throw new RuntimeException("Username is incorrect");
} else {
LOG.info("Username is correct.");
}
if ((null != password) && !message.containsSame(Tag.Password, password)) {
LOG.error("Password is incorrect.");
// you can throw exception here to logout the session
throw new RuntimeException("Password is incorrect");
} else {
LOG.info("Password is correct.");
}
}
}
}
Java FIX Engine