gpt4 book ai didi

java - 在 Playframework 中关闭 WSClient 时出现问题

转载 作者:太空宇宙 更新时间:2023-11-04 09:58:12 28 4
gpt4 key购买 nike

我在 playframework 中遇到 WSClient 问题。

当我在finally block 中发送关闭WSClient的请求时:

 public WSResponse sendPostRequest(String url, String bodyAsJson,  Map<String,List<String>> headers) throws Exception {
WSResponse response;
CompletionStage<WSResponse> wsResponseCompletionStage;

try {
//Headers
WSRequest request = ws.url(url);
request = mapHeaderParams(headers, request);
//sending request to api......
wsResponseCompletionStage = request.post(bodyAsJson);
request.getHeaders().put("Authorization", Arrays.asList("Basic "+ "xyz"));
response = wsResponseCompletionStage.toCompletableFuture().get();
logger.debug("Response Data from Api : " + response.getBody());
} catch (Exception e) {
logger.error("Error Posting Data to Api : " + e.getMessage());
ws.close();
throw e;
}finally {
ws.close();
}
return response;
}

当我想发送下一个请求时,我总是收到错误:

java.lang.IllegalStateException: Closed

..当我不关闭我的 ws 客户端时,它会像这样不断地登录我的 application.logs 并且根本不停止它:

debug] o.a.n.c.DefaultChannelPool - Closed 0 connections out of 1 in 0 ms [debug] o.a.n.c.DefaultChannelPool - Entry count for : https://api.com:443 : 1 [debug] o.a.n.c.DefaultChannelPool - Closed 0 connections out of 1 in 0 ms [debug] o.a.n.c.DefaultChannelPool - Entry count for : https://api.com:443 : 1 [debug] o.a.n.c.DefaultChannelPool - Closed 0 connections out of 1 in 0 ms

..所以 WSClient 永远不会关闭!

这是我的 WebClient 类:

@Singleton
public class ApiRequestClient{

@Inject
private WSClient ws;

final Logger.ALogger logger = Logger.of(this.getClass());

@Inject
public ApiRequestClient(WSClient ws) {
this.ws = ws;
}


public WSRequest mapHeaderParams(Map<String, List<String>> headers, WSRequest request) {
//not working !!!! ....
//request.getHeaders().putAll(headersa);
//thats why we do ......
Set keySet = headers.keySet();
for(Object key : keySet){
request.setHeader(key.toString(), headers.get(key).get(0));
}
return request;
}


public WSResponse sendPostRequest(String url, String bodyAsJson, Map<String,List<String>> headers) throws Exception {
WSResponse response;
CompletionStage<WSResponse> wsResponseCompletionStage;

try {
//Headers
WSRequest request = ws.url(url);
request = mapHeaderParams(headers, request);
//sending request to api......
wsResponseCompletionStage = request.post(bodyAsJson);
//FIXME !!!!
request.getHeaders().put("Authorization", Arrays.asList("Basic "+ "xyz"));
response = wsResponseCompletionStage.toCompletableFuture().get();
logger.debug("Response Data from Api : " + response.getBody());
} catch (Exception e) {
logger.error("Error Posting Data to Api : " + e.getMessage());
ws.close();
throw e;
}finally {
ws.close();
}
return response;
}

public static Map<String, String> queryStringToParameterMap(Map<String, String[]> queryString) {
Map<String, String> params = new HashMap<String, String>();
Set<String> keys = queryString.keySet();
for (String key : keys) {
params.put(key, queryString.get(key)[0]);
}
return params;
}

}

有人知道这种奇怪的行为吗?

非常感谢

最佳答案

在使用依赖项注入(inject)之前,您无需在每次调用后手动关闭 WSClient。 Play 将在启动时处理它的创建并在关闭时自动清理它。这是在 AhcWSModule 中完成的通过订阅申请stop hook .

只有在手动创建 WSClient 时才需要调用 close 方法,如 documentation 中所述。 .

关于java - 在 Playframework 中关闭 WSClient 时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53842159/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com