我刚刚尝试了关于AsyncResponse
的泽西教程的示例,可以在这里找到:http://jersey.java.net/apidocs/snapshot/jersey/javax/ws/rs/container/AsyncResponse.html
它似乎不起作用。甚至该示例在简单的单元测试中执行时抛出异常:
public class AsyncTest extends JerseyTest {
@Override
protected Application configure() {
return new ResourceConfig(MyAsync.class)
}
@Path("/async")
public static class MyAsync {
@GET
public void asyncGet(@Suspended final AsyncResponse asyncResponse) {
new Thread(new Runnable() {
@Override
public void run() {
String result = veryExpensiveOperation();
asyncResponse.resume(result);
}
private String veryExpensiveOperation() {
return "bla";
}
}).start();
}
}
@Test
public void testConvertWordToPdf() throws Exception {
String result = target().path("async").request().async().get().get(String.class);
}
}
有人可以告诉我我做错了什么吗?谢谢你的回答!
更新:我甚至尝试过可以在http://search.maven.org/#artifactdetails|org.glassfish.jersey.examples|server-async-standalone-webapp|2.1|war找到的官方示例,但我得到了相同的例外情况。如果我确保AsyncResponse
在方法返回之前响应,该示例有效。我得到的堆栈跟踪:
java.util.concurrent.ExecutionException: javax.ws.rs.ProcessingException:java.lang.NullPointerException at com.google.common.util.concurrent.AbstractFuture $ Sync.getValue(AbstractFuture.java:306) 在 com.google.common.util.concurrent.AbstractFuture $ Sync.get(AbstractFuture.java:293) 在 com.google.common.util.concurrent.AbstractFuture.get(AbstractFuture.java:116) at MyCompany.jersey.ConverterResourceTest.testTest(MyClass.java:20) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) 在 sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) 在java.lang.reflect.Method.invoke(Method.java:597)at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:84) 在org.testng.internal.Invoker.invokeMethod(Invoker.java:714)at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:901)at at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1231)at at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127) 在 org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111) 在org.testng.TestRunner.privateRun(TestRunner.java:767)at org.testng.TestRunner.run(TestRunner.java:617)at org.testng.SuiteRunner.runTest(SuiteRunner.java:334)at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329)at at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)at at org.testng.SuiteRunner.run(SuiteRunner.java:240)at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)at org.testng.TestNG.runSuitesSequentially(TestNG.java:1224)at at org.testng.TestNG.runSuitesLocally(TestNG.java:1149)at at org.testng.TestNG.run(TestNG.java:1057)at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:111)at at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204)at at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175)at at org.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:111) 引起:javax.ws.rs.ProcessingException: java.lang.NullPointerException at org.glassfish.jersey.client.ClientRuntime $ 1 $ 1.failure(ClientRuntime.java:148) 在 org.glassfish.jersey.test.inmemory.internal.InMemoryConnector $ 3.run(InMemoryConnector.java:265) 在 java.util.concurrent.Executors $ RunnableAdapter.call(Executors.java:439) at java.util.concurrent.FutureTask $ Sync.innerRun(FutureTask.java:303) 在java.util.concurrent.FutureTask.run(FutureTask.java:138)at com.google.common.util.concurrent.MoreExecutors $ SameThreadExecutorService.execute(MoreExecutors.java:293) 在 com.google.common.util.concurrent.AbstractListeningExecutorService.submit(AbstractListeningExecutorService.java:49) 在 org.glassfish.jersey.test.inmemory.internal.InMemoryConnector.apply(InMemoryConnector.java:257) 在 org.glassfish.jersey.client.ClientRuntime $ 1.run(ClientRuntime.java:156) 在org.glassfish.jersey.internal.Errors $ 1.call(Errors.java:271)at at org.glassfish.jersey.internal.Errors $ 1.call(Errors.java:267)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:267)at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:322) 在 org.glassfish.jersey.client.ClientRuntime $ 2.run(ClientRuntime.java:170) 在 java.util.concurrent.Executors $ RunnableAdapter.call(Executors.java:439) at java.util.concurrent.FutureTask $ Sync.innerRun(FutureTask.java:303) 在java.util.concurrent.FutureTask.run(FutureTask.java:138)at java.util.concurrent.ThreadPoolExecutor中的$ Worker.runTask(ThreadPoolExecutor.java:895) 在 java.util.concurrent.ThreadPoolExecutor中的$ Worker.run(ThreadPoolExecutor.java:918) 在java.lang.Thread.run(Thread.java:662)引起: java.lang.NullPointerException at org.glassfish.jersey.internal.util.KeyComparatorHashMap.putAll(KeyComparatorHashMap.java:509) 在 javax.ws.rs.core.AbstractMultivaluedMap.putAll(AbstractMultivaluedMap.java:332) 在 org.glassfish.jersey.test.inmemory.internal.InMemoryConnector.createClientResponse(InMemoryConnector.java:286) 在 org.glassfish.jersey.test.inmemory.internal.InMemoryConnector.apply(InMemoryConnector.java:247) 在 org.glassfish.jersey.test.inmemory.internal.InMemoryConnector $ 3.run(InMemoryConnector.java:261) ......还有20个
答案 0 :(得分:1)
顺便说一句,我在Grizzly容器中测试了这个测试用例。 InMemory容器不支持它,因为容器未在ContainerResponseWriter上实现挂起操作。 InMemoryResponseWriter应该抛出UnsupportedOperationException而不是返回false。这听起来像是一个泽西岛的虫子。
@Test
public void testConvertWordToPdf() throws Exception {
String result = target().path("async").request().async().get().get().readEntity(String.class);
assertEquals("bla", result);
}
这是我的POM依赖:
<dependency>
<groupId>org.glassfish.jersey.test-framework.providers</groupId>
<artifactId>jersey-test-framework-provider-grizzly2</artifactId>
<version>2.1</version>
<scope>test</scope>
</dependency>