Search code examples
jodd

Setting timeout for Jodd HTTP client?


I am using Jodd HTTP client for testing some REST APIs. Everything works fine (great, actually:) except I am not sure how to set the socket time out value. So sometimes, when server is not up, I need to wait for long time (because of default systems timeout is big).

I am using http request like this:

HttpResponse response = HttpRequest.get("http://server").query("p1", "v1")....send();

I do not see any timeout related method in HttpRequest class.


Solution

  • You have two options here.

    The first way is actually described in the docs :) Anyway, the idea is to tweak the HttpConnection like this:

    HttpRequest request = HttpRequest.get()...;
    request.open();
    SocketHttpConnection httpConnection = (SocketHttpConnection) request.httpConnection();
    Socket socket = httpConnection.getSocket();
    socket.setSoTimeout(1000);
    ...
    HttpResponse response = request.send();
    

    However, since this is something often required, we added in latest version (not yet released) the timeout method, so in the future you would be able to set it just with:

    httpRequest.timeout(1000);
    

    Hope this helps.