Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot set timeout for connection establishment of Client #89

Open
effad opened this issue Nov 24, 2022 · 1 comment
Open

Cannot set timeout for connection establishment of Client #89

effad opened this issue Nov 24, 2022 · 1 comment

Comments

@effad
Copy link

effad commented Nov 24, 2022

Consider this example:

import java.io.IOException;

import ca.uhn.hl7v2.*;
import ca.uhn.hl7v2.app.Connection;

public class HAPITest {

	public static void main(String[] args) throws IOException, HL7Exception {
		try(HapiContext context = new DefaultHapiContext()) {
			// TODO :: connection timeout
			Connection connection = context.newClient("google.com", 6666, false);				
			System.out.println("Established connection " + connection);
		} 
	}
}

since google.com will not answer the attempt to connect, the program will wait forever (i.e. hang).
There seems to be no way to fix the problem, since ca.uhn.hl7v2.app.ConnectionFactory.createSocket(SocketFactory, String, int, boolean) calls socket.connect(new InetSocketAddress(host, port));, which will in fact wait forever for the connection to be established.
ConnectionFactory cannot be overridden (all static methods), so it seems we're out of luck and would require to implement a completely new HapiContext.

@effad
Copy link
Author

effad commented Nov 24, 2022

Technically you can try to workaround be implementing a

public class ConnectTimeoutSocket extends Socket {
	@Override
	public void connect(SocketAddress endpoint) throws IOException {
		connect(endpoint, 10000);
	}
}

and

public class ConnectTimoutSocketFactory implements SocketFactory {

	@Override
	public Socket createSocket() throws IOException {
		return new ConnectTimeoutSocket();
	}
        ...

but this seems very hacky ...

effad pushed a commit to mestobo/mestobo that referenced this issue May 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant