Setting up QuickFIX/J involves several steps, including downloading the library, configuring it, and writing some initial code to get it running. Here's a detailed guide to help you get started with QuickFIX/J.
Download the Library:
You can download the QuickFIX/J library from its official website.
Alternatively, you can include it as a dependency in your Maven or Gradle project.
Maven Dependency: Add the following dependency to your pom.xml file if you're using Maven:
<dependency>
<groupId>org.quickfixj</groupId>
<artifactId>quickfixj-core</artifactId>
<version>2.3.0</version> <!-- Replace with the latest version -->
</dependency>
Gradle Dependency: Add the following dependency to your build.gradle file if you're using Gradle:
dependencies {
implementation 'org.quickfixj:quickfixj-core:2.3.0' // Replace with the latest version
}
QuickFIX/J requires configuration files to define session settings and other parameters. The primary configuration file is quickfixj.cfg.
Create the Configuration File: Create a file named quickfixj.cfg in your project's resources directory.
[default]
ConnectionType=initiator
HeartBtInt=30
SocketConnectPort=5001
SocketConnectHost=localhost
StartTime=00:00:00
EndTime=00:00:00
UseDataDictionary=Y
FileStorePath=store
FileLogPath=log
[session]
BeginString=FIX.4.4
SenderCompID=CLIENT1
TargetCompID=SERVER
Create a class that implements the quickfix.Application interface. This interface has methods for handling FIX session events and messages.
import quickfix.Application;
import quickfix.Message;
import quickfix.SessionID;
import quickfix.field.MsgType;
import quickfix.fix44.NewOrderSingle;
public class FixApplication implements Application {
@Override
public void onCreate(SessionID sessionId) {
System.out.println("Session created: " + sessionId);
}
@Override
public void onLogon(SessionID sessionId) {
System.out.println("Session logged on: " + sessionId);
}
@Override
public void onLogout(SessionID sessionId) {
System.out.println("Session logged out: " + sessionId);
}
@Override
public void toAdmin(Message message, SessionID sessionId) {
System.out.println("To Admin: " + message);
}
@Override
public void fromAdmin(Message message, SessionID sessionId) {
System.out.println("From Admin: " + message);
}
@Override
public void toApp(Message message, SessionID sessionId) throws quickfix.DoNotSend {
System.out.println("To App: " + message);
}
@Override
public void fromApp(Message message, SessionID sessionId) throws quickfix.FieldNotFound, quickfix.IncorrectDataFormat, quickfix.IncorrectTagValue, quickfix.UnsupportedMessageType {
System.out.println("From App: " + message);
// Example: Handle New Order Single message
if (message.getHeader().getField(new MsgType()).valueEquals(MsgType.ORDER_SINGLE)) {
NewOrderSingle newOrderSingle = (NewOrderSingle) message;
System.out.println("Received New Order Single: " + newOrderSingle);
}
}
}
Create a main class to initialize and start the QuickFIX/J engine.
import quickfix.*;
public class Main {
public static void main(String[] args) throws ConfigError {
SessionSettings settings = new SessionSettings("path/to/quickfixj.cfg");
Application application = new FixApplication();
MessageStoreFactory storeFactory = new FileStoreFactory(settings);
LogFactory logFactory = new FileLogFactory(settings);
MessageFactory messageFactory = new DefaultMessageFactory();
// Create the initiator
Initiator initiator = new SocketInitiator(application, storeFactory, settings, logFactory, messageFactory);
// Start the initiator
initiator.start();
// Keep the application running
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
System.out.println("Shutting down QuickFIX/J engine.");
initiator.stop();
}));
try {
Thread.sleep(60000); // Keep the application running for 1 minute for demo purposes
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
Compile your project and ensure all dependencies are correctly set up.
Run the Main class to start the QuickFIX/J engine.
Message Store: You can customize the message store to use databases or other storage mechanisms by using different MessageStoreFactory implementations.
Logging: Customize logging by using different LogFactory implementations.
Session Settings: Configure additional session parameters like SSL, reconnect intervals, etc., in the quickfixj.cfg file.
Error Handling: Implement robust error handling in your Application implementation.
Configuration Management: Keep configuration files secure and manage different environments (development, testing, production) separately.
Testing: Thoroughly test your FIX engine setup with both unit tests and integration tests.
By following this setup guide, you should be able to get QuickFIX/J up and running for your FIX protocol communication needs.