[英]Resteasy Client keeping connection allocated after a method throws an Exception
我目前正在使用TJWSEmbeddedJaxrsServer來幫助我進行RESTful API測試(由Resteasy創建),並且運行良好。 但是,當任何被調用的方法引發異常時,都會發生問題:Reasteasy客戶端“丟失”並仍然保持連接,不允許其他測試方法調用RESTful服務。 即使您實例化了可以解開異常並在嵌入式服務器中使用它的提供程序,也會發生這種情況。
有人可以幫我嗎?
為了模擬問題,實際上很簡單:
公共類InMemoryRestTest {
@Path("/myresource")
public static class MyResource {
@POST
@Consumes(MediaType.TEXT_PLAIN)
@Produces(MediaType.APPLICATION_XML)
public MyModel createMyModel(int number) throws Exception {
// supose this is a Business exception
throw new Exception("Test");
}
}
public static MyResource sut = new MyResource();
public static InMemoryRestServer server;
@BeforeClass
public static void beforeClass() throws Exception {
server = InMemoryRestServer.create(sut);
}
@AfterClass
public static void afterClass() throws Exception {
server.close();
}
@Test
public void postSimpleBody() throws Exception {
// This one throws the "Exception" exception and passes
Response response = server.newRequest("/myresource").request().buildPost(Entity.text("42")).invoke();
assertEquals(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), response.getStatus());
}
@Test
public void postAnother() throws Exception {
// This one fails with "Invalid use of BasicClientConnManager: connection still allocated."
Response response = server.newRequest("/myresource").request().buildPost(Entity.text("20")).invoke();
assertEquals(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), response.getStatus());
}
}
瞧! 運行針對“ postAnother()”的測試時,將發生以下錯誤:
javax.ws.rs.ProcessingException: Unable to invoke request
at org.jboss.resteasy.client.jaxrs.engines.ApacheHttpClient4Engine.invoke(ApacheHttpClient4Engine.java:287)
at org.jboss.resteasy.client.jaxrs.internal.ClientInvocation.invoke(ClientInvocation.java:407)
at biz.paluch.rest.test.InMemoryRestTest.postSimpleBody(InMemoryRestTest.java:51)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Caused by: java.lang.IllegalStateException: Invalid use of BasicClientConnManager: connection still allocated.
Make sure to release the connection before allocating another one.
at org.apache.http.impl.conn.BasicClientConnectionManager.getConnection(BasicClientConnectionManager.java:162)
at org.apache.http.impl.conn.BasicClientConnectionManager$1.getConnection(BasicClientConnectionManager.java:139)
at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:456)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:906)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:805)
at org.jboss.resteasy.client.jaxrs.engines.ApacheHttpClient4Engine.invoke(ApacheHttpClient4Engine.java:283)
... 27 more
默認的ResteasyClientBuilder不使用連接池,這意味着一次只能有一個並行連接。 因此,您必須確保關閉響應(即response.close()),以釋放(僅)連接。
在此處查看響應實現: https : //github.com/resteasy/Resteasy/blob/master/resteasy-client/src/main/java/org/jboss/resteasy/client/jaxrs/internal/ClientResponse.java
public void close()
{
if (isClosed()) return;
try {
isClosed = true;
releaseConnection(); // <= HERE!
}
catch (Exception e) {
throw new ProcessingException(e);
}
}
因此,以下方法可以解決您的問題:
@Test
public void postBody() throws Exception {
Response response = null;
try {
response = server.newRequest("...").request().buildPost(Entity.text("...")).invoke();
} finally {
response.close();
}
}
問候,
伯納德。
您需要配置ResteasyClientBuilder以使用連接池。 通過以下方式修改InMemoryRestServer類的withDefaults()方法
//this.resteasyClient = new ResteasyrestEasyClientBuilder().build();
ResteasyrestEasyClientBuilder restEasyClientBuilder = new ResteasyrestEasyClientBuilder();
restEasyClientBuilder = restEasyClientBuilder.connectionPoolSize(20);
this.resteasyClient = restEasyClientBuilder.build();
這樣,您的兩個測試用例都應無例外地運行(盡管兩者似乎都拋出AssertionError)
在Java SE 7及更高版本中,更干凈的方法是對資源使用try。 樣本片段如下:
try(response = server.newRequest("...").request().buildPost(Entity.text("...")).invoke())
{
/* response handling logic */
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.