Messages can be sent to a counter party with the static Session sendToTarget
methods.
This method has several signatures. They are:
package quickfix; public static boolean sendToTarget(Message message) throws SessionNotFound public static boolean sendToTarget(Message message, SessionID sessionID) throws SessionNotFound public static boolean sendToTarget (Message message, String senderCompID, String targetCompID) throws SessionNotFound
Once again. This should only be used for low level interface to other languages and middleware. do not use this for writing applications.
import quickfix.*; public void sendOrderCancelRequest() { Message message = new Message(); // BeginString message.getHeader().setField(new StringField(8, "FIX.4.2")); // SenderCompID message.getHeader().setField(new StringField(49, "TW")); // TargetCompID, with enumeration message.getHeader().setField(new StringField(56, "TARGET")); // MsgType message.getHeader().setField(new CharField(35, 'F')); // OrigClOrdID message.setField(new StringField(41, "123")); // ClOrdID message.setField(new StringField(11, "321")); // Symbol message.setField(new StringField(55, "LNUX")); // Side, with value enumeration message.setField(new CharField(54, Side.BUY)); // Text message.setField(new StringField(58, "Cancel My Order!")); Session.sendToTarget(message); }
And here, by using field classes, we can clearify our code and add some type safety. Once again, this is something you usually use for code that needs to work with multiple messages types or multiple FIX versions.
import quickfix.*; import quickfix.field.*; void sendOrderCancelRequest() { Message message = new Message(); Header header = message.getHeader(); header.setField(new BeginString("FIX.4.2")); header.setField(new SenderCompID(TW)); header.setField(new TargetCompID("TARGET")); header.setField(new MsgType("D")); message.setField(new OrigClOrdID("123")); message.setField(new ClOrdID("321")); message.setField(new Symbol("LNUX")); message.setField(new Side(Side.BUY)); message.setField(new Text("Cancel My Order!")); Session.sendToTarget(message); }
Finally, the highly recommended method is to use the type safe message classes. This
should typically be the only way you should ever have to create messages. Here the constructor
takes in all the required fields and adds the correct MsgType
and BeginString
for you.
What's more, by using the set
method instead of setField, the compiler will not let
you add a field that is not a part of a OrderCancelRequest
based on the FIX4.1 specs. Keep in
mind you can still use setField
if you want to force any field you want into the message.
import quickfix.*; void sendOrderCancelRequest() throws SessionNotFound { quickfix.fix41.OrderCancelRequest message = new quickfix.fix41.OrderCancelRequest( new OrigClOrdID("123"), new ClOrdID("321"), new Symbol("LNUX"), new Side(Side.BUY)); message.set(new Text("Cancel My Order!")); Session.sendToTarget(message, "TW", "TARGET"); }