2016-03-14 37 views
6

セッションを初期化して1つのFIXメッセージを送信する方法の簡単な例が必要です。QuickFIX/JでFIXメッセージを送信する方法

SessionSettings settings = new SessionSettings(new FileInputStream("fix.cfg")); 

Application application = new Application(settings); 
MessageStoreFactory messageStoreFactory = new FileStoreFactory(settings); 
LogFactory logFactory = new ScreenLogFactory(true, true, true); 
MessageFactory messageFactory = new DefaultMessageFactory(); 

Initiator initiator = new SocketInitiator(application, messageStoreFactory, settings, logFactory, messageFactory); 
initiator.start(); 

答えて

8

上記のコードから、イニシエータアプリケーション(クライアント)があり、acceptorアプリケーション(サーバー)も作成する必要があることがわかりました。ベロー私はあなたがしたいことをする2つのクラスを添付しました。

public class ServerApplication implements Application { 

@Override 
public void onCreate(SessionID sessionID) { 
} 

@Override 
public void onLogon(SessionID sessionID) { 
} 

@Override 
public void onLogout(SessionID sessionID) { 
} 

@Override 
public void toAdmin(Message message, SessionID sessionID) { 
} 

@Override 
public void fromAdmin(Message message, SessionID sessionID) throws FieldNotFound, IncorrectDataFormat, IncorrectTagValue, RejectLogon { 
} 

@Override 
public void toApp(Message message, SessionID sessionID) throws DoNotSend { 
} 

@Override 
public void fromApp(Message message, SessionID sessionID) throws FieldNotFound, IncorrectDataFormat, IncorrectTagValue, UnsupportedMessageType { 
    System.out.println("FromApp: " + message); 
} 

public static void main(String[] args) throws ConfigError, FileNotFoundException, InterruptedException, SessionNotFound { 
    SessionSettings settings = new SessionSettings("res/acceptor.config"); 

    Application application = new ServerApplication(); 
    MessageStoreFactory messageStoreFactory = new FileStoreFactory(settings); 
    LogFactory logFactory = new ScreenLogFactory(true, true, true); 
    MessageFactory messageFactory = new DefaultMessageFactory(); 

    Acceptor initiator = new SocketAcceptor(application, messageStoreFactory, settings, logFactory, messageFactory); 
    initiator.start(); 

    CountDownLatch latch = new CountDownLatch(1); 
    latch.await(); 
} 

}

これが開始し、それに接続するクライアントからのメッセージを聞くままになりますサーバーアプリケーションです:

は、最初に私はacceptorアプリケーションをリストアップします。

[default] 
ApplicationID=server 
FileStorePath=storage/messages/ 
ConnectionType=acceptor 
StartTime=00:01:00 Europe/Bucharest 
EndTime=23:59:00 Europe/Bucharest 
HeartBtInt=30 
UseDataDictionary=Y 
DataDictionary=FIX42.xml 
ValidateUserDefinedFields=N 
ValidateIncomingMessage=N 
RefreshOnLogon=Y 

[session] 
BeginString=FIX.4.2 
SocketAcceptPort=9877 
SenderCompID=server 
TargetCompID=client 
AcceptorTemplate=N 
lockquote 

次に、クライアントアプリケーションコードの場合は、次のように設定ファイル(acceptor.properties)が使用されます。これは、サーバーに接続し、その後、それにメッセージを送信する試み:

public class ClientApplication implements Application { 

private static volatile SessionID sessionID; 

@Override 
public void onCreate(SessionID sessionID) { 
    System.out.println("OnCreate"); 
} 

@Override 
public void onLogon(SessionID sessionID) { 
    System.out.println("OnLogon"); 
    ClientApplication.sessionID = sessionID; 
} 

@Override 
public void onLogout(SessionID sessionID) { 
    System.out.println("OnLogout"); 
    ClientApplication.sessionID = null; 
} 

@Override 
public void toAdmin(Message message, SessionID sessionID) { 
    System.out.println("ToAdmin"); 
} 

@Override 
public void fromAdmin(Message message, SessionID sessionID) throws FieldNotFound, IncorrectDataFormat, IncorrectTagValue, RejectLogon { 
    System.out.println("FromAdmin"); 
} 

@Override 
public void toApp(Message message, SessionID sessionID) throws DoNotSend { 
    System.out.println("ToApp: " + message); 
} 

@Override 
public void fromApp(Message message, SessionID sessionID) throws FieldNotFound, IncorrectDataFormat, IncorrectTagValue, UnsupportedMessageType { 
    System.out.println("FromApp"); 
} 

public static void main(String[] args) throws ConfigError, FileNotFoundException, InterruptedException, SessionNotFound { 
    SessionSettings settings = new SessionSettings("res/initiator.config"); 

    Application application = new ClientApplication(); 
    MessageStoreFactory messageStoreFactory = new FileStoreFactory(settings); 
    LogFactory logFactory = new ScreenLogFactory(true, true, true); 
    MessageFactory messageFactory = new DefaultMessageFactory(); 

    Initiator initiator = new SocketInitiator(application, messageStoreFactory, settings, logFactory, messageFactory); 
    initiator.start(); 

    while (sessionID == null) { 
     Thread.sleep(1000); 
    } 

    final String orderId = "342"; 
    NewOrderSingle newOrder = new NewOrderSingle(new ClOrdID(orderId), new HandlInst('1'), new Symbol("6758.T"), 
      new Side(Side.BUY), new TransactTime(new Date()), new OrdType(OrdType.MARKET)); 
    Session.sendToTarget(newOrder, sessionID); 
    Thread.sleep(5000); 
} 

}

そのための設定ファイル(initiator.config)はほぼために使用されるものと同じですアクセプタ:

[default] 
ApplicationID=client 
FileStorePath=storage/messages/ 
ConnectionType=initiator 
StartTime=00:01:00 Europe/Bucharest 
EndTime=23:59:00 Europe/Bucharest 
HeartBtInt=30 
UseDataDictionary=Y 
DataDictionary=FIX42.xml 
ValidateUserDefinedFields=N 
ValidateIncomingMessage=N 
RefreshOnLogon=Y 

[session] 
BeginString=FIX.4.2 
SocketConnectHost=localhost 
SocketConnectPort=9877 
SenderCompID=client 
TargetCompID=server 

設定ファイルにはいくつかのオプションがありませんが、テスト目的では十分です。それぞれのクラスには、必要なケースをテストするためだけのメインメソッドが追加されています。通常は、開始または停止の方法が少し異なります。サーバーアプリケーションはメッセージ/接続をリッスンし、停止せず、クライアントアプリケーションは最初のメッセージを送信した直後に停止します。

+0

Tnxあなた!!!!!!!!!! – user3756506

3

クイックフィックス/ Jのインストールに含ま例、すなわちExecutorBanzaiがあります。私は、この最初のコードを持っています。そのことについてはhereを読むことができます。

クイックフィックスは、いくつかのサンプルアプリケーションが付属しています。これらのアプリケーションはquickfix/examplesディレクトリにあります。これらは優れたアプリケーション設計を示すものではなく、実際の実動システムで使用することを意図したものです。 QuickFIXでアプリケーションを構築する方法に関するチュートリアルとして提供されているだけです。

キュータは非常に単純な注文執行シミュレータです。指値注文のみをサポートし、常に完全に埋めます。

バンザイは、単純な取引クライアントです。 Executorと一緒に使用すると、注文実行の購入側と売却側の両方でQuickFIX/Jを使用する簡単な例を見ることができます。

関連する問題