【问题标题】:How to configure JAX-RS Client in order to make calls concurrently如何配置 JAX-RS 客户端以同时进行调用
【发布时间】:2018-09-21 03:32:51
【问题描述】:

我的 webapp 是通过 http 访问的,并自己进行 http 调用。为此,我使用 jaxrs-client。由于Client 被认为是一种昂贵的资源,因此它会被初始化一次并在请求中重用。

Client client = ClientBuilder.newClient();
WebTarget webTarget = client.target(baseUri).path(...);

在请求期间,http-call 是这样进行的:

Builder request = webTarget.request(MediaType.APPLICATION_JSON);
Response response = request.post(...);
try {
    // evaluate response
}
finally {
    response.close();
}

所以一切正常,只要 webapp 部署在 TomEE 上或没有发生并行性。 但是当代码在 Wildfly 中同时执行时,它会失败并显示

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.util.Asserts.check(Asserts.java:34)
  at org.apache.http.impl.conn.BasicClientConnectionManager.getConnection(BasicClientConnectionManager.java:162)
  at org.apache.http.impl.conn.BasicClientConnectionManager$1.getConnection(BasicClientConnectionManager.java:144)
  at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:423)
  at org.apache.http.impl.client.AbstractHttpClient.doExecute(AbstractHttpClient.java:882)
  at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:82)

错误是由 Wildfly 生成的,显然是使用 apache-httpclient 作为其 JAXRS 实现提供程序的一部分。

询问 google,有人会提示建议设置 REST-EASY 或 apache-httpclient(池大小或特定 HttpClientConnectionManager)。 但是我的应用程序不依赖于其中之一。它仅取决于javax:javaee-api:7.0

我的问题:是否有独立于供应商的方式来配置 javax.ws.rs.client.Client 能够同时拨打电话?

【问题讨论】:

    标签: java http jax-rs wildfly


    【解决方案1】:

    对您的问题的简短回答是……不。这是长答案:

    您的代码直接依赖于 javaee-api 中定义的 jax-rs api(接口)。您将代码部署到 wildfly,其运行时提供 Rest-Easy 作为 JAX-RS 的特定实现。 Wildfly 的依赖管理/类加载负责加载 Rest-Easy 的 jax-rs api 的具体实现。

    Rest-Easy 使用 apache http 客户端(apache-http 客户端不是 jax-rs api 的实现,Rest Easy 是)作为底层 http 客户端。如果没有配置,apache-http 客户端将默认使用 BasicClientConnManager 运行。要处理并发请求,您需要显式配置多线程连接管理器 - 有关详细信息,请参阅 apache http-client 文档http-client documentation - 或者如果您需要示例,请告诉我。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-04
      • 1970-01-01
      • 2016-12-06
      相关资源
      最近更新 更多