0

I am using Jersey-Client to check the availability of a server resource:

public class Test{


    private final Client client=...;       

    public void test(URI myURL){

        try {
            final WebTarget functionConCheck = client.target(myURL);
            final Response resp = functionConCheck.request(MediaType.APPLICATION_JSON).head();
            try {
                if (resp.getStatus() == Status.OK.getStatusCode()) {
                    System.out.println("OK");
                } 
            } finally {
                resp.close();
            }
        } catch (MalformedURLException | URISyntaxException ex) {
            LOGGER.error("Error checking", ex);
        } catch (final ProcessingException e) {
            LOGGER.error("No network connection", e);
        }
    }

}

But the code throws an exception:

ERROR [] ? (:) - No network connection
javax.ws.rs.ProcessingException: java.net.SocketException: Socket closed
    at org.glassfish.jersey.client.internal.HttpUrlConnector.apply(HttpUrlConnector.java:287) ~[jersey-client-2.22.1.jar:?]
    at org.glassfish.jersey.client.ClientRuntime.invoke(ClientRuntime.java:255) ~[jersey-client-2.22.1.jar:?]
    at org.glassfish.jersey.client.JerseyInvocation$1.call(JerseyInvocation.java:684) ~[jersey-client-2.22.1.jar:?]
    at org.glassfish.jersey.client.JerseyInvocation$1.call(JerseyInvocation.java:681) ~[jersey-client-2.22.1.jar:?]
    at org.glassfish.jersey.internal.Errors.process(Errors.java:315) ~[jersey-common-2.22.1.jar:?]
    at org.glassfish.jersey.internal.Errors.process(Errors.java:297) ~[jersey-common-2.22.1.jar:?]
    at org.glassfish.jersey.internal.Errors.process(Errors.java:228) ~[jersey-common-2.22.1.jar:?]
    at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:444) ~[jersey-common-2.22.1.jar:?]
    at org.glassfish.jersey.client.JerseyInvocation.invoke(JerseyInvocation.java:681) ~[jersey-client-2.22.1.jar:?]
    at org.glassfish.jersey.client.JerseyInvocation$Builder.method(JerseyInvocation.java:411) ~[jersey-client-2.22.1.jar:?]
    at org.glassfish.jersey.client.JerseyInvocation$Builder.head(JerseyInvocation.java:375) ~[jersey-client-2.22.1.jar:?]
Caused by: java.net.SocketException: Socket is closed
    at java.net.Socket.getInputStream(Unknown Source) ~[?:1.7.0_80]
    at sun.security.ssl.SSLSocketImpl.doneConnect(Unknown Source) ~[?:1.7.0_80]
    at sun.security.ssl.SSLSocketImpl.<init>(Unknown Source) ~[?:1.7.0_80]
    at sun.security.ssl.SSLSocketFactoryImpl.createSocket(Unknown Source) ~[?:1.7.0_80]
    at sun.net.www.protocol.https.HttpsClient.afterConnect(Unknown Source) ~[?:1.7.0_80]
    at sun.net.www.http.HttpClient.parseHTTP(Unknown Source) ~[?:1.7.0_80]
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source) ~[?:1.7.0_80]
    at java.net.HttpURLConnection.getResponseCode(Unknown Source) ~[?:1.7.0_80]
    at sun.net.www.protocol.https.HttpsURLConnectionImpl.getResponseCode(Unknown Source) ~[?:1.7.0_80]
    at org.glassfish.jersey.client.internal.HttpUrlConnector._apply(HttpUrlConnector.java:394) ~[jersey-client-2.22.1.jar:?]
    at org.glassfish.jersey.client.internal.HttpUrlConnector.apply(HttpUrlConnector.java:285) ~[jersey-client-2.22.1.jar:?]

client.close() is never called anywhere. The only thing being closed is the response, but there is a new response instance for each method call. Where does this exception come from and how to avoid it?

gorootde
  • 4,003
  • 4
  • 41
  • 83
  • Did you try increasing the default timeout? – Achilles Rasquinha Apr 26 '16 at 12:16
  • I thought if a timeout occurs there will be an exception stating that the connection timed out.Does the timeout influence a socket close? – gorootde Apr 26 '16 at 12:17
  • Possibly the server closed the connection. Is there any kind of authentication you missed? – Aconcagua Apr 26 '16 at 12:17
  • No there is no auth on the server side. How to find out who closed the connection? – gorootde Apr 26 '16 at 12:23
  • @k_wave jus have a look at this - http://stackoverflow.com/questions/585599/whats-causing-my-java-net-socketexception-connection-reset Hope it helps – Gandhi Apr 26 '16 at 12:29
  • Try using the [apache connector](https://jersey.java.net/documentation/latest/client.html#d0e4844) – Paul Samsotha Apr 26 '16 at 13:32
  • Unfortunately nothing in the comments helped to fix this. The only timeout I am setting is the connect timeout (5000ms). The default request timeout is 0, which means infinite. I've added the exception cause in my original post- does this help? – gorootde Apr 27 '16 at 10:15
  • 1
    "I am using Jersey-Client to check the availability of a server resource" is already the wrong question. The only reasonable way to check for the availability of a resource is to try to use it in the normal course of execution, and deal with non-availability when and if it actually arises. Any other technique introduces timing-window problems for a start, among numerous other vulnerabilites. – user207421 Apr 27 '16 at 10:39
  • This is just a snippet to show the issue (minimal working example). So theres no point in discussing the sense of it. – gorootde Apr 27 '16 at 10:43
  • Stacktrace shows it has either issue with library (try other version), or server (returning no data), or Internet – DiLDoST Sep 10 '22 at 01:52
  • @gorootde do you use an IP address in your target URL or an host name? – rmunge Sep 11 '22 at 15:50
  • There's nothing wrong with the code, I've run it successfully. So there are 2 possibilities: either the connection is closed on client side (your computer) or on server side. In order to know, just try with another URL (ex: https://google.com). If it works, then your issue is due to the server you're trying to reach (maybe doesn't allow HEAD method? try with GET?). Otherwise the issue is on client side. Do you have specific network settings? (which platform? do you go through a proxy? or running in a container?) – pismy Sep 12 '22 at 07:06
  • It could be a network problem. Like a router killing connections between your client and server. If a client is caching connections and the connections aren't used often you can end up with half-open connection after other side kills connection due to inactivity. If that is a case you can avoid this by using heartbeats or by shortening expiration time of unused connection. – Talijanac Sep 12 '22 at 17:00

1 Answers1

0

It looks like the server is closing the connection right after it receives it. Probably something is not sent in time by the client what is expected by the server, so it closes the connection, that might be some header or maybe the method is not accepted.

You can create a server socket java app or use some existing app to monitor what exactly is sent by the jersey client to that server. And you can maybe use something to look how the proper communication with the resource server you want to check should look like. Maybe there is already a real life example how to use the resource from webpage? Then you can use something like developer tools (usually F12 in browser) and look into the network tab and to see the details of the request. Otherwise you can use some spying software like fiddler or wireshark.

That's how I usually solve this kind of problems.

Krzysztof Cichocki
  • 6,294
  • 1
  • 16
  • 32