Skip to content

Commit 7f098eb

Browse files
stasalchrjohn
andauthored
Wrap ConnectionException and add socketAddress to details (#451)
* Wrap connection exception and add socketAddress to details #450 * add test assertions, add getter for socketAddress * Update and rename ConnectionException.java to ConnectException.java * Moved common code out of if-block Co-authored-by: Christoph John <christoph.john@macd.com>
1 parent c285ea8 commit 7f098eb

File tree

3 files changed

+68
-6
lines changed

3 files changed

+68
-6
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*******************************************************************************
2+
* Copyright (c) quickfixengine.org All rights reserved.
3+
*
4+
* This file is part of the QuickFIX FIX Engine
5+
*
6+
* This file may be distributed under the terms of the quickfixengine.org
7+
* license as defined by quickfixengine.org and appearing in the file
8+
* LICENSE included in the packaging of this file.
9+
*
10+
* This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING
11+
* THE WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A
12+
* PARTICULAR PURPOSE.
13+
*
14+
* See http://www.quickfixengine.org/LICENSE for licensing information.
15+
*
16+
* Contact ask@quickfixengine.org if any conditions of this licensing
17+
* are not clear to you.
18+
******************************************************************************/
19+
20+
package quickfix.mina.initiator;
21+
22+
import java.io.IOException;
23+
import java.net.SocketAddress;
24+
25+
public class ConnectException extends IOException {
26+
27+
private final SocketAddress socketAddress;
28+
29+
public ConnectException(SocketAddress socketAddress) {
30+
this.socketAddress = socketAddress;
31+
}
32+
33+
public ConnectException(String message, Throwable cause, SocketAddress socketAddress) {
34+
super(message, cause);
35+
this.socketAddress = socketAddress;
36+
}
37+
38+
public ConnectException(String message, SocketAddress socketAddress) {
39+
super(message);
40+
this.socketAddress = socketAddress;
41+
}
42+
43+
public ConnectException(Throwable cause, SocketAddress socketAddress) {
44+
super(cause.getMessage(), cause);
45+
this.socketAddress = socketAddress;
46+
}
47+
48+
public SocketAddress getSocketAddress() {
49+
return socketAddress;
50+
}
51+
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -261,13 +261,13 @@ private void handleConnectException(Throwable e) {
261261
e = e.getCause();
262262
}
263263
final String nextRetryMsg = " (Next retry in " + computeNextRetryConnectDelay() + " milliseconds)";
264+
ConnectException wrappedException = new ConnectException(e, socketAddress);
264265
if (e instanceof IOException) {
265266
fixSession.getLog().onErrorEvent(e.getClass().getName() + " during connection to " + socketAddress + ": " + e + nextRetryMsg);
266-
fixSession.getStateListener().onConnectException(fixSession.getSessionID(), (IOException) e);
267267
} else {
268268
LogUtil.logThrowable(fixSession.getLog(), "Exception during connection to " + socketAddress + nextRetryMsg, e);
269-
fixSession.getStateListener().onConnectException(fixSession.getSessionID(), new Exception(e));
270269
}
270+
fixSession.getStateListener().onConnectException(fixSession.getSessionID(), wrappedException);
271271
connectFuture = null;
272272
}
273273

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

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import quickfix.field.MsgType;
3232
import quickfix.mina.ProtocolFactory;
3333
import quickfix.mina.SingleThreadedEventHandlingStrategy;
34+
import quickfix.mina.initiator.ConnectException;
3435
import quickfix.mina.ssl.SSLSupport;
3536
import quickfix.test.acceptance.ATServer;
3637
import quickfix.test.util.StackTraceUtil;
@@ -41,15 +42,18 @@
4142
import java.lang.management.ManagementFactory;
4243
import java.lang.management.ThreadInfo;
4344
import java.lang.management.ThreadMXBean;
45+
import java.net.InetSocketAddress;
4446
import java.net.ServerSocket;
4547
import java.net.Socket;
48+
import java.net.SocketAddress;
4649
import java.util.HashMap;
4750
import java.util.concurrent.CountDownLatch;
4851
import java.util.concurrent.ExecutorService;
4952
import java.util.concurrent.Executors;
5053
import java.util.concurrent.TimeUnit;
5154
import java.util.concurrent.atomic.AtomicBoolean;
5255
import java.util.concurrent.atomic.AtomicInteger;
56+
import java.util.concurrent.atomic.AtomicReference;
5357
import java.util.logging.Level;
5458

5559
import static junit.framework.TestCase.assertNotNull;
@@ -233,22 +237,25 @@ public void testDoubleStartOfInitiator() throws Exception {
233237
}
234238

235239
@Test
236-
public void testInitiatorConnectionException() throws Exception {
240+
public void testInitiatorConnectException() throws Exception {
237241
// use a free port to make sure nothing is listening
238242
int freePort = AvailablePortFinder.getNextAvailable();
239243
Initiator initiator = null;
244+
String host = "localhost";
240245
AtomicBoolean onConnectExceptionWasCalled = new AtomicBoolean(false);
246+
AtomicReference<SocketAddress> socketAddress = new AtomicReference<>(null);
241247
try {
242248
SessionID clientSessionID = new SessionID(FixVersions.BEGINSTRING_FIX42, "TW", "ISLD");
243249
SessionSettings settings = getClientSessionSettings(clientSessionID, freePort);
244250
settings.setString(clientSessionID, "ReconnectInterval", "1");
245-
settings.setString(clientSessionID, "SocketConnectHost", "localhost");
246-
settings.setString(clientSessionID, "SocketConnectProtocol", ProtocolFactory.getTypeString(ProtocolFactory.VM_PIPE));
251+
settings.setString(clientSessionID, "SocketConnectHost", host);
252+
settings.setString(clientSessionID, "SocketConnectProtocol", ProtocolFactory.getTypeString(ProtocolFactory.SOCKET));
247253

248254
SessionStateListener sessionStateListener = new SessionStateListener() {
249255
@Override
250256
public void onConnectException(SessionID sessionID, Exception e) {
251257
onConnectExceptionWasCalled.set(true);
258+
socketAddress.set(((ConnectException) e).getSocketAddress());
252259
}
253260
};
254261
// add state listener on creation of Session
@@ -261,12 +268,16 @@ public void onCreate(SessionID sessionId) {
261268
DefaultSessionFactory sessionFactory = new DefaultSessionFactory(clientApplication, new MemoryStoreFactory(), new ScreenLogFactory(settings), new DefaultMessageFactory());
262269
initiator = new SocketInitiator(sessionFactory, settings, 10000);
263270
initiator.start();
264-
Thread.sleep(3000); // make sure we try to connect
271+
Thread.sleep(5000); // make sure we try to connect
265272
} finally {
266273
if (initiator != null) {
267274
initiator.stop(true);
268275
}
269276
assertTrue(onConnectExceptionWasCalled.get());
277+
InetSocketAddress inetSocketAddress = (InetSocketAddress) socketAddress.get();
278+
assertNotNull(inetSocketAddress);
279+
assertEquals(host, inetSocketAddress.getHostName());
280+
assertEquals(freePort, inetSocketAddress.getPort());
270281
}
271282
}
272283

0 commit comments

Comments
 (0)