Closed
Description
I am trying to terminate a connection if no data is being received or server is just keeping the connection open for a url by setting value for connectionTimeout and readTimeout but it is not working.
Spring boot version 2.7.1
Here is the link to SO https://stackoverflow.com/questions/73165245/setting-connectiontimeout-and-readtimeout-not-working-on-urlresource
Below code reproduces the issue.
try {
URL url = new URL("http://httpstat.us/200?sleep=20000");
UrlResource urlResource = new UrlResource(url) {
@Override
protected void customizeConnection(HttpURLConnection connection) throws IOException {
super.customizeConnection(connection);
connection.setConnectTimeout(4000);
connection.setReadTimeout(2000);
}
};
InputStream inputStream = urlResource.getInputStream();
InputStreamReader isr = new InputStreamReader(inputStream,
StandardCharsets.UTF_8);
BufferedReader br = new BufferedReader(isr);
br.lines().forEach(line -> System.out.println(line));
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
System.out.println("IO exception");
e.printStackTrace();
}
Expected:- both connectionTimeout and readTimeout should work as per given value and terminate the connection if time exceeds the timeout value