I have discovered some weird behaviour of HttpURLConnection
in Java. I have java client that sets up http connection, sends data to java service and returns response. During test of my application I tried to check timeout mechanism (both set up for 5000 ms), so I killed java service with kill -SIGSTOP
and I expected read/connect timeouts on client side. However, there is no timeouts on client side, connection is created, data is written and connection is freezed for ~3 minutes, then IOException
is thrown with "Error writing to server"
message. Is there any mistake in configuration of HttpUrlConnection
?
HttpURLConnection connection = (HttpURLConnection) urlObj.openConnection(config.getProxy());
try {
connection.setUseCaches(false);
connection.setDoOutput(true);
connection.setConnectTimeout(5000);
connection.setReadTimeout(5000);
connection.setRequestMethod("POST");
BufferedOutputStream stream = new BufferedOutputStream(connection.getOutputStream());
stream.write(data);
stream.flush();
stream.close();
return new response(connection);
} catch (Exception e) {
logger.log("{send} exception occurred: " + e.getMessage());
return new response(connection);
} finally {
if (null != connection) {
logger.log("{send} closing connection: " + connection);
connection.disconnect();
}
}