Simulate java.net.ConnectException: Connection timout

Richie

I would like to artificially create a connection timeout in my java application to test my connection timeout handling. I've had a look at this thread Artificially create a connection timeout error.

But when I attempt what is suggested such as http://10.255.255.1 or http://www.google.com:81 the libs I'm using to make http calls (Apache http client 3.1 ) gives me a connection refused exception instead of a timeout.

[9/07/15 19:28:45:000 EST] 00000088 SystemErr     R IOException Connection refused: connect
[9/07/15 19:28:46:188 EST] 00000088 SystemErr     R java.net.ConnectException: Connection refused: connect
    at java.net.PlainSocketImpl.socketConnect(Native Method)
    at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:336)
    at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:201)
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:188)
    at java.net.Socket.connect(Socket.java:478)
    at sun.reflect.GeneratedMethodAccessor187.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java(Compiled Code))
    at java.lang.reflect.Method.invoke(Method.java(Compiled Code))
    at org.apache.commons.httpclient.protocol.ReflectionSocketFactory.createSocket(ReflectionSocketFactory.java:140)
    at org.apache.commons.httpclient.protocol.DefaultProtocolSocketFactory.createSocket(DefaultProtocolSocketFactory.java:125)
    at org.apache.commons.httpclient.HttpConnection.open(HttpConnection.java:707)
    at org.apache.commons.httpclient.HttpMethodDirector.executeWithRetry(HttpMethodDirector.java:387)
    at org.apache.commons.httpclient.HttpMethodDirector.executeMethod(HttpMethodDirector.java:171)
    at org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:397)
    at org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:323)

Can someone suggest what I can tweak or a different suggestion all together to allow me to test a connection timeout?


EDIT

Here is the code I'm trying to test....

    HttpClient client = new HttpClient();           
    HttpConnectionManagerParams httpConnectionManagerParams = client.getHttpConnectionManager().getParams();        
    httpConnectionManagerParams.setConnectionTimeout(45000);
    httpConnectionManagerParams.setSoTimeout(45000);
    httpConnectionManagerParams.setTcpNoDelay(false);

    // Create a method instance.
    GetMethod method = new GetMethod(url);
    HttpMethodParams httpMethodParams = method.getParams();

    // no retries
    httpMethodParams.setParameter(
            HttpMethodParams.RETRY_HANDLER,
            new DefaultHttpMethodRetryHandler(0, false));
    httpMethodParams.setSoTimeout(45000);

    try {
        // Execute the method.  
        int statusCode = client.executeMethod(method);      

        // Read the response body.
        byte[] responseBody = method.getResponseBody();
        jsonResponse = new String(
                new String(responseBody).getBytes(JsonConstants.DEFAULT_CHARSET), JsonConstants.DEFAULT_CHARSET);

    } catch (HttpException e) {
        System.err.println("HttpException " + e.getMessage());  
        e.printStackTrace();

    } catch (IOException e) {
        System.err.println("IOException " + e.getMessage());
        e.printStackTrace();

    } finally {
        // Release the connection.
        method.releaseConnection();
    }

thanks

Alp

Why don't you use mock testing ? IMHO it is one of the best mocking tools out there: JMockit. You can mock up, for example HttpClient class and throw connectionTimeOutException (or whatever you really need to test your code).

It is still unclear to me, what happens when time out occurs, from the code that you sent. Does it throw an exception ? How does your code recognize it when it happens ?

Please take a look at it. If you need further assitance, give me a shout =]

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Simulate java.net.ConnectException: Connection timout

From Dev

Hadoop: java.net.ConnectException: Connection refused

From Dev

java.net.ConnectException: Connection refused TCP

From Dev

Hadoop: java.net.ConnectException: Connection refused

From Dev

java.net.ConnectException: Connection refused: connect

From Dev

Hadoop "failed on connection exception: java.net.ConnectException: Connection refused"

From Dev

java.net.ConnectException: Connection refused: connect for HTTPS connections

From Dev

Tomcat exception - java.net.ConnectException: Connection refused: connect

From Dev

java.net.ConnectException: Connection refused: connect: localhost

From Dev

Spark - failed on connection exception: java.net.ConnectException - localhost

From Dev

java.net.ConnectException: Connection refused error when running Hive

From Dev

Jetty 9.0.6 - java.net.ConnectException: Connection refused: connect

From Dev

Unable to open debugger port : java.net.ConnectException "Connection refused"

From Dev

java.net.ConnectException: Connection refused apache mysql

From Dev

Caused by: java.net.ConnectException: Connection refused: no further information

From Dev

java.net.ConnectException: Connection timed out: no further information

From Dev

java.net.ConnectException: Connection refused: connect,Launch canceled

From Dev

java.net.ConnectException: Connection refused apache mysql

From Dev

Authentication error and java.net.ConnectException: Connection refused

From Dev

Spring boot inside docker container throws java.net.ConnectException: Connection refused (Connection refused)

From Dev

List remote refs failed: java.net.ConnectException: Connection time out: github.com

From Dev

How to resolve java net ConnectException Connection refused connect even server is up

From Dev

com.jcraft.jsch.JSchException: java.net.ConnectException: Connection refused: connect

From Dev

Hadoop standalone installation - java.net.ConnectException: Connection refused error while running jar

From Dev

IOException java.net.ConnectException: failed to connect to /127.0.0.1(port 5000):connect failed:ECONNREFUSED(Connection Refused)

From Dev

java.net.ConnectException: failed to connect to /192.168.253.3 (port 2468): connect failed: ECONNREFUSED (Connection refused)

From Dev

NIO client giving exception : java.net.ConnectException: Connection refused: no further information

From Dev

com.sun.jersey.api.client.ClientHandlerException: java.net.ConnectException: Connection refused: connect in Spring Boot

From Dev

What is the exact difference between "java.net.ConnectException: Connection timed out" and "java.net.SocketTimeoutException: connect timed out"?

Related Related

  1. 1

    Simulate java.net.ConnectException: Connection timout

  2. 2

    Hadoop: java.net.ConnectException: Connection refused

  3. 3

    java.net.ConnectException: Connection refused TCP

  4. 4

    Hadoop: java.net.ConnectException: Connection refused

  5. 5

    java.net.ConnectException: Connection refused: connect

  6. 6

    Hadoop "failed on connection exception: java.net.ConnectException: Connection refused"

  7. 7

    java.net.ConnectException: Connection refused: connect for HTTPS connections

  8. 8

    Tomcat exception - java.net.ConnectException: Connection refused: connect

  9. 9

    java.net.ConnectException: Connection refused: connect: localhost

  10. 10

    Spark - failed on connection exception: java.net.ConnectException - localhost

  11. 11

    java.net.ConnectException: Connection refused error when running Hive

  12. 12

    Jetty 9.0.6 - java.net.ConnectException: Connection refused: connect

  13. 13

    Unable to open debugger port : java.net.ConnectException "Connection refused"

  14. 14

    java.net.ConnectException: Connection refused apache mysql

  15. 15

    Caused by: java.net.ConnectException: Connection refused: no further information

  16. 16

    java.net.ConnectException: Connection timed out: no further information

  17. 17

    java.net.ConnectException: Connection refused: connect,Launch canceled

  18. 18

    java.net.ConnectException: Connection refused apache mysql

  19. 19

    Authentication error and java.net.ConnectException: Connection refused

  20. 20

    Spring boot inside docker container throws java.net.ConnectException: Connection refused (Connection refused)

  21. 21

    List remote refs failed: java.net.ConnectException: Connection time out: github.com

  22. 22

    How to resolve java net ConnectException Connection refused connect even server is up

  23. 23

    com.jcraft.jsch.JSchException: java.net.ConnectException: Connection refused: connect

  24. 24

    Hadoop standalone installation - java.net.ConnectException: Connection refused error while running jar

  25. 25

    IOException java.net.ConnectException: failed to connect to /127.0.0.1(port 5000):connect failed:ECONNREFUSED(Connection Refused)

  26. 26

    java.net.ConnectException: failed to connect to /192.168.253.3 (port 2468): connect failed: ECONNREFUSED (Connection refused)

  27. 27

    NIO client giving exception : java.net.ConnectException: Connection refused: no further information

  28. 28

    com.sun.jersey.api.client.ClientHandlerException: java.net.ConnectException: Connection refused: connect in Spring Boot

  29. 29

    What is the exact difference between "java.net.ConnectException: Connection timed out" and "java.net.SocketTimeoutException: connect timed out"?

HotTag

Archive