This application acts as a simple FIX acceptor.
Must be started first.
Item | Description |
---|---|
conf/sample/ReconnectorSellSide.properties | sample configuration |
conf/logback.xml | logger configuration |
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 | import biz.onixs.fix.dictionary.Version; import biz.onixs.fix.engine.Engine; import biz.onixs.fix.engine.Session; import biz.onixs.fix.engine.SessionState; import biz.onixs.util.settings.PropertyBasedSettings; import biz.onixs.util.settings.Settings; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.util.concurrent.Semaphore; public class ReconnectorSellSide implements Session.StateChangeListener { private static final Logger LOG = LoggerFactory.getLogger(ReconnectorSellSide. class ); private static final String SETTINGS_RESOURCE = "sample/ReconnectorSellSide.properties" ; private final Semaphore sessionIsDisconnected = new Semaphore( 0 ); private final Semaphore sessionIsEstablished = new Semaphore( 0 ); private Settings settings = null ; private Session session = null ; private void run() throws InterruptedException { LOG.info( "Loading settings from: {}" , SETTINGS_RESOURCE); settings = new PropertyBasedSettings(SETTINGS_RESOURCE); // LOG.info( "Starting the Engine..." ); final Engine engine = Engine.init(settings); // createSession(); session.logonAsAcceptor(); sessionIsDisconnected.acquire(); // LOG.info( "Waiting for connection..." ); sessionIsEstablished.acquire(); // LOG.info( "Waiting for disconnection..." ); sessionIsDisconnected.acquire(); session.logout(); session.dispose(); // LOG.info( "Engine shutdown..." ); engine.shutdown(); } private void createSession() { final Version fixVersion = Version.getByNumber(settings.getString( "FIXVersion" )); session = new Session(settings.getString( "SenderCompID" ), settings.getString( "TargetCompID" ), fixVersion, false ); // session.addStateChangeListener( this ); } @Override public void onStateChange( final Object sender, final Session.StateChangeArgs args) { final SessionState newState = args.getNewState(); if (SessionState.AWAIT_LOGON == newState) { sessionIsDisconnected.release(); } else if (SessionState.ESTABLISHED == newState) { sessionIsEstablished.release(); } } public static void main( final String[] args) { try { LOG.info( "ReconnectorSellSide" ); LOG.info( "The application is starting..." ); final ReconnectorSellSide sellSide = new ReconnectorSellSide(); sellSide.run(); } catch ( final Throwable throwable) { LOG.error(throwable.getMessage(), throwable); } finally { LOG.info( "The application is stopped." ); } } } |
This application acts as a FIX initiator. It demonstrates how different reconnection logic can be implemented. This reconnection logic is especially helpful when initial connection is not established during the first connection attempt.
Can be run with the “ReconnectorSellSide”. The “ReconnectorBuySide” must be started after “ReconnectorSellSide”.
Item | Description |
---|---|
conf/sample/ReconnectorBuySide.properties | sample configuration |
conf/logback.xml | logger configuration |
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 | import biz.onixs.fix.dictionary.Version; import biz.onixs.fix.engine.Engine; import biz.onixs.fix.engine.Session; import biz.onixs.fix.engine.SessionState; import biz.onixs.util.settings.PropertyBasedSettings; import biz.onixs.util.settings.Settings; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.util.concurrent.Semaphore; public class ReconnectorBuySide implements Session.StateChangeListener { private static final Logger LOG = LoggerFactory.getLogger(ReconnectorBuySide. class ); private static final String SETTINGS_RESOURCE = "sample/ReconnectorBuySide.properties" ; private final Semaphore sessionIsEstablished = new Semaphore( 0 ); private Session session = null ; private Settings settings = null ; private void run() throws InterruptedException { LOG.info( "Loading settings from: {}" , SETTINGS_RESOURCE); settings = new PropertyBasedSettings(SETTINGS_RESOURCE); // LOG.info( "Starting the Engine..." ); final Engine engine = Engine.init(settings); // createSession(); // final Reconnector reconnector = new Reconnector(); //reconnector.setAttemptCounter(new GrowingIntervalAttemptCounter()); reconnector.setSession(session); reconnector.setHost(settings.getString( "CounterpartyHost" )); reconnector.setPort(settings.getInteger( "CounterpartyPort" )); reconnector.start(); // sessionIsEstablished.acquire(); // reconnector.stop(); // session.logout( "The session is disconnected by SimpleBuySide" ); session.dispose(); // LOG.info( "Engine shutdown..." ); engine.shutdown(); } private void createSession() { final Version fixVersion = Version.getByNumber(settings.getString( "FIXVersion" )); session = new Session(settings.getString( "SenderCompID" ), settings.getString( "TargetCompID" ), fixVersion, false ); // session.setSenderSubID( "SenderSubID (50) field" ) .addStateChangeListener( this ); } @Override public void onStateChange( final Object sender, final Session.StateChangeArgs args) { final SessionState newState = args.getNewState(); if (SessionState.ESTABLISHED == newState) { sessionIsEstablished.release(); } } public static void main( final String[] args) { try { LOG.info( "ReconnectorBuySide" ); LOG.info( "The application is starting..." ); final ReconnectorBuySide buySide = new ReconnectorBuySide(); buySide.run(); } catch ( final Throwable throwable) { LOG.error(throwable.getMessage(), throwable); } finally { LOG.info( "The application is stopped." ); } } } |
The following classes are used in the sample:
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 | import biz.onixs.fix.engine.Session; import biz.onixs.fix.engine.SessionState; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.util.Objects; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; public class Reconnector implements Runnable, Session.StateChangeListener { private static final Logger LOG = LoggerFactory.getLogger(Reconnector. class ); private AttemptCounter attemptCounter = new FixedIntervalAttemptCounter(); private String host = null ; private int port = 0 ; private Session session = null ; private boolean reconnect = false ; private boolean running = false ; private boolean active = true ; private ExecutorService executorService = null ; public Reconnector() { } public AttemptCounter getAttemptCounter() { return attemptCounter; } public void setAttemptCounter( final AttemptCounter attemptCounter) { Objects.requireNonNull(attemptCounter, "null == attemptCounter" ); this .attemptCounter = attemptCounter; } public String getHost() { return host; } public void setHost( final String host) { Objects.requireNonNull(host, "null == host" ); this .host = host; } public int getPort() { return port; } public void setPort( final int port) { this .port = port; } public Session getSession() { return session; } public void setSession( final Session session) { Objects.requireNonNull(session, "null == session" ); this .session = session; } public boolean isReconnect() { return reconnect; } /** * Sets whether to connect after graceful disconnection. * * @param reconnect flag to connect after graceful disconnection */ public void setReconnect( final boolean reconnect) { this .reconnect = reconnect; } public boolean isRunning() { return running; } public boolean isActive() { return active; } public void start() { session.addStateChangeListener( this ); attemptCounter.reset(); executorService = Executors.newSingleThreadExecutor(); active = true ; executorService.submit( this ); } public void stop() { session.removeStateChangeListener( this ); active = false ; executorService.shutdownNow(); } @Override public synchronized void run() { while (attemptCounter.hasNextAttempt()) { final int interval = attemptCounter.getNextInterval(); if ((!active) || (session.getState() != SessionState.DISCONNECTED)) { return ; } try { running = true ; LOG.info( "Connect attempt #{} to {}:{}" , attemptCounter.getAttemptNumber(), host, port); LOG.info( "Logon started" ); session.logonAsInitiator(host, port); LOG.info( "Logon finished" ); running = false ; return ; } catch ( final RuntimeException e) { LOG.info( "Connect attempt #{} failed" , attemptCounter.getAttemptNumber(), e); if (attemptCounter.hasNextAttempt()) { session.resetLocalSequenceNumbers(); LOG.info( "Next connect attempt in {} msecs." , interval); try { Thread.sleep(( long ) interval); } catch ( final InterruptedException ignored) { } } else { LOG.info( "Connect attempt limit reached." ); } } } running = false ; } @Override public void onStateChange( final Object sender, final Session.StateChangeArgs args) { if ((!running) && (active) && (reconnect) && (args.getNewState() == SessionState.DISCONNECTED)) { executorService.submit( this ); } } } |
22 23 24 25 26 27 28 29 30 31 32 | public interface AttemptCounter { int getAttemptNumber(); void incrementAttemptNumber(); boolean hasNextAttempt(); int getNextInterval(); void reset(); } |
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | public abstract class AbstractAttemptCounter implements AttemptCounter { private int attemptNumber = 0 ; @Override public int getAttemptNumber() { return attemptNumber; } @Override public void incrementAttemptNumber() { attemptNumber++; } @Override public void reset() { attemptNumber = 0 ; } } |
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 | import biz.onixs.fix.engine.Engine; import biz.onixs.fix.engine.EngineSettings; public class FixedIntervalAttemptCounter extends AbstractAttemptCounter { private int maxAttemptNumber = 0 ; private int attemptInterval = 0 ; public FixedIntervalAttemptCounter() { if (Engine.isInited()) { final EngineSettings engineSettings = Engine.getInstance().getSettings(); maxAttemptNumber = engineSettings.getConnectionRetriesNumber(); attemptInterval = engineSettings.getConnectionRetriesInterval(); } } public int getMaxAttemptNumber() { return maxAttemptNumber; } public void setMaxAttemptNumber( final int maxAttemptNumber) { if ( 0 >= maxAttemptNumber) throw new IllegalArgumentException( "maxAttemptNumber <= 0" ); this .maxAttemptNumber = maxAttemptNumber; } public int getAttemptInterval() { return attemptInterval; } public void setAttemptInterval( final int attemptInterval) { if ( 0 >= attemptInterval) throw new IllegalArgumentException( "attemptInterval <= 0" ); this .attemptInterval = attemptInterval; } @Override public boolean hasNextAttempt() { return (getAttemptNumber() < maxAttemptNumber); } @Override public int getNextInterval() { if (!hasNextAttempt()) throw new IllegalStateException( "No more attempts left." ); incrementAttemptNumber(); return attemptInterval; } } |
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 | public class GrowingIntervalAttemptCounter extends AbstractAttemptCounter { private int minInterval = 5000 ; private int maxInterval = 60000 ; private int growthFactor = 2 ; private int currentInterval; public int getMinInterval() { return minInterval; } public void setMinInterval( final int minInterval) { this .minInterval = minInterval; } public int getMaxInterval() { return maxInterval; } public void setMaxInterval( final int maxInterval) { this .maxInterval = maxInterval; } public int getGrowthFactor() { return growthFactor; } public void setGrowthFactor( final int growthFactor) { if ( 0 >= growthFactor) throw new IllegalArgumentException( "" ); this .growthFactor = growthFactor; } @Override public boolean hasNextAttempt() { return true ; } @Override public int getNextInterval() { incrementAttemptNumber(); if ( 1 == getAttemptNumber()) { currentInterval = minInterval; } else if (currentInterval < maxInterval) { currentInterval *= growthFactor; } return Math.min(currentInterval, maxInterval); } } |