title: Handling Client Timeout in Jersey 2
body: <p>I have a Jersey Resource which calls another service. I'm trying to control the timeout on that call and things seem to be working except I get a somewhat disturbing error stack in the console. I am worried by it despite that things appear to otherwise be working. 
  Here is my code:</p>
  
  <pre><code>public class SORepro extends JerseyTest {
  @Path("/")
  public static class Resource {
      Client client = null;
  
      public Resource() {
          client = ClientBuilder.newClient();
          int timeoutInMillis = 2 * 1000;
          client.property(ClientProperties.CONNECT_TIMEOUT, timeoutInMillis);
          client.property(ClientProperties.READ_TIMEOUT, timeoutInMillis);
      }
  
      @Context
      UriInfo uriInfo;
  
      @GET
      @Path("/get")
      @Produces("application/json")
      public String get() {
          URI uri = uriInfo.getBaseUriBuilder().path(Resource.class).build();
          Invocation.Builder request = client.target(uri).path("/otherResource/timeout").request()
                  .accept(MediaType.APPLICATION_JSON_TYPE)
                  .header("id", 123);
          try {
              Response resp = request.get();
          } catch (ProcessingException pe) {
              return "Timeout";
          }
          return "";
      }
  }
  
  @Path("/otherResource")
  public static class Resource2 {
      @GET
      @Path("/timeout")
      @Produces("application/json")
      public void getSth() {
          try {
              Thread.sleep(6000);
          } catch (InterruptedException e) {
          }
      }
  
  }
  
  
  @Override
  protected Application configure() {
      return new ResourceConfig(Resource.class)
              .register(Resource2.class);
  }
  
  @Test
  public void testClientTimeout() throws Exception {
      Invocation.Builder request = target().path("/get").request()
              .accept(MediaType.APPLICATION_JSON_TYPE);
      Response response = request.get();
      assertTrue("Status " + response.getStatus(), response.getStatus() == 200);
      System.out.println("Response entity" + response.readEntity(String.class));
  }
  }
  </code></pre>
  
  <p>And the error:</p>
  
  <pre><code>qtp464649704{STOPPING,8&lt;=1&lt;=200,i=0,q=0} rejected org.eclipse.jetty.server.HttpConnection$CommitCallback$1@21cf240e
  /otherResource/timeout
  java.lang.RuntimeException: A MultiException has 1 exceptions.  They are:
  1. java.lang.IllegalStateException:     ServiceLocatorImpl(__HK2_Generated_0,1,652433136) has been shut down
  
  at org.glassfish.jersey.jetty.JettyHttpContainer.handle(JettyHttpContainer.java:197) ~[jersey-container-jetty-http-2.22.1.jar:?]
  at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:97) ~[jetty-server-9.1.1.v20140108.jar:9.1.1.v20140108]
  at org.eclipse.jetty.server.Server.handle(Server.java:459) ~[jetty-server-9.1.1.v20140108.jar:9.1.1.v20140108]
  at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:281) [jetty-server-9.1.1.v20140108.jar:9.1.1.v20140108]
  at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:232) [jetty-server-9.1.1.v20140108.jar:9.1.1.v20140108]
  at org.eclipse.jetty.io.AbstractConnection$1.run(AbstractConnection.java:505) [jetty-io-9.1.1.v20140108.jar:9.1.1.v20140108]
  at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:607) [jetty-util-9.1.1.v20140108.jar:9.1.1.v20140108]
  at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:536) [jetty-util-9.1.1.v20140108.jar:9.1.1.v20140108]
  at java.lang.Thread.run(Thread.java:745) [?:1.8.0_66]
  Caused by: org.glassfish.hk2.api.MultiException: A MultiException has 1 exceptions.  They are:
  1. java.lang.IllegalStateException: ServiceLocatorImpl(__HK2_Generated_0,1,652433136) has been shut down
  
  at org.jvnet.hk2.internal.FactoryCreator.getFactoryHandle(FactoryCreator.java:105) ~[hk2-locator-2.4.0-b25.jar:?]
  at org.jvnet.hk2.internal.FactoryCreator.dispose(FactoryCreator.java:172) ~[hk2-locator-2.4.0-b25.jar:?]
  at org.jvnet.hk2.internal.SystemDescriptor.dispose(SystemDescriptor.java:525) ~[hk2-locator-2.4.0-b25.jar:?]
  at org.glassfish.jersey.process.internal.RequestScope$Instance.remove(RequestScope.java:532) ~[jersey-common-2.19.jar:?]
  at org.glassfish.jersey.process.internal.RequestScope$Instance.release(RequestScope.java:549) ~[jersey-common-2.19.jar:?]
  at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:319) ~[jersey-common-2.19.jar:?]
  at org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:291) ~[jersey-server-2.19.jar:?]
  at org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:1140) ~[jersey-server-2.19.jar:?]
  at org.glassfish.jersey.jetty.JettyHttpContainer.handle(JettyHttpContainer.java:195) ~[jersey-container-jetty-http-2.22.1.jar:?]
  ... 8 more
  Caused by: java.lang.IllegalStateException: ServiceLocatorImpl(__HK2_Generated_0,1,652433136) has been shut down
  at org.jvnet.hk2.internal.ServiceLocatorImpl.checkState(ServiceLocatorImpl.java:2264) ~[hk2-locator-2.4.0-b25.jar:?]
  at org.jvnet.hk2.internal.ServiceLocatorImpl.getServiceHandleImpl(ServiceLocatorImpl.java:635) ~[hk2-locator-2.4.0-b25.jar:?]
  at org.jvnet.hk2.internal.ServiceLocatorImpl.getServiceHandle(ServiceLocatorImpl.java:628) ~[hk2-locator-2.4.0-b25.jar:?]
  at org.jvnet.hk2.internal.ServiceLocatorImpl.getServiceHandle(ServiceLocatorImpl.java:646) ~[hk2-locator-2.4.0-b25.jar:?]
  at org.jvnet.hk2.internal.FactoryCreator.getFactoryHandle(FactoryCreator.java:102) ~[hk2-locator-2.4.0-b25.jar:?]
  at org.jvnet.hk2.internal.FactoryCreator.dispose(FactoryCreator.java:172) ~[hk2-locator-2.4.0-b25.jar:?]
  at org.jvnet.hk2.internal.SystemDescriptor.dispose(SystemDescriptor.java:525) ~[hk2-locator-2.4.0-b25.jar:?]
  at org.glassfish.jersey.process.internal.RequestScope$Instance.remove(RequestScope.java:532) ~[jersey-common-2.19.jar:?]
  at org.glassfish.jersey.process.internal.RequestScope$Instance.release(RequestScope.java:549) ~[jersey-common-2.19.jar:?]
  at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:319) ~[jersey-common-2.19.jar:?]
  at org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:291) ~[jersey-server-2.19.jar:?]
  at org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:1140) ~[jersey-server-2.19.jar:?]
  at org.glassfish.jersey.jetty.JettyHttpContainer.handle(JettyHttpContainer.java:195) ~[jersey-container-jetty-http-2.22.1.jar:?]
  ... 8 more
  Could not send response error 500: java.lang.RuntimeException: A MultiException has 1 exceptions.  They are:
  1. java.lang.IllegalStateException: ServiceLocatorImpl(__HK2_Generated_0,1,652433136) has been shut down
  </code></pre>