Search code examples
javasessionquickfixfix-protocolquickfixj

SocketInitiator getSession give session not at the same order as in the config file


When I try to get a sessionconfig by the index that exist in the file that contains the sessionConfig (in our case "InitiatorSettings.cfg")

[default]
FileStorePath=data
FileLogPath=log
HeartBtInt=30
ReconnectInterval=5

[session] 
SessionName=badisInit1
BeginString=FIX.4.2
SenderCompID=client1
TargetCompID=server1
ConnectionType=initiator
SocketConnectPort=9878
SocketConnectHost=localhost
UseDataDictionary=N

[session] 
SessionName=init0badis
BeginString=FIX.4.2
SenderCompID=client1
TargetCompID=server12
ConnectionType=initiator
SocketConnectPort=9878
SocketConnectHost=localhost
UseDataDictionary=N

[session] 
SessionName=test211
BeginString=FIX.4.2
SenderCompID=badis
TargetCompID=server
ConnectionType=initiator
SocketConnectPort=9878
SocketConnectHost=localhost
UseDataDictionary=N

I get sessions in different orders, in just the first two sessionIDs:

 get session id FIX.4.2:client1->server12
 get session id FIX.4.2:client1->server1
 get session id FIX.4.2:badis->server

there is the code that I have used to load the sessions and print them I don't know if it is normal to have different orders in the file and after we load them, or there is something wrong with my config file ..

        SessionSettings sessionSettings = new SessionSettings("InitiatorSettings.cfg");

        ApplicationImp mainApplication = new ApplicationImp();

        FileStoreFactory fileStoreFactory = new FileStoreFactory(sessionSettings);

        FileLogFactory logFactory = new FileLogFactory(sessionSettings);

        MessageFactory messageFactory = new DefaultMessageFactory();

        socketInitiator = new SocketInitiator( mainApplication,
                fileStoreFactory, this.sessionSettings, logFactory,
                messageFactory);

        for(int i=0;i<socketInitiator.getSessions().size();i++)
        {
            System.out.println("get session id "+socketInitiator.getSessions().get(i));
        }

Thanks for the help.


Solution

  • It is normal behavior that the sessions are not ordered as the SessionID->Sessions are stored in a HashMap and converted to a List on the getSessions() method.

    from SessionConnector (superclass of SocketInitiator)

    public ArrayList<SessionID> getSessions() {
        return new ArrayList<SessionID>(sessions.keySet());
    }
    

    (As a reminder, a HashMap doesn't guarantee order of the put() call. You would need LinkedHashMap for that.)

    A suggestion aside from modifying the core code, would be to add a property will an ordered list of the sessionIDs and iterate through that property and look it up in the getSessionMap()