Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

4.1. Session configuration and start - Acceptor
Note

The example below will start the server with acceptor session only defined in configuration file from section 3. To start the server with initiator session - please see section 4.2

QuickFIX/J

FIX Antenna Java

Code Block
languagejava
linenumberstrue
import quickfix.*;
import java.io.FileInputStream;
 
public class QFJApp {
 
    public static void main(String args[]) throws Exception {
        if (args.length != 1) {
            return;
        }
        // path to file with session's configuration
        String fileName = args[0];
 
        // FooApplication is your class that implements the Application interface
        Application application = new FooApplication();
 
        SessionSettings settings = new SessionSettings(new FileInputStream(fileName));
        MessageStoreFactory storeFactory = new FileStoreFactory(settings);
        MessageFactory messageFactory = new DefaultMessageFactory();
        // start acceptor
        Acceptor acceptor =
                new SocketAcceptor(application, storeFactory, settings, messageFactory);
        acceptor.start();
 
        // preventing application from exiting
        System.out.println("Press enter to exit");
        System.in.read();
        acceptor.stop();
    }
}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
 	
Code Block
languagejava
linenumberstrue
import com.epam.fix.message.FIXFieldList;
import com.epam.fixengine.*;
import com.epam.fixengine.acceptor.DenyNonRegisteredAcceptorStrategyHandler;
import com.epam.fixengine.configuration.Configuration;
 
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
 
// implements FIXServerListener to get notifications about new connections
public class SimpleAcceptorServer implements FIXServerListener {
 
    // all active sessions should have references or will be GCed
    private List<FIXSession> activeSessions = new ArrayList<FIXSession>();
 
    public static void main(String[] args) throws IOException {
        if (args.length != 1) {
            return;
        }
        String fileName = args[0];
 
        // enable server strategy for accepting only registered sessions
        Configuration globalServerConfig = Configuration.getGlobalConfiguration();
        globalServerConfig.setProperty(Configuration.SERVER_ACCEPTOR_STRATEGY,
                DenyNonRegisteredAcceptorStrategyHandler.class.getCanonicalName());
 
        // create server that will listen for TCP/IP connections
        FIXServer server = new FIXServer();
        server.setPort(9880);
        // setting listener for new connections
        server.setListener(new SimpleAcceptorServer());
        // setting path to properties file with sessions
        server.setConfigPath(fileName);
 
        // starting new thread and listen for incoming connections
        server.start();
 
        // preventing application from exiting
        System.out.println("Press enter to exit");
        System.in.read();
        // stop the thread that listens for new connections
        server.stop();
    }
 
    // this method is called for every new connection
    public void newFIXSession(FIXSession session) {
        try {
            activeSessions.add(session);
 
            // setting listener for incoming messages;
            // MyFIXSessionListener is is your class that implements
            // the FIXSessionListener interface
            session.setFIXSessionListener(new MyFIXSessionListener(session));
 
            // accept incoming session
            session.connect();
        } catch (IOException e) {
        }
    }
}

...