在Playframework中关闭WSClient时出现问题

时间:2018-12-18 22:39:04

标签: java playframework wsc

在播放框架中WSClient出现问题。

当我发送请求后,在最后的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:已关闭

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

  

debug] o.a.n.c.DefaultChannelPool-在1合0中关闭了0个连接   ms [debug] o.a.n.c.DefaultChannelPool-的条目计数:   https://api.com:443:1 [debug] o.a.n.c.DefaultChannelPool-   在0毫秒内关闭1个连接中的0个连接[调试]   o.a.n.c.DefaultChannelPool-的条目计数:   https://api.com:443:1 [debug] o.a.n.c.DefaultChannelPool-   在0毫秒内关闭1个连接中的0个连接

..因此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;
    }

}

任何人都知道这种奇怪的行为吗?

很多钱

1 个答案:

答案 0 :(得分:0)

在使用依赖注入之前,您无需在每次调用后手动关闭WSClient。 Play会在启动时处理其创建,并在关机时自动对其进行清理。订阅AhcWSModule应用即可在stop hook中完成。

仅在按照documentation中所述手动创建close时,才需要调用WSClient方法。