3

When I connect an initiator to a FIX server using SSL via the initiator.start() method, it fires up a separate thread to establish the connection. Now when this fails e.g. due to an SSL handshake issue or an issue with server connectivity, the error is thrown out to the logs but can't seemingly be trapped in code and managed. How can I detect/trap when the error happens?

Disconnecting: Socket exception (<server ip>): java.net.SocketException: Connection reset.

At the bottom of the stack trace is the threadpool executor.

I have a try-catch around the start method but as the exception happens in another thread it can't catch it.

Christoph John
  • 3,003
  • 2
  • 13
  • 23
skword
  • 311
  • 2
  • 11
  • Sorry, what do you need to trap or manage? All you need to know is whether the session is logged on or off, right? Could you explain what you want to do? – Christoph John May 18 '20 at 13:42
  • I would like to be able to detect that if there are SSL or connection issues to be able to flag it to some alerting logic along with a reason. I could put a loop on the isLoggedOn method, but I would rather handle the error gracefully. – skword May 18 '20 at 17:26

1 Answers1

5

You should implement the quickfix.SessionStateListener interface in your application.

The onDisconnect() callback should get called in your case.

By the way: the try-catch around your Initiator's start() method will only catch Exceptions that occur during the startup process, e.g. configuration errors.

Update: starting from QuickFIX/J 2.3.0 you could also implement the callback onConnectException() from the SessionStateListener which will notify you of Exceptions that happen during the initialization of the connection in the Initiator.

Christoph John
  • 3,003
  • 2
  • 13
  • 23
  • This answer only works for things that made it as far as a TCP connection but doesn't report a Session that never manages to get that far connect in the first place (i.e. wrong IP or a firewall issue). – drekbour Dec 09 '20 at 12:12
  • @drekbour Hmm, every firewall issue I had eventually was rejected or timed out with a Socket Exception. Don't know what happens on wrong IP though. Shouldn't it be the same? – Christoph John Dec 09 '20 at 12:48
  • `java.net.ConnectException during connection to /localhost:80: java.net.ConnectException: Connection timed out (Next retry in 30000 milliseconds)` This is sent to the qfj Log framework then swallowed by quickfix.mina.initiator.IoSessionInitiator.ConnectTask#handleConnectException – drekbour Dec 09 '20 at 12:58
  • Ok. Could you open a PR or issue on github? Thanks – Christoph John Dec 09 '20 at 13:07
  • Hi @ChristophJohn, what's the difference between `onDisconnect()` and `onConnectException()`. Does `onConnectException()` will cause `onDisconnect()` – macemers Jun 15 '23 at 01:49
  • @macemers `onConnectException()` is only called on initiators when there is an Exception during network connection establishment. `onDisconnect()` is called when an already established session is disconnected. Regardless of if it was an initator or acceptor connection. – Christoph John Jun 15 '23 at 09:41