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
  • 3,885
  • 3
  • 34
  • 77
  • 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
  • "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

0 Answers0