Jersey资源从Jersey客户端接收重复请求

问题描述 投票:5回答:3

我们最近在服务器和客户端上从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

服务器显示两个相同“名称”的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进行序列化/反序列化

tomcat jersey jax-rs jersey-2.0 jersey-client
3个回答
0
投票

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

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

0
投票

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

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


0
投票

由于解决方案不是Bastian Baist的答案的一部分,我将在此处发布。似乎Jersey bug跟踪器被移动到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());

令人讨厌的是,球衣维护者似乎没有解决这个问题。

最新问题
© www.soinside.com 2019 - 2024. All rights reserved.