Fix接口实践(quickfix协议实例)

转自:hadoove


使用到的xml

下载


项目结构目录

image.png


maven文件

        <dependency>
            <groupId>org.apache.servicemix.bundles</groupId>
            <artifactId>org.apache.servicemix.bundles.quickfix</artifactId>
            <version>1.5.3_1</version>
        </dependency>

服务端

配置文件

[default]
ConnectionType=acceptor
SocketAcceptAddress=127.0.0.1
SocketAcceptPort=7200
SocketReuseAddress=Y
FileLogPath=log
FileStorePath=D:\quickfix\temp
DataDictionary=D:\quickfix\fix42_instruction_20160907-forTest.xml

[session]
BeginString=FIX.4.2
SenderCompID=FixServer
TargetCompID=CLIENT1
HeartBtInt=180
ResetOnLogon=Y
ResetSeqNumFlag=Y
StartTime=00:00:00
EndTime=23:00:00

启动类

import java.util.Scanner;
import quickfix.Application;
import quickfix.ConfigError;
import quickfix.DefaultMessageFactory;
import quickfix.FieldConvertError;
import quickfix.FileLogFactory;
import quickfix.FileStoreFactory;
import quickfix.LogFactory;
import quickfix.MessageFactory;
import quickfix.MessageStoreFactory;
import quickfix.RuntimeError;
import quickfix.SessionSettings;
import quickfix.ThreadedSocketAcceptor;

/**
 * @Author xiaoFsu
 * @Date 2020/7/20 10:13
 * @Version 1.0
 */
public class FixServer {


    private static ThreadedSocketAcceptor acceptor = null;
    public static ThreadedSocketAcceptor getAcceptor() {
        return acceptor;
    }

    /*
     * 指定配置文件启动
     *
     * @param proFile
     * @throws ConfigError
     * @throws FieldConverError
     */
    public FixServer(String propFile) throws ConfigError, FieldConvertError {
        // 设置配置文件
        SessionSettings settings = new SessionSettings(propFile);

        // 设置一个APPlication
        Application application = new FixServerApplication();

        /**
         *
         * quickfix.MessageStore 有2种实现。 quickfix.JdbcStore,quickfix.FileStore .
         * JdbcStoreFactory 负责创建JdbcStore , FileStoreFactory 负责创建FileStorequickfix
         * 默认用文件存储,因为文件存储效率高。
         */
        MessageStoreFactory storeFactory = new FileStoreFactory(settings);

        LogFactory logFactory = new FileLogFactory(settings);

        MessageFactory messageFactory = new DefaultMessageFactory();

        acceptor = new ThreadedSocketAcceptor(application, storeFactory, settings, logFactory, messageFactory);

    }

    private void startServer() throws RuntimeError,ConfigError {
        acceptor.start();
    }

    private void stop(){
        acceptor.stop();
    }

    /**
     * 被调用的start方法
     *
     * @throws ConfigError
     * @throws FieldConvertError
     */
    public static void start() throws ConfigError, FieldConvertError {
        FixServer servercom = new FixServer("D:\\quickfix\\quickfix-server.properties");
        servercom.startServer();
    }

    /**
     * 测试本地使用的main方法
     *
     * @param args
     * @throws FieldConvertError
     * @throws ConfigError
     */
    public static void main(String[] args) throws ConfigError, FieldConvertError {
        FixServer fixServer = new FixServer("D:\\quickfix\\quickfix-server.properties");
        //SessionID sessionID = new SessionID("FIX.4.2", "FixServer", "CLIENT1");

        fixServer.startServer();

        Scanner reader = new Scanner(System.in);
        System.out.println("press <enter> to quit");
        reader.nextLine();

        fixServer.stop();
        System.out.println("quit success");
    }
}

FixServerApplication


import quickfix.Application;
import quickfix.DoNotSend;
import quickfix.FieldNotFound;
import quickfix.IncorrectDataFormat;
import quickfix.IncorrectTagValue;
import quickfix.Message;
import quickfix.RejectLogon;
import quickfix.Session;
import quickfix.SessionID;
import quickfix.SessionNotFound;
import quickfix.UnsupportedMessageType;
import quickfix.field.ClOrdID;
import quickfix.field.ExecID;
import quickfix.field.ExecTransType;
import quickfix.field.ExecType;
import quickfix.field.OrdStatus;
import quickfix.field.OrderID;
import quickfix.field.Side;
import quickfix.field.Symbol;
import quickfix.fix42.ExecutionReport;
import quickfix.fix42.MessageCracker;
import quickfix.fix42.NewOrderSingle;
/**
 * @Author xiaoFsu
 * @Date 2020/7/20 10:13
 * @Version 1.0
 */
/**
 *
 * MessageCracker是一个工具类,通过继承MessageCracker可以覆盖onMessage方法
 * 通过调用crack回调onMessage中的业务逻辑。所以所有的业务逻辑可以直接写在onMessage 方法中。
 *
 *      onCreate –>当一个Fix Session建立是调用
 *
 *      onLogon –>当一个Fix Session登录成功时候调用
 *
 *      onLogout –>当一个Fix Session退出时候调用
 *
 *      fromAdmin–>当收到一个消息,经过一系列检查,合格后,属于Admin 类型时候调用
 *
 *      fromApp–>当收到一个消息,经过一系列检查,合格后,不属于Admin 类型时候调用
 *
 *      toAdmin–>当发送一个admin类型消息调用toApp—>当发送一个非admin(业务类型)消息调用
 *
 */

public class FixServerApplication extends MessageCracker implements Application {
    @Override
    public void onMessage(NewOrderSingle order, SessionID sessionID) {
        System.out.println("Receiver onMessage..  " + order);
    }

    @Override
    public void fromAdmin(Message arg0, SessionID arg1) throws FieldNotFound,
            IncorrectDataFormat, IncorrectTagValue, RejectLogon {
        // TODO Auto-generated method stub
        System.out.println("Receiver fromAdmin..  " + arg0);
    }

    @Override
    public void fromApp(Message arg0, SessionID arg1) throws FieldNotFound,
            IncorrectDataFormat, IncorrectTagValue, UnsupportedMessageType {
        System.out.println("Receiver fromApp..  " + arg0);
        ExecutionReport exeReport = new ExecutionReport();
        exeReport.set(new OrderID("1234"));
        exeReport.set(new ClOrdID("5678"));
        exeReport.set(new ExecID("2233"));
        exeReport.set(new ExecTransType('0'));
        exeReport.set(new ExecType('2'));
        exeReport.set(new OrdStatus('2'));
        exeReport.set(new Symbol("USD/EUR"));
        exeReport.set(new Side(Side.BUY));
        System.out.println("send exeReport infomation to client " + arg1);
        try {
            Session.sendToTarget(exeReport, arg1);
        } catch (SessionNotFound e) {
            e.printStackTrace();
        }

    }

    @Override
    public void onCreate(SessionID arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onLogon(SessionID arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onLogout(SessionID arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void toAdmin(Message arg0, SessionID arg1) {
        // TODO Auto-generated method stub

    }

    @Override
    public void toApp(Message arg0, SessionID arg1) throws DoNotSend {
        // TODO Auto-generated method stub

    }
}

客户端

配置文件

[default]
ConnectionType=initiator
HeartBtInt=30
ReconnectInterval=0
FileStorePath=D:\quickfix\temp
FileLogPath=log
UseDataDictionary=N
SocketConnectHost=127.0.0.1
DataDictionary=D:\quickfix\fix42_instruction_20160907-forTest.xml
ContinueInitializationOnError=Y

[session]
BeginString=FIX.4.2
SenderCompID=CLIENT1
TargetCompID=FixServer
SocketConnectHost=127.0.0.1
SocketConnectPort=7200
StartTime=00:00:00
EndTime=23:00:00
ResetOnLogon=Y
ResetSeqNumFlag=Y

启动类

import java.util.Date;
import quickfix.Application;
import quickfix.ConfigError;
import quickfix.DefaultMessageFactory;
import quickfix.FieldConvertError;
import quickfix.FileLogFactory;
import quickfix.FileStoreFactory;
import quickfix.LogFactory;
import quickfix.MessageFactory;
import quickfix.MessageStoreFactory;
import quickfix.RuntimeError;
import quickfix.Session;
import quickfix.SessionID;
import quickfix.SessionNotFound;
import quickfix.SessionSettings;
import quickfix.SocketInitiator;
import quickfix.field.Account;
import quickfix.field.ClOrdID;
import quickfix.field.HandlInst;
import quickfix.field.OrdType;
import quickfix.field.OrderQty;
import quickfix.field.Price;
import quickfix.field.Side;
import quickfix.field.Symbol;
import quickfix.field.TransactTime;
import quickfix.fix42.NewOrderSingle;
/**
 * @Author xiaoFsu
 * @Date 2020/7/20 10:00
 * @Version 1.0
 */
public class FixInitialtor {
    private static SocketInitiator initiator = null;

    public static SocketInitiator getInitiator() {
        return initiator;
    }

    private static SessionSettings settings;

    /*
     * 指定配置文件启动
     *
     * @param proFile
     * @throws ConfigError
     * @throws FieldConverError
     */
    public FixInitialtor(String propFile) throws ConfigError, FieldConvertError {
        // 设置配置文件
        settings = new SessionSettings(propFile);

        // 设置一个APPlication
        Application application = new FixInitialtorApplication();

        /**
         *
         * quickfix.MessageStore 有2种实现。 quickfix.JdbcStore,quickfix.FileStore .
         * JdbcStoreFactory 负责创建JdbcStore , FileStoreFactory 负责创建FileStorequickfix
         * 默认用文件存储,因为文件存储效率高。
         */
        MessageStoreFactory storeFactory = new FileStoreFactory(settings);

        LogFactory logFactory = new FileLogFactory(settings);

        MessageFactory messageFactory = new DefaultMessageFactory();

        initiator = new SocketInitiator(application, storeFactory, settings, logFactory, messageFactory);


    }

    private void startServer() throws RuntimeError,ConfigError {
        initiator.start();
    }

    private void stop(){
        initiator.stop();
    }

    /**
     * 被调用的start方法
     *
     * @throws ConfigError
     * @throws FieldConvertError
     */
    public static void start() throws ConfigError, FieldConvertError {
        FixInitialtor servercom = new FixInitialtor("D:\\quickfix\\quickfix-service.properties");
        servercom.startServer();
    }

    /**
     * 测试本地使用的main方法
     *
     * @param args
     * @throws FieldConvertError
     * @throws ConfigError
     * @throws InterruptedException
     */
    public static void main(String[] args) throws ConfigError, FieldConvertError, InterruptedException {
        FixInitialtor fixInitialtor = new FixInitialtor("D:\\quickfix\\quickfix-service.properties");
        fixInitialtor.startServer();
        SessionID sessionID = new SessionID("FIX.4.2", "CLIENT1", "FixServer");
        while(true){
            NewOrderSingle order = new NewOrderSingle();
            order.set(new ClOrdID("5678"));
            order.set(new Account("100"));
            order.set(new HandlInst('1'));
            order.set(new OrderQty(45.00));
            order.set(new Price(25.40));
            order.set(new Symbol("USD/EUR"));
            order.set(new Side(Side.BUY));
            order.set( new TransactTime(new Date()));
            order.set(new OrdType(OrdType.LIMIT));
            try {
                //35=D – New Order Single
                Session.sendToTarget(order, sessionID);
                Thread.sleep(20000L);

            } catch (SessionNotFound e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

    }

}

FixInitialtorApplication

import quickfix.*;
/**
 * @Author xiaoFsu
 * @Date 2020/7/20 10:03
 * @Version 1.0
 */
public class FixInitialtorApplication implements Application {

    @Override
    public void fromAdmin(Message arg0, SessionID arg1) throws FieldNotFound,
            IncorrectDataFormat, IncorrectTagValue, RejectLogon {
        // TODO Auto-generated method stub
        System.out.println("From Admin: " + arg0.toString());
        System.out.println("From Admin: " + arg1.toString());
    }

    @Override
    public void fromApp(Message arg0, SessionID arg1) throws FieldNotFound,
            IncorrectDataFormat, IncorrectTagValue, UnsupportedMessageType {
        System.out.println("From App: " + arg0.toString());
        System.out.println("From App: " + arg1.toString());

    }

    @Override
    public void onCreate(SessionID arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onLogon(SessionID arg0) {

    }

    @Override
    public void onLogout(SessionID arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void toAdmin(Message arg0, SessionID arg1) {
        // TODO Auto-generated method stub
        System.out.println("Sender toAdmin: " + arg0.toString());
        System.out.println("Sender toAdmin: " + arg1.toString());
    }

    @Override
    public void toApp(Message msg, SessionID sessionId) throws DoNotSend {
        System.out.println("Sender toApp: " + msg.toString());
        System.out.println("Sender toApp: " + sessionId.toString());

    }
}