How to send fix messages using quickfix / J
I need a simple example of how to initialize a session and send a fix message I have this initial code:
SessionSettings settings = new SessionSettings( new FileInputStream("fix.cfg"));
Application application = new Application(settings);
MessageStoreFactory messageStoreFactory = new FileStoreFactory(settings);
LogFactory logFactory = new ScreenLogFactory( true,true,true);
MessageFactory messageFactory = new DefaultMessageFactory();
Initiator initiator = new SocketInitiator(application,messageStoreFactory,settings,logFactory,messageFactory);
initiator.start();
Solution
From the above code, I see that you have a launcher application (client) and you also need to create an acceptor application (server) Bellow, I have attached two courses that you can do what you want
First, I'll list the receiver applications:
public class ServerApplication implements Application {
@Override
public void onCreate(SessionID sessionID) {
}
@Override
public void onlogon(SessionID sessionID) {
}
@Override
public void onlogout(SessionID sessionID) {
}
@Override
public void toAdmin(Message message,SessionID sessionID) {
}
@Override
public void fromAdmin(Message message,SessionID sessionID) throws FieldNotFound,IncorrectDataFormat,IncorrectTagValue,Rejectlogon {
}
@Override
public void toApp(Message message,SessionID sessionID) throws DoNotSend {
}
@Override
public void fromApp(Message message,UnsupportedMessageType {
System.out.println("FromApp: " + message);
}
public static void main(String[] args) throws ConfigError,FileNotFoundException,InterruptedException,SessionNotFound {
SessionSettings settings = new SessionSettings("res/acceptor.config");
Application application = new ServerApplication();
MessageStoreFactory messageStoreFactory = new FileStoreFactory(settings);
LogFactory logFactory = new ScreenLogFactory( true,true);
MessageFactory messageFactory = new DefaultMessageFactory();
Acceptor initiator = new SocketAcceptor(application,messageFactory);
initiator.start();
CountDownLatch latch = new CountDownLatch(1);
latch.await();
}
}
This is a server application that will remain started and listen for messages from clients connected to it This is the configuration file it uses (acceptor. Properties):
[default] ApplicationID=server FileStorePath=storage/messages/ ConnectionType=acceptor StartTime=00:01:00 Europe/Bucharest EndTime=23:59:00 Europe/Bucharest HeartBtInt=30 UseDataDictionary=Y DataDictionary=FIX42.xml ValidateUserDefinedFields=N ValidateIncomingMessage=N RefreshOnlogon=Y [session] BeginString=FIX.4.2 SocketAcceptPort=9877 SenderCompID=server TargetCompID=client AcceptorTemplate=N lockquote
Next is the client application code It tries to connect to the server, and then it sends it a message:
public class ClientApplication implements Application {
private static volatile SessionID sessionID;
@Override
public void onCreate(SessionID sessionID) {
System.out.println("OnCreate");
}
@Override
public void onlogon(SessionID sessionID) {
System.out.println("Onlogon");
ClientApplication.sessionID = sessionID;
}
@Override
public void onlogout(SessionID sessionID) {
System.out.println("Onlogout");
ClientApplication.sessionID = null;
}
@Override
public void toAdmin(Message message,SessionID sessionID) {
System.out.println("ToAdmin");
}
@Override
public void fromAdmin(Message message,Rejectlogon {
System.out.println("FromAdmin");
}
@Override
public void toApp(Message message,SessionID sessionID) throws DoNotSend {
System.out.println("ToApp: " + message);
}
@Override
public void fromApp(Message message,UnsupportedMessageType {
System.out.println("FromApp");
}
public static void main(String[] args) throws ConfigError,SessionNotFound {
SessionSettings settings = new SessionSettings("res/initiator.config");
Application application = new ClientApplication();
MessageStoreFactory messageStoreFactory = new FileStoreFactory(settings);
LogFactory logFactory = new ScreenLogFactory( true,true);
MessageFactory messageFactory = new DefaultMessageFactory();
Initiator initiator = new SocketInitiator(application,messageFactory);
initiator.start();
while (sessionID == null) {
Thread.sleep(1000);
}
final String orderId = "342";
NewOrderSingle newOrder = new NewOrderSingle(new ClOrdID(orderId),new HandlInst('1'),new Symbol("6758.T"),new Side(Side.BUY),new TransactTime(new Date()),new OrdType(OrdType.MARKET));
Session.sendToTarget(newOrder,sessionID);
Thread.sleep(5000);
}
}
Its configuration file (initiator. Config) is almost the same as that used for acceptor:
[default] ApplicationID=client FileStorePath=storage/messages/ ConnectionType=initiator StartTime=00:01:00 Europe/Bucharest EndTime=23:59:00 Europe/Bucharest HeartBtInt=30 UseDataDictionary=Y DataDictionary=FIX42.xml ValidateUserDefinedFields=N ValidateIncomingMessage=N RefreshOnlogon=Y [session] BeginString=FIX.4.2 SocketConnectHost=localhost SocketConnectPort=9877 SenderCompID=client TargetCompID=server
Some options are missing from the configuration files, but they are sufficient for testing purposes Each class has a main method that is only used to test the case you want Usually you start or stop them a little differently The server application listens for messages / connections and never stops, while the client application stops immediately after sending the first message
