My application is periodically throwing Too Many Open files: Socket Exception. Lsof command shows that there are many broken sockets with "Can't identify protocol" message. So, I think somehow socket/stream are not getting closed even though I am doing clean up in final block as suggested in
Seeing too many lsof can't identify protocol
Java app with URLConnection leads "Too many open files"
Here is my code:
private static Map<String, List<String>>getResponseHeaders (URL url) throws IOException {
InputStream urlStream = null
URLConnection urlConnection = null
try{
urlConnection = (HttpURLConnection)url.openConnection()
urlConnection.setAllowUserInteraction(false)
urlConnection.setRequestProperty("User-Agent", "Agent-123")
urlConnection.setConnectTimeout(10000)
urlConnection.setReadTimeout(15000)
urlStream = urlConnection.inputStream
return urlConnection.getHeaderFields()
}catch (IOException ex){
log.info( "Could not open the connection for url "+url +" error msg "+ex.message)
throw ex
}finally {
if(urlStream){
urlStream.close()
}
if(urlConnection){
urlConnection.disconnect()
}
}
Looking for some help here. Thanks!