在Jersey 2中处理客户端超时

时间:2016-05-23 15:56:12

标签: jersey-2.0 jersey-client jersey-test-framework

我有一个泽西资源调用另一个服务。我试图控制该调用的超时,但事情似乎正在起作用,除非我在控制台中得到一个有点令人不安的错误堆栈。我很担心,尽管事情似乎在起作用。 这是我的代码:

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));
}
}

错误:

qtp464649704{STOPPING,8<=1<=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

0 个答案:

没有答案
相关问题