Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I set the default global Java Socket Timeout?

Tags:

java

sockets

I am running into a problem where various places in my application involving http and ftp connections are hanging indefinitely (observed for hours at a time before killing the java process) on java.net.SocketInputStream.socketRead0.

I'd like to override the default socket timeout of 0 globally so that I don't have to set it in every single location where I initiate a connection. Is there some global Java property or method I can call to set the default socket timeout to something other than 0?

like image 838
binarylegit Avatar asked Aug 30 '25 18:08

binarylegit


1 Answers

It depends on how your application creates sockets. If your application code uses URLConnection then you can set default timeout using JVM properties:

-Dsun.net.client.defaultReadTimeout=30000
-Dsun.net.client.defaultConnectTimeout=5000

This properties are implementation specific but still works for latest Oracle/OpenJDK.

https://docs.oracle.com/javase/8/docs/technotes/guides/net/properties.html

like image 89
Leonid Talalaev Avatar answered Sep 02 '25 08:09

Leonid Talalaev