java.lang.IllegalStateException: Already connected

  1. 0

    Misleading exception is raised once the Jersey client is called once the host with a non-existing, broken or unavailable url is passed in as a parameter for target method. There is no chance to establish connection to such the host but the client is reporting java.lang.IllegalStateException: Already connected instead of rather anticipated UnknownHostException or ConnectException. Example: class DigestRequest { } class DigestResponse { } public class DigestNoLimitClient { public static void main(String[] args) throws Exception { String host = "http://blabla.bla"; Client client = ClientBuilder.newClient(); Entity<DigestRequest> request = Entity.json(new DigestRequest()); Response response = client.target(host).request().post(request); DigestResponse responseObject = response.readEntity(DigestResponse.class); System.out.println(responseObject); } } Result: Exception in thread "main" javax.ws.rs.ProcessingException: Already connected at org.glassfish.jersey.client.ClientRuntime.invoke(ClientRuntime.java:255) at org.glassfish.jersey.client.JerseyInvocation$1.call(JerseyInvocation.java:667) at org.glassfish.jersey.client.JerseyInvocation$1.call(JerseyInvocation.java:664) at org.glassfish.jersey.internal.Errors.process(Errors.java:315) at org.glassfish.jersey.internal.Errors.process(Errors.java:297) at org.glassfish.jersey.internal.Errors.process(Errors.java:228) at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:424) at org.glassfish.jersey.client.JerseyInvocation.invoke(JerseyInvocation.java:664) at org.glassfish.jersey.client.JerseyInvocation$Builder.method(JerseyInvocation.java:424) at org.glassfish.jersey.client.JerseyInvocation$Builder.post(JerseyInvocation.java:333) at net.adamsmolnik.client.DigestNoLimitClient.main(DigestNoLimitClient.java:43) Caused by: java.lang.IllegalStateException: Already connected at sun.net.www.protocol.http.HttpURLConnection.setRequestProperty(HttpURLConnection.java:3000) at org.glassfish.jersey.client.HttpUrlConnector.setOutboundHeaders(HttpUrlConnector.java:348) at org.glassfish.jersey.client.HttpUrlConnector.access$100(HttpUrlConnector.java:87) at org.glassfish.jersey.client.HttpUrlConnector$3.getOutputStream(HttpUrlConnector.java:311) at org.glassfish.jersey.message.internal.CommittingOutputStream.commitStream(CommittingOutputStream.java:200) at org.glassfish.jersey.message.internal.CommittingOutputStream.commitStream(CommittingOutputStream.java:194) at org.glassfish.jersey.message.internal.CommittingOutputStream.commit(CommittingOutputStream.java:262) at org.glassfish.jersey.message.internal.OutboundMessageContext.commitStream(OutboundMessageContext.java:811) at org.glassfish.jersey.client.ClientRequest.writeEntity(ClientRequest.java:546) at org.glassfish.jersey.client.HttpUrlConnector._apply(HttpUrlConnector.java:315) at org.glassfish.jersey.client.HttpUrlConnector.apply(HttpUrlConnector.java:227) at org.glassfish.jersey.client.ClientRuntime.invoke(ClientRuntime.java:246) Compare the behavior of code listed above with that showed below: String host = "http://blabla.bla"; HttpURLConnection con = (HttpURLConnection)new URL(host).openConnection(); con.setDoOutput(true); con.getOutputStream().write(1); You get the exception as expected: Exception in thread "main" java.net.UnknownHostException: blabla.bla at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:184) at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172) at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) at java.net.Socket.connect(Socket.java:589)

    Java.net JIRA | 2 years ago | adams74
    java.lang.IllegalStateException: Already connected
  2. 0

    Misleading exception is raised once the Jersey client is called once the host with a non-existing, broken or unavailable url is passed in as a parameter for target method. There is no chance to establish connection to such the host but the client is reporting java.lang.IllegalStateException: Already connected instead of rather anticipated UnknownHostException or ConnectException. Example: class DigestRequest { } class DigestResponse { } public class DigestNoLimitClient { public static void main(String[] args) throws Exception { String host = "http://blabla.bla"; Client client = ClientBuilder.newClient(); Entity<DigestRequest> request = Entity.json(new DigestRequest()); Response response = client.target(host).request().post(request); DigestResponse responseObject = response.readEntity(DigestResponse.class); System.out.println(responseObject); } } Result: Exception in thread "main" javax.ws.rs.ProcessingException: Already connected at org.glassfish.jersey.client.ClientRuntime.invoke(ClientRuntime.java:255) at org.glassfish.jersey.client.JerseyInvocation$1.call(JerseyInvocation.java:667) at org.glassfish.jersey.client.JerseyInvocation$1.call(JerseyInvocation.java:664) at org.glassfish.jersey.internal.Errors.process(Errors.java:315) at org.glassfish.jersey.internal.Errors.process(Errors.java:297) at org.glassfish.jersey.internal.Errors.process(Errors.java:228) at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:424) at org.glassfish.jersey.client.JerseyInvocation.invoke(JerseyInvocation.java:664) at org.glassfish.jersey.client.JerseyInvocation$Builder.method(JerseyInvocation.java:424) at org.glassfish.jersey.client.JerseyInvocation$Builder.post(JerseyInvocation.java:333) at net.adamsmolnik.client.DigestNoLimitClient.main(DigestNoLimitClient.java:43) Caused by: java.lang.IllegalStateException: Already connected at sun.net.www.protocol.http.HttpURLConnection.setRequestProperty(HttpURLConnection.java:3000) at org.glassfish.jersey.client.HttpUrlConnector.setOutboundHeaders(HttpUrlConnector.java:348) at org.glassfish.jersey.client.HttpUrlConnector.access$100(HttpUrlConnector.java:87) at org.glassfish.jersey.client.HttpUrlConnector$3.getOutputStream(HttpUrlConnector.java:311) at org.glassfish.jersey.message.internal.CommittingOutputStream.commitStream(CommittingOutputStream.java:200) at org.glassfish.jersey.message.internal.CommittingOutputStream.commitStream(CommittingOutputStream.java:194) at org.glassfish.jersey.message.internal.CommittingOutputStream.commit(CommittingOutputStream.java:262) at org.glassfish.jersey.message.internal.OutboundMessageContext.commitStream(OutboundMessageContext.java:811) at org.glassfish.jersey.client.ClientRequest.writeEntity(ClientRequest.java:546) at org.glassfish.jersey.client.HttpUrlConnector._apply(HttpUrlConnector.java:315) at org.glassfish.jersey.client.HttpUrlConnector.apply(HttpUrlConnector.java:227) at org.glassfish.jersey.client.ClientRuntime.invoke(ClientRuntime.java:246) Compare the behavior of code listed above with that showed below: String host = "http://blabla.bla"; HttpURLConnection con = (HttpURLConnection)new URL(host).openConnection(); con.setDoOutput(true); con.getOutputStream().write(1); You get the exception as expected: Exception in thread "main" java.net.UnknownHostException: blabla.bla at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:184) at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172) at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) at java.net.Socket.connect(Socket.java:589)

    Java.net JIRA | 2 years ago | adams74
    java.lang.IllegalStateException: Already connected
  3. 0

    java.lang.IllegalStateException: Already connected for setRequestProperty method : Java

    Stack Overflow | 5 months ago | Harshvardhan Solanki
    java.lang.IllegalStateException: Already connected
  4. Speed up your debug routine!

    Automated exception search integrated into your IDE

  5. 0

    "Already Connected"

    GitHub | 2 years ago | vajnorcan
    java.lang.IllegalStateException: Already connected
  6. 0

    Exception in thread "main" java.lang.IllegalStateException: Already connected

    Stack Overflow | 2 years ago | user3195247
    java.lang.IllegalStateException: Already connected

    6 unregistered visitors
    Not finding the right solution?
    Take a tour to get the most out of Samebug.

    Tired of useless tips?

    Automated exception search integrated into your IDE

    Root Cause Analysis

    1. java.lang.IllegalStateException

      Already connected

      at sun.net.www.protocol.http.HttpURLConnection.setRequestProperty()
    2. Java RT
      HttpURLConnection.setRequestProperty
      1. sun.net.www.protocol.http.HttpURLConnection.setRequestProperty(HttpURLConnection.java:3013)
      1 frame