Skip to content

Commit 9d1c547

Browse files
authored
Add SessionStateListener.onConnectException() (#368)
* Add SessionStateListener.onConnectException() * Added missing `@Test` annotation * added some javadoc
1 parent 1968f7a commit 9d1c547

4 files changed

Lines changed: 108 additions & 12 deletions

File tree

quickfixj-core/src/main/java/quickfix/Session.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2885,6 +2885,10 @@ public void removeStateListener(SessionStateListener listener) {
28852885
stateListeners.removeListener(listener);
28862886
}
28872887

2888+
public SessionStateListener getStateListener() {
2889+
return stateListener;
2890+
}
2891+
28882892
/**
28892893
* @return the default application version ID for messages sent from this session
28902894
*/

quickfixj-core/src/main/java/quickfix/SessionStateListener.java

Lines changed: 63 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,42 +16,93 @@
1616
* Contact ask@quickfixengine.org if any conditions of this licensing
1717
* are not clear to you.
1818
******************************************************************************/
19-
2019
package quickfix;
2120

2221
public interface SessionStateListener {
2322

24-
default void onConnect(){
23+
/**
24+
* Called when connection has been established.
25+
*/
26+
default void onConnect() {
27+
}
28+
29+
/**
30+
* Called when Exception occurs during connection establishment.
31+
*
32+
* @param exception thrown Exception
33+
*/
34+
default void onConnectException(Exception exception) {
2535
}
2636

27-
default void onDisconnect(){
37+
/**
38+
* Called when connection has been disconnected.
39+
*/
40+
default void onDisconnect() {
2841
}
2942

30-
default void onLogon(){
43+
/**
44+
* Called when session has been logged on.
45+
*/
46+
default void onLogon() {
3147
}
3248

33-
default void onLogout(){
49+
/**
50+
* Called when session has been logged out.
51+
*/
52+
default void onLogout() {
3453
}
3554

36-
default void onReset(){
55+
/**
56+
* Called when message store gets reset.
57+
*/
58+
default void onReset() {
3759
}
3860

39-
default void onRefresh(){
61+
/**
62+
* Called when message store gets refreshed on Logon.
63+
*/
64+
default void onRefresh() {
4065
}
4166

42-
default void onMissedHeartBeat(){
67+
/**
68+
* Called when TestRequest is sent out due to missed Heartbeat.
69+
*/
70+
default void onMissedHeartBeat() {
4371
}
4472

45-
default void onHeartBeatTimeout(){
73+
/**
74+
* Called when Heartbeat timeout has been detected.
75+
*/
76+
default void onHeartBeatTimeout() {
4677
}
4778

48-
default void onResendRequestSent(int beginSeqNo, int endSeqNo, int currentEndSeqNo){
79+
/**
80+
* Called when ResendRequest has been sent out.
81+
*
82+
* @param beginSeqNo first seqnum that gets requested
83+
* @param endSeqNo last seqnum that gets requested
84+
* @param currentEndSeqNo last seqnum of range that gets requested on
85+
* chunked ResendRequests
86+
*/
87+
default void onResendRequestSent(int beginSeqNo, int endSeqNo, int currentEndSeqNo) {
4988
}
5089

51-
default void onSequenceResetReceived(int newSeqNo, boolean gapFillFlag){
90+
/**
91+
* Called when SequenceReset has been received.
92+
*
93+
* @param newSeqNo NewSeqNo from SequenceReset
94+
* @param gapFillFlag GapFillFlag from SequenceReset
95+
*/
96+
default void onSequenceResetReceived(int newSeqNo, boolean gapFillFlag) {
5297
}
5398

54-
default void onResendRequestSatisfied(int beginSeqNo, int endSeqNo){
99+
/**
100+
* Called when a received ResendRequest has been satisfied.
101+
*
102+
* @param beginSeqNo first seqnum that was requested
103+
* @param endSeqNo last seqnum that was requested
104+
*/
105+
default void onResendRequestSatisfied(int beginSeqNo, int endSeqNo) {
55106
}
56107

57108
}

quickfixj-core/src/main/java/quickfix/mina/initiator/IoSessionInitiator.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,8 +261,10 @@ private void handleConnectException(Throwable e) {
261261
final String nextRetryMsg = " (Next retry in " + computeNextRetryConnectDelay() + " milliseconds)";
262262
if (e instanceof IOException) {
263263
fixSession.getLog().onErrorEvent(e.getClass().getName() + " during connection to " + socketAddress + ": " + e + nextRetryMsg);
264+
fixSession.getStateListener().onConnectException((IOException) e);
264265
} else {
265266
LogUtil.logThrowable(fixSession.getLog(), "Exception during connection to " + socketAddress + nextRetryMsg, e);
267+
fixSession.getStateListener().onConnectException(new Exception(e));
266268
}
267269
connectFuture = null;
268270
}

quickfixj-core/src/test/java/quickfix/SocketInitiatorTest.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,45 @@ public void testDoubleStartOfInitiator() throws Exception {
229229
}
230230
}
231231

232+
@Test
233+
public void testInitiatorConnectionException() throws Exception {
234+
// use a free port to make sure nothing is listening
235+
int freePort = AvailablePortFinder.getNextAvailable();
236+
Initiator initiator = null;
237+
AtomicBoolean onConnectExceptionWasCalled = new AtomicBoolean(false);
238+
try {
239+
SessionID clientSessionID = new SessionID(FixVersions.BEGINSTRING_FIX42, "TW", "ISLD");
240+
SessionSettings settings = getClientSessionSettings(clientSessionID, freePort);
241+
settings.setString(clientSessionID, "ReconnectInterval", "1");
242+
settings.setString(clientSessionID, "SocketConnectHost", "0.0.0.0");
243+
settings.setString(clientSessionID, "SocketConnectProtocol", ProtocolFactory.getTypeString(ProtocolFactory.SOCKET));
244+
245+
SessionStateListener sessionStateListener = new SessionStateListener() {
246+
@Override
247+
public void onConnectException(Exception e) {
248+
onConnectExceptionWasCalled.set(true);
249+
}
250+
};
251+
// add state listener on creation of Session
252+
Application clientApplication = new ApplicationAdapter() {
253+
@Override
254+
public void onCreate(SessionID sessionId) {
255+
Session.lookupSession(clientSessionID).addStateListener(sessionStateListener);
256+
}
257+
};
258+
DefaultSessionFactory sessionFactory = new DefaultSessionFactory(clientApplication, new MemoryStoreFactory(), new ScreenLogFactory(settings), new DefaultMessageFactory());
259+
initiator = new SocketInitiator(sessionFactory, settings, 10000);
260+
initiator.start();
261+
Thread.sleep(3000); // make sure we try to connect
262+
} finally {
263+
if (initiator != null) {
264+
initiator.stop(true);
265+
}
266+
assertTrue(onConnectExceptionWasCalled.get());
267+
}
268+
}
269+
270+
232271
private interface LogSessionStateListener extends Log, SessionStateListener {}
233272

234273
// QFJ-907

0 commit comments

Comments
 (0)