实现客户端向服务器下单,服务器回应
log4j.properties
#file name: log4j.properties
app.log.home=/wls/apache/applogs/Pagi_qer
app.name=PAGI-QER
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.Threshold=INFO
log4j.appender.CONSOLE.Encoding=UTF-8
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=[%d{yyyy-MM-dd HH\:mm\:ss\:SSS} [%p]][${app.name}] %X{module}-%X{random}: %m%n
log4j.appender.PRODUCT=org.apache.log4j.DailyRollingFileAppender
log4j.appender.PRODUCT.Append=true
log4j.appender.PRODUCT.DatePattern='.'yyyy-MM-dd
log4j.appender.PRODUCT.File=${app.log.home}/info.log
log4j.appender.PRODUCT.Threshold=INFO
log4j.appender.PRODUCT.Encoding=UTF-8
log4j.appender.PRODUCT.layout=org.apache.log4j.PatternLayout
log4j.appender.PRODUCT.layout.ConversionPattern=[%d{yyyy-MM-dd HH\:mm\:ss\:SSS} [%p]][${app.name}] %X{module}-%X{random}: %m%n
log4j.appender.PRODUCT-ERROR=org.apache.log4j.DailyRollingFileAppender
log4j.appender.PRODUCT-ERROR.Append=true
log4j.appender.PRODUCT-ERROR.DatePattern='.'yyyy-MM-dd
log4j.appender.PRODUCT-ERROR.File=${app.log.home}/error.log
log4j.appender.PRODUCT-ERROR.Threshold=ERROR
log4j.appender.PRODUCT-ERROR.Encoding=UTF-8
log4j.appender.PRODUCT-ERROR.layout=org.apache.log4j.PatternLayout
log4j.appender.PRODUCT-ERROR.layout.ConversionPattern=[%d{yyyy-MM-dd HH\:mm\:ss\:SSS} [%p]][${app.name}] %X{module}-%X{random}: %m%n
log4j.category.org.hibernate.type=trace
log4j.logger.org.springframework=INFO
log4j.rootLogger=INFO, CONSOLE ,PRODUCT,PRODUCT-ERROR
quickfix.properties
#quickfix-server.properties
[default]
ConnectionType=initiator
HeartBtInt=30
ReconnectInterval=0
FileStorePath=F:\quantization_01\temp
FileLogPath=log
UseDataDictionary=N
SocketConnectHost=127.0.0.1
DataDictionary=src/resources/properties/fix42-jp.xml
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
fixInitialtor.java
package quickfixInititor.quickfixInititor;
import java.time.LocalDateTime;
import org.apache.log4j.PropertyConfigurator;
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;
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("quickfix-server.properties");
servercom.startServer();
}
/**
* 测试本地使用的main方法
*
* @param args
* @throws FieldConvertError
* @throws ConfigError
* @throws InterruptedException
* @throws ProxoolException
*/
public static void main(String[] args) throws ConfigError, FieldConvertError, InterruptedException {
// 配置LOG日记
PropertyConfigurator.configure("src/resources/properties/log4j.properties");
FixInitialtor fixInitialtor = new FixInitialtor("src/resources/properties/quickfix-server.properties");
fixInitialtor.startServer();
SessionID sessionID = new SessionID("FIX.4.2", "CLIENT1", "FixServer");
while(true){
NewOrderSingle order = new NewOrderSingle();
LocalDateTime date = LocalDateTime.now();
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(date));
order.set(new OrdType(OrdType.LIMIT));
try {
//35=D – New Order Single
Session.sendToTarget(order, sessionID);
Thread.sleep(3000);
} catch (SessionNotFound e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
fixInitialtorApplication.java
package quickfixInititor.quickfixInititor;
import quickfix.Application;
import quickfix.DoNotSend;
import quickfix.FieldNotFound;
import quickfix.IncorrectDataFormat;
import quickfix.IncorrectTagValue;
import quickfix.Message;
import quickfix.RejectLogon;
import quickfix.SessionID;
import quickfix.UnsupportedMessageType;
/**
*
* 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
System.out.println("From Admin: " + arg0.toString());
}
@Override
public void fromApp(Message arg0, SessionID arg1) throws FieldNotFound,
IncorrectDataFormat, IncorrectTagValue, UnsupportedMessageType {
System.out.println("From App: " + arg0.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 toApp: " + arg0.toString());
}
@Override
public void toApp(Message msg, SessionID sessionId) throws DoNotSend {
System.out.println("Sender toApp: " + msg.toString());
}
}
quickfix-service.properties
#quickfix-server.properties
[default]
ConnectionType=acceptor
SocketAcceptAddress=127.0.0.1
SocketAcceptPort=10003
SocketReuseAddress=Y
FileLogPath=log
FileStorePath=F:/quantization_02/temp
DataDictionary=src/resources/properties/fix42-jp.xml
[session]
BeginString=FIX.4.2
SenderCompID=FixServer
TargetCompID=CLIENT1
HeartBtInt=180
ResetOnLogon=Y
ResetSeqNumFlag=Y
StartTime=00:00:00
EndTime=23:00:00
fixServer.java
package quickfix01.quickfix01;
import java.util.Scanner;
import org.apache.log4j.PropertyConfigurator;
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;
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("quickfix-server.properties");
servercom.startServer();
}
/**
* 测试本地使用的main方法
*
* @param args
* @throws FieldConvertError
* @throws ConfigError
* @throws ProxoolException
*/
public static void main(String[] args) throws ConfigError, FieldConvertError {
// 配置LOG日记
PropertyConfigurator.configure("src/resources/properties/log4j.properties");
FixServer fixServer = new FixServer("src/resources/properties/quickfix-server.properties");
//SessionID sessionID = new SessionID("FIX.4.2", "FixServer", "CLIENT1");
fixServer.startServer();
Scanner reader = new Scanner(System.in);
System.out.println("press to quit" );
reader.nextLine();
fixServer.stop();
System.out.println("quit success");
}
}
FixServerApplication.java
package quickfix01.quickfix01;
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;
/**
*
* 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
}
}
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<groupId>quickfix01groupId>
<artifactId>quickfix01artifactId>
<version>0.0.1-SNAPSHOTversion>
<name>quickfix01name>
<url>http://www.example.comurl>
<properties>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
<maven.compiler.source>1.7maven.compiler.source>
<maven.compiler.target>1.7maven.compiler.target>
properties>
<dependencies>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>4.11version>
<scope>testscope>
dependency>
<dependency>
<groupId>org.quickfixjgroupId>
<artifactId>quickfixj-coreartifactId>
<version>2.0.0version>
dependency>
<dependency>
<groupId>org.quickfixjgroupId>
<artifactId>quickfixj-messages-allartifactId>
<version>2.0.0version>
dependency>
<dependency>
<groupId>log4jgroupId>
<artifactId>log4jartifactId>
<version>1.2.17version>
dependency>
<dependency>
<groupId>org.apache.logging.log4jgroupId>
<artifactId>log4j-coreartifactId>
<version>2.10.0version>
dependency>
dependencies>
<build>
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-clean-pluginartifactId>
<version>3.0.0version>
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>