You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
By implementing these interface methods in your derived class, you are requesting to be notified of events that occur on the FIX engine. The function that you should be most aware of is `fromApp`.
53
+
54
+
Here are explanations of what these functions provide for you.
55
+
56
+
`onCreate` is called when QFJ creates a new session. A session comes into and remains in existence for the life of the application. Sessions exist whether or not a counter party is connected to it. As soon as a session is created, you can begin sending messages to it. If no one is logged on, the messages will be sent at the time a connection is established with the counterparty.
57
+
58
+
`onLogon` notifies you when a valid logon has been established with a counter party. This is called when a connection has been established and the FIX logon process has completed with both parties exchanging valid logon messages.
59
+
60
+
`onLogout` notifies you when an FIX session is no longer online. This could happen during a normal logout exchange or because of a forced termination or a loss of network connection.
61
+
62
+
`toAdmin` provides you with a peek at the administrative messages that are being sent from your FIX engine to the counter party. This is normally not useful for an application however it is provided for any logging you may wish to do. Notice that the `quickfix.Message` is mutable. This allows you to add fields before an adminstrative message before it is sent out.
63
+
64
+
`toApp` is a callback for application messages that are being sent to a counterparty. If you throw a `DoNotSend` exception in this method, the application will not send the message. This is mostly useful if the application has been asked to resend a message such as an order that is no longer relevant for the current market. Messages that are being resent are marked with the `PossDupFlag` in the header set to true; If a `DoNotSend` exception is thrown and the flag is set to true, a sequence reset will be sent in place of the message. If it is set to false, the message will simply not be sent. Notice that the `quickfix.Message` is mutable. This allows you to add fields to an application message before it is sent out.
65
+
66
+
`fromAdmin` notifies you when an administrative message is sent from a counterparty to your FIX engine. This can be useful for doing extra validation on `Logon` messages such as for checking passwords. Throwing a `RejectLogon` exception will disconnect the counterparty.
67
+
68
+
`fromApp` is one of the core entry points for your FIX application. Every application level request will come through here. If, for example, your application is a sell-side OMS, this is where you will get your new order requests. If you were a buy side, you would get your execution reports here. If a `FieldNotFound` exception is thrown, the counterparty will receive a reject indicating a conditionally required field is missing. The `Message` class will throw this exception when trying to retrieve a missing field, so you will rarely need the throw this explicitly. You can also throw an `UnsupportedMessageType` exception. This will result in the counterparty getting a reject informing them your application cannot process those types of messages. An `IncorrectTagValue` can also be thrown if a field contains a value that is out of range or you do not support.
69
+
70
+
71
+
The sample code below shows how you might start up a FIX acceptor which listens on a socket. If you wanted an initiator, you would simply replace the acceptor in this code fragment with a `SocketInitiator`. `ThreadedSocketInitiator` and `ThreadedSocketAcceptor` classes are also available. These will supply a thread to each session that is created. If you use these you must make sure your application is thread safe.
72
+
73
+
```
74
+
import quickfix.*;
75
+
import java.io.FileInputStream;
76
+
77
+
public class MyClass {
78
+
79
+
public static void main(String args[]) throws Exception {
80
+
if (args.length != 1) return;
81
+
String fileName = args[0];
82
+
83
+
// FooApplication is your class that implements the Application interface
84
+
Application application = new FooApplication();
85
+
86
+
SessionSettings settings = new SessionSettings(new FileInputStream(fileName));
87
+
MessageStoreFactory storeFactory = new FileStoreFactory(settings);
88
+
LogFactory logFactory = new FileLogFactory(settings);
89
+
MessageFactory messageFactory = new DefaultMessageFactory();
Most of the messages you will be interested in looking at will be arriving in your overloaded `fromApp` method of your application. You can get fields out of messages with different degrees of type safety. The type in question here is the FIX message type.
102
+
103
+
When the application passes you a `Message` class, the Java type checker has no idea what specific FIX message it is, you must determine that dynamically. There is, however, a way we can make Java aware of this type information.
104
+
105
+
Keep in mind that all messages have a header and a trailer. If you want to see fields in them, you must first call `getHeader()` or `getTrailer()` to get access to them. Otherwise you access them just like in the message body.
106
+
107
+
QuickFIX/J has message classes that correlate to all the messages defined in the spec. They are, just like the field classes, generated directly off of the FIX specifications. To take advantage of this, you must break the messages out with the supplied `MessageCracker`.
108
+
109
+
```
110
+
import quickfix.*;
111
+
import quickfix.field.*;
112
+
113
+
public void fromApp(Message message, SessionID sessionID)
// compile time error!! field not defined for OrderCancelRequest
136
+
ClearingAccount clearingAccount = new ClearingAccount();
137
+
message.get(clearingAccount);
138
+
}
139
+
```
140
+
141
+
In order to use this you must use the `MessageCracker` as a mixin to your application. This will provide you with the `crack` method and allow you to overload specific message functions.
142
+
143
+
Any function you do not overload will by default throw an `UnsupportedMessageType` exception
144
+
145
+
146
+
Define your application like this:
147
+
148
+
```
149
+
import quickfix.Application;
150
+
import quickfix.MessageCracker;
151
+
152
+
public class MyApplication extends MessageCracker implements quickfix.Application {
153
+
public void fromApp(Message message, SessionID sessionID)
public void myEmailHandler(quickfix.fix50.Email email, SessionID sessionID) {
161
+
// handler implementation
162
+
}
163
+
164
+
// By convention (notice different version of FIX. It's an error to have two handlers for the same message)
165
+
// Convention is "onMessage" method with message object as first argument and SessionID as second argument
166
+
public void onMessage(quickfix.fix44.Email email, SessionID sessionID) {
167
+
// handler implementation
168
+
}
169
+
}
170
+
```
171
+
172
+
If you'd rather use composition rather than inheritance of the `MessageCracker` you can construct a message cracker with a delegate object. The delegate message handler methods will be automatically discovered.
173
+
174
+
Message crackers for each FIX version are still generated for backward compatibility but it's more efficient to define the specific handlers you need.
175
+
176
+
The generated classes define handlers for all messages defined by that version of FIX. This requires the JVM to load those classes when the cracker is loaded. Most applications only need to handle a small subset of the messages defined by a FIX version so loading all the messages classes is excessive overhead in those cases.
177
+
178
+
179
+
### Sending messages
180
+
181
+
Messages can be sent to a counter party with one of the static `Session.sendToTarget` methods. This method has several signatures. They are:
182
+
183
+
```
184
+
package quickfix;
185
+
186
+
public static boolean sendToTarget(Message message)
187
+
throws SessionNotFound
188
+
189
+
public static boolean sendToTarget(Message message, SessionID sessionID)
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.
198
+
199
+
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 that you can still use `setField` if you want to force any field you want into the message.
0 commit comments