score:74

Accepted answer

I found it out myself now:

boolean isDebug = java.lang.management.ManagementFactory.getRuntimeMXBean().
    getInputArguments().toString().indexOf("jdwp") >= 0;

This will check if the Java Debug Wire Protocol agent is used.

score:5

You're solving the wrong problem. Your program doesn't need to know this unless it's dealing with eclipse or jvm internals.

Solution

Use a system property with a default value:

int timeout = Integer.parseInt( 
    System.getProperty("socket.timeout", "1000"));
socket.setSoTimeout(timeout);

And in the debug launch configuration, just add

-Dsocket.timeout=20000

to the call parameters

(If you don't specify the system property, the default value will be used)

References

score:13

Make the timeout configurable. The simplest way is to just use a system property and read it with Integer.getInteger:

private final static int SOCKET_TIMEOUT =
  Integer.getInteger("com.yourapp.module.socketTimeout", 1000); // default 1 sec

Then, when starting your app for debugging, just set the property from the command line (or an appropriate config file, depending on the environment your app runs in):

java -Dcom.yourapp.module.socketTimeout=1000000 MainClass

This is good because it does not magically alter the behavior when you fire the app up in a debugger, and you can change the timeout when not debugging (for example, if you need to run it somewhere with a slow connection, some day).

(Of course, if your system already uses a config file, it may be appropriate to add this value as an entry there instead.)

As to whether one second is an appropriate timeout... that depends completely on the app. Sometimes it's better to give a correct answer eventually, other times failing quickly is better than waiting for success.


Related Query