hapifhir / hapi-hl7v2

277 stars 138 forks source link

HL7v2 - Cannot configure MLLP client connection timeout, when created from DefaultHapiContext #126

Open patrick-vachon-smilecdr opened 2 months ago

patrick-vachon-smilecdr commented 2 months ago

On Hapi-base 2.5.1 When using ca.uhn.hl7v2.DefaultHapiContext.newClient(), it ends up creating a Socket that is not using a connection timeout:

    at ca.uhn.hl7v2.app.ConnectionFactory.createSocket(ConnectionFactory.java:77)
    at ca.uhn.hl7v2.app.ConnectionFactory.openEagerly(ConnectionFactory.java:53)
    at ca.uhn.hl7v2.app.ConnectionFactory.open(ConnectionFactory.java:44)
    at ca.uhn.hl7v2.app.ConnectionHub$1.open(ConnectionHub.java:99)
    at ca.uhn.hl7v2.app.ConnectionHub$1.open(ConnectionHub.java:85)
    at ca.uhn.hl7v2.app.ConnectionHub$CountingMap.put(ConnectionHub.java:489)
    at ca.uhn.hl7v2.app.ConnectionHub.attach(ConnectionHub.java:121)
    at ca.uhn.hl7v2.app.ConnectionHub.attach(ConnectionHub.java:260)
    at ca.uhn.hl7v2.DefaultHapiContext.newClient(DefaultHapiContext.java:335)

It would be great to be able to configure it somehow, easily.

class ConnectionFactory
    ...
    private static Socket createSocket(ca.uhn.hl7v2.util.SocketFactory socketFactory, String host, int port, boolean tls)
        throws IOException {
        Socket socket;
        if (tls) {
            socket = socketFactory.createTlsSocket();
        } else {
            socket = socketFactory.createSocket();
        }
->      socket.connect(new InetSocketAddress(host, port));
        return socket;
    }

as Socket has a connect() method on which we can pass a connection timeout. May be it could be added in ca.uhn.hl7v2.util.SocketFactory and used with the appropriate Socket.connect()?

Was found with

        <dependency>
            <groupId>ca.uhn.hapi</groupId>
            <artifactId>hapi-structures-v24</artifactId>
            <version>2.5.1</version>
        </dependency>

Culprit code on master: https://github.com/hapifhir/hapi-hl7v2/blob/master/hapi-base/src/main/java/ca/uhn/hl7v2/app/ConnectionFactory.java

Thanks!

michaelabuckley commented 2 months ago

If you add a timeout to the ConnectionData, you could configure it when building the ConnectionHub. That is passed all the way down to the ConnectionFactory.