2012-08-06 60 views
0

我已經創建了兩個類:Initiator和Acceptor。我想從發起者發送消息到接受者,然後處理收到的消息。我無法發送消息。如何在quickfix上發送和接收消息?

這是我initiator.java

SocketInitiator socketInitiator = null; 
String fileName = "conf/intiator.cfg"; 
try { 
    SessionSettings initiatorSettings = new SessionSettings(new FileInputStream(fileName)); 
    Application initiatorApplication = new Initiator(); 
    FileStoreFactory fileStoreFactory = new FileStoreFactory(
      initiatorSettings); 
    FileLogFactory fileLogFactory = new FileLogFactory(
      initiatorSettings); 
    MessageFactory messageFactory = new DefaultMessageFactory(); 
    socketInitiator = new SocketInitiator(initiatorApplication, fileStoreFactory, initiatorSettings, fileLogFactory, messageFactory); 
    socketInitiator.start(); 
    Message msg = new Message(); 
    msg.setString(1, "Hello this is test Message"); 


    SessionID sessionId = (SessionID) socketInitiator.getSessions().get(0); 
    Session.lookupSession(sessionId).logon(); 
    initiatorApplication.onLogon(sessionId); 
    initiatorApplication.toApp(msg, sessionId); 
} catch (Exception e) { 
    e.printStackTrace(); 
} 

這裏是應用程序接口的它的覆蓋信息。

public void toApp(Message message, SessionID sessionId) throws DoNotSend { 
    try { 
     boolean result = quickfix.Session.sendToTarget(message, sessionId); 
     if (result) { 
      System.out.println("Message ahs send :)"); 
     } else { 
      System.out.println("Not Send"); 
     } 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 

} 

這是引發劑initiator.cfg文件

[default] 
StartTime=00:00:01 
EndTime=23:59:59 
HeartBtInt=10 
SocketUseSSL=N 
MillisecondsInTimeStamp=Y 
FileIncludeMilliseconds=Y 
CheckLatency=N 
SocketTcpNoDelay=Y 


[session] 
BeginString=FIX.4.4 
ConnectionType=initiator 
DisableQuickFixReconnLogic=Y 
AdapterUserIndex=0 

SocketConnectHost=127.0.0.1 

Timezone=America/New_York 

SocketConnectPort=3000 
UseDataDictionary=Y 
DataDictionary=conf/resources/FIX44.xml 
ValidateFieldsOutOfOrder=N 
ValidateFieldsHaveValues=N 
ValidateUserDefinedFields=N 
LogonTimeout=10 
FileStorePath=conf/connector 
FileLogPath=conf/connector/logs 
FileLogBackupPath=conf/connector 
ResetOnLogout=Y 
ResetOnDisconnect=N 
SendResetSeqNumFlag=Y 
RawData=fxall123 
#SessionQualifier=FXallStream 
MillisecondsInTimeStamp=Y 
FileIncludeMilliseconds=Y 

[session] 
BeginString=FIX.4.4 
ConnectionType=initiator 
DisableQuickFixReconnLogic=Y 
AdapterUserIndex=1 
SenderCompID=initiator-id 
#SenderSubID=trader1 
#TargetCompID=target-id 
#TargetSubID=qftrade 

SocketConnectHost=127.0.0.1 
Timezone=America/New_York 
#SocketConnectPort=443 
SocketConnectPort=3000 
UseDataDictionary=Y 
DataDictionary=conf/resources/FIX44.xml 
ValidateFieldsOutOfOrder=N 
ValidateFieldsHaveValues=N 
ValidateUserDefinedFields=N 
LogonTimeout=5 
FileStorePath=conf/connector 
FileLogPath=conf/connector 
FileLogBackupPath=conf/connector/backup 
ResetOnLogout=Y 
ResetOnLogon=Y 
ResetOnDisconnect=N 
SendResetSeqNumFlag=Y 
RawData=fxall123 
#SessionQualifier=FXallTrade 

Acceptor.java

String fileName = "conf/acceptor.cfg"; 
SocketAcceptor socketAcceptor = null; 
try { 
    FileInputStream is = new FileInputStream(fileName); 
    SessionSettings executorSettings = new SessionSettings(is); 
    Application application = new Acceptor(); 
    FileStoreFactory fileStoreFactory = new FileStoreFactory(
      executorSettings); 
    MessageFactory messageFactory = new DefaultMessageFactory(); 
    FileLogFactory fileLogFactory = new FileLogFactory(executorSettings); 
    socketAcceptor = new SocketAcceptor(application, fileStoreFactory, 
      executorSettings, fileLogFactory, messageFactory); 
    socketAcceptor.start(); 

    SessionID sessionId = (SessionID) socketAcceptor.getSessions().get(0); 
    application.onLogon(sessionId); 
    int[] i = {1, 2, 3, 4, 5}; 

    // application.fromApp(new Message(i), sessionId); 
} catch (Exception e) { 
    e.printStackTrace(); 
} 

acceptor.cfg

[default] 
StartTime=00:00:00 
EndTime=23:50:00 
HeartBtInt=10 
ReconnectInterval=6 
SocketUseSSL=N 
MillisecondsInTimeStamp=Y 
CheckLatency=N 
SocketTcpNoDelay=N 
SocketAcceptAddress=127.0.0.1 
SocketAcceptPort=3000 

[session] 
BeginString=FIX.4.4 
ConnectionType=acceptor 
#DisableQuickFixReconnLogic=Y 
AdapterUserIndex=0 
SenderCompID=target-id 
#SenderSubID=qfstream 
#TargetCompID=inttest 
#TargetSubID=trader1 
Timezone=America/New_York 
UseDataDictionary=Y 
DataDictionary=conf/resources/FIX44.xml 
ValidateFieldsOutOfOrder=N 
ValidateFieldsHaveValues=N 
ValidateUserDefinedFields=N 
LogonTimeout=5 
FileStorePath=conf/Acceptor 
FileLogPath=conf/Acceptor/logs 
ResetOnLogout=Y 
ResetOnDisconnect=N 
SendResetSeqNumFlag=Y 

誰能告訴我問題在哪裏。在配置中還是在代碼中?

+0

你看到了什麼錯誤?你怎麼知道它不工作? – 2012-08-06 13:17:39

+0

布爾結果= quickfix.Session.sendToTarget(message,sessionId);如果(結果){ System.out.println(「Message ahs send :)」); } else { System.out.println(「Not Send」); },否則語句執行 – 2012-08-06 15:37:07

回答

3

您需要對xml文件進行更改。我猜你正在使用的字段沒有在fix.4.4文件中聲明。檢查你的XML文件,並以下列方式包含它。

我想要發送QuoteID與貨幣,但在貨幣QuoteID沒有聲明。在這種情況下,您將無法發送消息。 您需要在fix.4.4 /或您正在使用的任何版本中刪除該字段(QuoteID)。

<Currency> 
// 
// 
<field name="QuoteID" required="N" /> // N or Y depend on your requirement 
// 
// 
</currency> 

也檢查日誌,你會在那裏發現錯誤信息。

可能是你不包括要求字段,也可以創建問題。 使用以下鏈接檢查需要哪些字段。 http://www.fixprotocol.org/FIXimate3.0/

5

我只想指出顯而易見(這段代碼有很多錯誤,請參閱示例以瞭解quickfixj的工作原理)。您嘗試發送的Message不是有效的FIX消息,因此在發送之前會被引擎拒絕。您需要創建一個真正的FIX消息以供quickfix發送。同樣重要的是,我需要提及的是toApp是一個事件處理程序,在發送消息時被調用。如果您打電話給quickfix.Session.sendToTarget,它會在發送時再次呼叫toApp。由於您的toApp中沒有控制語句,因此會導致無限循環。配置也有點不對,而其他一些代碼看起來很拗口,但那些是你最大的問題。請看文檔和例子。

+0

大量使用單詞hin – gdbj 2016-04-20 17:30:07