java.lang.IllegalStateException: Already connected

Java.net JIRA | adams74 | 2 years ago
  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. Speed up your debug routine!

    Automated exception search integrated into your IDE

  4. 0

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

    Stack Overflow | 5 months ago | Harshvardhan Solanki
    java.lang.IllegalStateException: Already connected
  5. 0

    "Already Connected"

    GitHub | 2 years ago | vajnorcan
    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)[na:1.8.0_60]
      1 frame
    3. jersey-core-client
      HttpUrlConnector$4.getOutputStream
      1. org.glassfish.jersey.client.internal.HttpUrlConnector.setOutboundHeaders(HttpUrlConnector.java:421)[jersey-client-2.22.1.jar:na]
      2. org.glassfish.jersey.client.internal.HttpUrlConnector.access$100(HttpUrlConnector.java:96)[jersey-client-2.22.1.jar:na]
      3. org.glassfish.jersey.client.internal.HttpUrlConnector$4.getOutputStream(HttpUrlConnector.java:384)[jersey-client-2.22.1.jar:na]
      3 frames
    4. Jersey Core
      OutboundMessageContext.commitStream
      1. org.glassfish.jersey.message.internal.CommittingOutputStream.commitStream(CommittingOutputStream.java:200)[jersey-common-2.22.1.jar:na]
      2. org.glassfish.jersey.message.internal.CommittingOutputStream.commitStream(CommittingOutputStream.java:194)[jersey-common-2.22.1.jar:na]
      3. org.glassfish.jersey.message.internal.CommittingOutputStream.commit(CommittingOutputStream.java:262)[jersey-common-2.22.1.jar:na]
      4. org.glassfish.jersey.message.internal.OutboundMessageContext.commitStream(OutboundMessageContext.java:816)[jersey-common-2.22.1.jar:na]
      4 frames
    5. jersey-core-client
      JerseyInvocation$2.call
      1. org.glassfish.jersey.client.ClientRequest.writeEntity(ClientRequest.java:545)[jersey-client-2.22.1.jar:na]
      2. org.glassfish.jersey.client.internal.HttpUrlConnector._apply(HttpUrlConnector.java:388)[jersey-client-2.22.1.jar:na]
      3. org.glassfish.jersey.client.internal.HttpUrlConnector.apply(HttpUrlConnector.java:285)[jersey-client-2.22.1.jar:na]
      4. org.glassfish.jersey.client.ClientRuntime.invoke(ClientRuntime.java:255)[jersey-client-2.22.1.jar:na]
      5. org.glassfish.jersey.client.JerseyInvocation$2.call(JerseyInvocation.java:700)[jersey-client-2.22.1.jar:na]
      5 frames
    6. Jersey Core
      RequestScope.runInScope
      1. org.glassfish.jersey.internal.Errors.process(Errors.java:315)[jersey-common-2.22.1.jar:na]
      2. org.glassfish.jersey.internal.Errors.process(Errors.java:297)[jersey-common-2.22.1.jar:na]
      3. org.glassfish.jersey.internal.Errors.process(Errors.java:228)[jersey-common-2.22.1.jar:na]
      4. org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:444)[jersey-common-2.22.1.jar:na]
      4 frames
    7. jersey-core-client
      JerseyInvocation$Builder.post
      1. org.glassfish.jersey.client.JerseyInvocation.invoke(JerseyInvocation.java:696)[jersey-client-2.22.1.jar:na]
      2. org.glassfish.jersey.client.JerseyInvocation$Builder.method(JerseyInvocation.java:448)[jersey-client-2.22.1.jar:na]
      3. org.glassfish.jersey.client.JerseyInvocation$Builder.post(JerseyInvocation.java:349)[jersey-client-2.22.1.jar:na]
      3 frames