泽西岛资源接收来自泽西岛客户

时间:2016-06-22 00:33:20

标签: tomcat jersey jax-rs jersey-2.0 jersey-client

我们最近在服务器和客户端上从Jersey 1.x升级到Jersey 2.22.1。我们现在看到间歇性地泽西岛将接收两个请求。

  • 断断续续地,我指的是几千个请求中的每一个。
  • 我们从未使用Jersey 1.x遇到此问题。
  • 我不清楚这是客户端还是服务器端的问题。
  • 在客户端,日志只显示单个 POST请求和响应(请参阅下面的代码段)
  • 在服务器端,日志显示两个 POST请求和响应(请参阅下面的代码段)

我能够通过在此客户端POST请求上循环数千次来重现它。每个请求都会发送一个唯一的名称'它会持久存储在服务器上。我知道我收到了一个重复的请求,当我遇到一个唯一的约束违规,试图坚持相同的名称'两次。我排除了代码的其他部分,因为日志确认Jersey正在接收两个相同的名称'

的POST请求

我已在服务器上的org.glassfish包中启用了跟踪日志记录,并在客户端上注册了LoggingFilter()。

客户端仅显示1个POST请求和响应:

Jun 21, 2016 6:02:51 PM org.glassfish.jersey.filter.LoggingFilter log
INFO: 8291 * Sending client request on thread main
8291 > POST http://localhost:9797/my-webapp/v1/data-feeds/
8291 > Accept: application/json
8291 > Content-Type: application/json

Jun 21, 2016 6:02:51 PM org.glassfish.jersey.filter.LoggingFilter log
INFO: 8291 * Client response received on thread main
8291 < 200
8291 < Cache-Control: no-cache, no-store, max-age=0, must-revalidate
8291 < Content-Length: 181
8291 < Content-Type: application/json
8291 < Date: Wed, 22 Jun 2016 00:02:51 GMT
8291 < Expires: 0
8291 < Pragma: no-cache
8291 < Server: Apache-Coyote/1.1
8291 < Set-Cookie: JSESSIONID=CFF556E7FCDB5B1F644BA04603364DFD; Path=/my-webapp/; HttpOnly
8291 < X-Content-Type-Options: nosniff
8291 < X-Frame-Options: DENY
8291 < X-XSS-Protection: 1; mode=block

服务器显示两个相同&#39;名称&#39;

的POSTS
Jun 21, 2016 6:02:51 PM org.glassfish.jersey.filter.LoggingFilter log
INFO: 8293 * Server has received a request on thread http-bio-9797-exec-21
8293 > POST http://localhost:9797/my-webapp/v1/data-feeds/
8293 > accept: application/json
8293 > authorization: Basic YWRtaW46bmltZGE=
8293 > connection: keep-alive
8293 > content-length: 181
8293 > content-type: application/json
8293 > host: localhost:9797
8293 > user-agent: Jersey/2.22.1 (HttpUrlConnection 1.8.0_31)

2016-06-21 18:02:51,964 [INFO] [c.m.c.r.r.MyResource] Received POST request /data-feeds with args [FeedData{name='pool4146'}]
Jun 21, 2016 6:02:51 PM org.glassfish.jersey.filter.LoggingFilter log
INFO: 8294 * Server has received a request on thread http-bio-9797-exec-97
8294 > POST http://localhost:9797/my-webapp/v1/data-feeds/
8294 > accept: application/json
8294 > authorization: Basic YWRtaW46bmltZGE=
8294 > connection: keep-alive
8294 > content-length: 181
8294 > content-type: application/json
8294 > host: localhost:9797
8294 > user-agent: Jersey/2.22.1 (HttpUrlConnection 1.8.0_31)

2016-06-21 18:02:51,978 [INFO] [c.m.c.r.r.MyResource] Received POST request /data-feeds with args [FeedData{name='pool4146'}]
Jun 21, 2016 6:02:51 PM org.glassfish.jersey.filter.LoggingFilter log

INFO: 8293 * Server responded with a response on thread http-bio-9797-exec-21
8293 < 200
8293 < Content-Type: application/json

Jun 21, 2016 6:02:51 PM org.glassfish.jersey.filter.LoggingFilter log
INFO: 8294 * Server responded with a response on thread http-bio-9797-exec-97
8294 < 200
8294 < Content-Type: application/json

如果此处有任何其他可能相关的配置信息,请告诉我。我们使用Tomcat 7.x和Jackson进行序列化/反序列化

3 个答案:

答案 0 :(得分:0)

我遇到了同样的问题,我无法复制这个问题。您是否尝试关闭ClientResponse流?让我知道这是如何工作的。

Client client = new Client();
WebResource resource = client.resource(restUrl);
final ClientResponse response = resource.get(ClientResponse.class);
response.close();

答案 1 :(得分:0)

这似乎是我们遇到的类似错误。我们在修复此问题时遇到了很多麻烦。

我们已在https://java.net/jira/browse/JERSEY-3254发布了此消息并附上了此修复程序。基本上它位于HttpAuthenticationFilter中并使摘要认证失效。结果往往是得到一些错误的401代码或StreamClosed Exceptions。我不会将我的解决方案复制到这篇文章中,我认为JIRA问题不会被删除。

答案 2 :(得分:0)

由于该解决方案不属于Bastian Baist回答的一部分,因此我将其发布在此处。似乎Jersey错误跟踪器已移至GitHub,因此作为参考,这是新的有效链接:https://github.com/jersey/jersey/issues/3526

要解决此问题,可以将以下内容添加到jersey/core-client/src/main/java/org/glassfish/jersey/client/authentication/HttpAuthenticationFilter.java:

static boolean repeatRequest(ClientRequestContext request, ClientResponseContext response, String newAuthorizationHeader) throws IOException {
// If the failed response has an entity stream, close it. We must do this to avoid leaking resources
// when we replace the entity stream of the failed response with that of the repeated response (see below).
// Notice that by closing the entity stream before sending the repeated request we allow resources allocated
// to the failed request (e.g., the HTTP connection) to be reused, if possible, for the repeated request.
if (response.hasEntity()) {
    response.getEntityStream().close();
    response.setEntityStream(null);
}

Client client = request.getClient();

并删除以下代码行:

 Client client = ClientBuilder.newClient(request.getConfiguration());

我很讨厌球衣维护者似乎没有解决这个问题。