FIX Antenna Java provides few ways to define listening ports for incoming FIX sessions. It can be defined explicitly, via API, or implicitly, if the port is defined in the acceptor configuration.
API for managing listening ports
FIXServer class is responsible for listening to incoming connections and initializing acceptors.
Open ports for listening
There are few ways to open ports in FIX Antenna-based server application:
Listening ports can be defined via FIXServer constructors:
// Create a FIX server, with default configuration from fixengine.properties, which listen ports 8090, 8091, 8092 FIXServer server = new FIXServer(8090, 8091, 8092); server.start(); ...
// Create a FIX server, with custom configuration, which listen ports 8090, 8091, 8092 FIXServer server = new FIXServer(serverConfiguration, 8090, 8091, 8092); server.start(); ...
- It's possible to set listening ports for already existing FIXServer instance but before its start:
// Create a FIX server, with custom configuration, but without any which listen ports FIXServer server = new FIXServer(); // Configure server to listen to ports 8090, 8091, 8092 server.setPorts(8090, 8091, 8092); server.start(); ...
There is also a way to open a listening port for already started server:
// Create a FIX server, with custom configuration, but without any which listen ports FIXServer server = new FIXServer(); server.start() ... // Open port 8090 to listen server.openPort(8090);
FIXServer.openPort() method can be used with the second parameter, which defines is the port can be automatically closed when deregistered the last acceptor for such port(see Managing listening ports via configuration):
// Create a FIX server, with custom configuration, but without any which listen ports FIXServer server = new FIXServer(); server.start() ... // Open port 8090 to listen server.openPort(8090, true); // autoclose = true
FIXServer.openPort() method can be used with the second parameter, which defines is the port can be automatically closed when deregistered the last acceptor for such port:
Check open ports
FIXServer class provides two methods for checking the list of open ports
FIXServe.getPorts() return a list of actually configured ports. It also returns a list of ports, defined with constructor or with setPorts() method, before the start:
FIXServer server = new FIXServer(); server.getPorts(); // returns {} server.setPorts(8090, 8091, 8092); server.getPorts(); // returns {8090, 8091, 8092} server.start; server.getPorts(); // returns {8090, 8091, 8092}
FIXServe.getOpenPorts() return a list of opened ports - only those ones with can accept incoming connections:
FIXServer server = new FIXServer(); server.setPorts(8090, 8091, 8092); server.getOpenPorts(); // returns {} server.start; server.getOpenPorts(); // returns {8090, 8091, 8092}
Close open ports
Closing the port explicitly:
FIXServer server = new FIXServer(); ... server.getOpenPorts(); // returns {8094} server.closePort(8094); server.getOpenPorts(); // returns {}: 8094 was auto closed
Managing listening ports via configuration
FIX Antenna Java engine can automatically manage listening ports for server during registration/deregistration acceptor sessions. It works only if the port property is defined for the acceptor session. Additionally, such an acceptor could be connected only on defined port:
// Define configuration for acceptor with a port parameter SessionParameters acceptorParameters = new SessionParameters(); acceptorParameters.setSenderCompId("acceptor"); acceptorParameters.setTargetCompId("target1"); acceptorParameters.setPort("8090"); // check that server is started but there is no open ports server.getOpenPorts(); // returns {} // register acceptor server.registerAcceptorSession(sessionParameters); server.getOpenPorts(); // returns {8090}
The port will be automatically closed if the last acceptor with this port is unregistered. It needs to note that this rule is true only for ports which were opened automatically by registering acceptor or via openPort() call with parameter autoclose = true:
// The acceptor with property "port" with value "8090" is registered on server and port is open to listen server.getOpenPorts(); // returns {8090} // unregister acceptor - port will be automatically closed server.unregisterAcceptorSession(acceptorParameters); // acceptorParameters.getPort() == 8090 server.getOpenPorts(); // returns {} - port 8090 was auto closed