2023年11月29日发(作者:)
quickfix协议实例
实现客户端向服务器下单,服务器回应
客户端
⽬录结构
ties
#file name: ties
app.=/wls/apache/applogs/Pagi_qer
app.name=PAGI-QER
log4j.E=org.4j.ConsoleAppender
log4j.old=INFO
log4j.ng=UTF-8
log4j.=org.4j.PatternLayout
log4j.sionPattern=[%d{yyyy-MM-dd HH:mm:ss:SSS} [%p]][${app.name}] %X{module}-%X{random}: %m%n
log4j.T=org.4j.DailyRollingFileAppender
log4j.=true
log4j.ttern='.'yyyy-MM-dd
log4j.=${app.}/info.log
log4j.old=INFO
log4j.ng=UTF-8
log4j.=org.4j.PatternLayout
log4j.sionPattern=[%d{yyyy-MM-dd HH:mm:ss:SSS} [%p]][${app.name}] %X{module}-%X{random}: %m%n
log4j.T-ERROR=org.4j.DailyRollingFileAppender
log4j.T-ERROR.Append=true
log4j.T-ERROR.DatePattern='.'yyyy-MM-dd
log4j.T-ERROR.File=${app.}/error.log
log4j.T-ERROR.Threshold=ERROR
log4j.T-ERROR.Encoding=UTF-8
log4j.T-ERROR.layout=org.4j.PatternLayout
log4j.T-ERROR.sionPattern=[%d{yyyy-MM-dd HH:mm:ss:SSS} [%p]][${app.name}] %X{module}-%X{random}: %m%n
log4j.=trace
log4j.framework=INFO
log4j.rootLogger=INFO, CONSOLE ,PRODUCT,PRODUCT-ERROR
ties
#ties
[default]
ConnectionType=initiator
HeartBtInt=30
ReconnectInterval=0
FileStorePath=F:quantization_01temp
FileLogPath=log
UseDataDictionary=N
SocketConnectHost=127.0.0.1
DataDictionary=src/resources/properties/
ContinueInitializationOnError=Y
[session]
BeginString=FIX.4.2
SenderCompID=CLIENT1
TargetCompID=FixServer
SocketConnectHost=127.0.0.1
SocketConnectPort=10003
StartTime=00:00:00
EndTime=23:00:00
ResetOnLogon=Y
ResetSeqNumFlag=Y
package ixInititor;
import ateTime;
import tyConfigurator;
import ation;
import Error;
import tMessageFactory;
import onvertError;
import gFactory;
import oreFactory;
import tory;
import eFactory;
import eStoreFactory;
import eError;
}
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();
/**
*
* eStore 有2种实现。 ore,ore .
* 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 {
();
}
private void stop(){
();
}
/**
* 被调⽤的start⽅法
*
* @throws ConfigError
* @throws FieldConvertError
*/
public static void start() throws ConfigError, FieldConvertError {
FixInitialtor servercom = new FixInitialtor("ties");
erver();
}
/**
* 测试本地使⽤的main⽅法
*
* @param args
* @throws FieldConvertError
* @throws ConfigError
* @throws InterruptedException
* @throws ProxoolException
*/
public static void main(String[] args) throws ConfigError, FieldConvertError, InterruptedException {
// 配置LOG⽇记
ure("src/resources/properties/ties");
ure("src/resources/properties/ties");
FixInitialtor fixInitialtor = new FixInitialtor("src/resources/properties/ties");
erver();
SessionID sessionID = new SessionID("FIX.4.2", "CLIENT1", "FixServer");
while(true){
NewOrderSingle order = new NewOrderSingle();
LocalDateTime date = ();
(new ClOrdID("5678"));
(new Account("100"));
(new HandlInst('1'));
(new OrderQty(45.00));
(new Price(25.40));
(new Symbol("USD/EUR"));
(new Side());
( new TransactTime(date));
(new OrdType());
try {
//35=D – New Order Single
Target(order, sessionID);
(3000);
} catch (SessionNotFound e) {
// TODO Auto-generated catch block
tackTrace();
}
}
}
}
package ixInititor;
import ation;
import end;
import otFound;
import ectDataFormat;
import ectTagValue;
import e;
import Logon;
import nID;
import ortedMessageType;
/**
*
* 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 FixInitialtorApplication implements Application{
@Override
public void fromAdmin(Message arg0, SessionID arg1) throws FieldNotFound,
IncorrectDataFormat, IncorrectTagValue, RejectLogon {
// TODO Auto-generated method stub
n("From Admin: " + ng());
}
@Override
public void fromApp(Message arg0, SessionID arg1) throws FieldNotFound,
IncorrectDataFormat, IncorrectTagValue, UnsupportedMessageType {
n("From App: " + ng());
}
@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
n("Sender toApp: " + ng());
}
@Override
public void toApp(Message msg, SessionID sessionId) throws DoNotSend {
n("Sender toApp: " + ng());
}
服务器端
⽂件⽬录
ties
#ties
[default]
ConnectionType=acceptor
SocketAcceptAddress=127.0.0.1
SocketAcceptPort=10003
SocketReuseAddress=Y
FileLogPath=log
FileStorePath=F:/quantization_02/temp
DataDictionary=src/resources/properties/
[session]
BeginString=FIX.4.2
SenderCompID=FixServer
TargetCompID=CLIENT1
HeartBtInt=180
ResetOnLogon=Y
ResetSeqNumFlag=Y
import edSocketAcceptor;
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();
/**
*
* eStore 有2种实现。 ore,ore .
* 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 {
();
}
private void stop(){
();
}
/**
* 被调⽤的start⽅法
*
* @throws ConfigError
* @throws FieldConvertError
*/
public static void start() throws ConfigError, FieldConvertError {
FixServer servercom = new FixServer("ties");
erver();
}
/**
* 测试本地使⽤的main⽅法
*
* @param args
* @throws FieldConvertError
* @throws ConfigError
* @throws ProxoolException
*/
public static void main(String[] args) throws ConfigError, FieldConvertError {
public static void main(String[] args) throws ConfigError, FieldConvertError {
// 配置LOG⽇记
ure("src/resources/properties/ties");
FixServer fixServer = new FixServer("src/resources/properties/ties");
//SessionID sessionID = new SessionID("FIX.4.2", "FixServer", "CLIENT1");
erver();
Scanner reader = new Scanner();
n("press
ne();
();
n("quit success");
}
}
package ix01;
import ation;
import end;
import otFound;
import ectDataFormat;
import ectTagValue;
import e;
import Logon;
import n;
import nID;
import nNotFound;
import ortedMessageType;
import D;
import ;
import ansType;
import pe;
import tus;
import D;
import ;
import ;
import ionReport;
import eCracker;
import erSingle;
/**
*
* 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) {
n("Receiver onMessage.. " + order);
}
@Override
public void fromAdmin(Message arg0, SessionID arg1) throws FieldNotFound,
IncorrectDataFormat, IncorrectTagValue, RejectLogon {
// TODO Auto-generated method stub
n("Receiver fromAdmin.. " + arg0);
}
@Override
public void fromApp(Message arg0, SessionID arg1) throws FieldNotFound,
IncorrectDataFormat, IncorrectTagValue, UnsupportedMessageType {
n("Receiver fromApp.. " + arg0);
ExecutionReport exeReport = new ExecutionReport();
(new OrderID("1234"));
(new ClOrdID("5678"));
(new ExecID("2233"));
(new ExecTransType('0'));
(new ExecType('2'));
(new OrdStatus('2'));
(new Symbol("USD/EUR"));
(new Side());
n("send exeReport infomation to client " + arg1);
try {
Target(exeReport, arg1);
} catch (SessionNotFound e) {
tackTrace();
}
}
@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
}
}
}
<project xmlns="/POM/4.0.0" xmlns:xsi="/2001/XMLSchema-instance"
xsi:schemaLocation="/POM/4.0.0 /xsd/">
<modelVersion>4.0.0modelVersion>
<groupId>quickfix01groupId>
<artifactId>quickfix01artifactId>
<version>0.0.1-SNAPSHOTversion>
<name>quickfix01name>
<url>url>
<properties>
<Encoding>UTF-8Encoding>
<>1.7>
<>1.7>
properties>
<dependencies>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>4.11version>
<scope>testscope>
dependency>
<dependency>
<groupId>ixjgroupId>
<artifactId>quickfixj-coreartifactId>
<version>2.0.0version>
dependency>
<dependency>
<groupId>ixjgroupId>
<artifactId>quickfixj-messages-allartifactId>
<version>2.0.0version>
dependency>
<dependency>
<groupId>log4jgroupId>
<artifactId>log4jartifactId>
<version>1.2.17version>
dependency>
<dependency>
<groupId>4jgroupId>
<artifactId>log4j-coreartifactId>
<version>2.10.0version>
dependency>
dependencies>
<build>
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-clean-pluginartifactId>
<version>3.0.0version>
plugin>
plugin>
<plugin>
<artifactId>maven-resources-pluginartifactId>
<version>3.0.2version>
plugin>
<plugin>
<artifactId>maven-compiler-pluginartifactId>
<version>3.7.0version>
plugin>
<plugin>
<artifactId>maven-surefire-pluginartifactId>
<version>2.20.1version>
plugin>
<plugin>
<artifactId>maven-jar-pluginartifactId>
<version>3.0.2version>
plugin>
<plugin>
<artifactId>maven-install-pluginartifactId>
<version>2.5.2version>
plugin>
<plugin>
<artifactId>maven-deploy-pluginartifactId>
<version>2.8.2version>
plugin>
plugins>
pluginManagement>
build>
project>


发布评论