1

我在 playframework 中遇到了 WSClient 的问题。

当我在 finally 块之后发送关闭 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:关闭

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

调试] oancDefaultChannelPool - 在 0 毫秒内关闭 0 个连接,共 1 个 [调试] oancDefaultChannelPool - 条目计数: https ://api.com:443:1 [调试] oancDefaultChannelPool - 在 0 毫秒内关闭 0 个连接,共 1 个 [调试] oancDefaultChannelPool - 条目计数: https ://api.com:443 : 1 [debug] oancDefaultChannelPool - 在 0 毫秒内关闭 0 个连接,共 1 个

.. 所以 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;
    }

}

有谁知道这种奇怪的行为??

多谢

4

1 回答 1

0

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

仅在文档中描述的手动close创建时才需要调用方法。WSClient

于 2018-12-19T09:27:27.280 回答