1

WSClient根据播放文档创建了一个。并使用该客户端对象,我使用 WSRequest 来获取我的响应。但我得到的只是一个null正文和0服务器响应代码。当我调试到我要求的地方时,get()它会说java.lang.illegalstateexception: closed

以下是我的代码。

WS 客户端

 private WSClient wsClient() throws IOException {
   akka.stream.Materializer materializer = akka.stream.ActorMaterializer.create(akka.actor.ActorSystem.create());
    // Set up the client config (you can also use a parser here):
    scala.Option<String> noneString = scala.None$.empty();
    WSClientConfig wsClientConfig = new WSClientConfig(
            Duration.apply(120000, TimeUnit.SECONDS), // connectionTimeout
            Duration.apply(120000, TimeUnit.SECONDS), // idleTimeout
            Duration.apply(120000, TimeUnit.SECONDS), // requestTimeout
            true, // followRedirects
            true, // useProxyProperties
            noneString, // userAgent
            true, // compressionEnabled / enforced
            SSLConfigFactory.defaultConfig());

    AhcWSClientConfig clientConfig = AhcWSClientConfigFactory.forClientConfig(wsClientConfig);

    // Add underlying asynchttpclient options to WSClient
    AhcConfigBuilder builder = new AhcConfigBuilder(clientConfig);
    DefaultAsyncHttpClientConfig.Builder ahcBuilder = builder.configure();
    AsyncHttpClientConfig.AdditionalChannelInitializer logging = new AsyncHttpClientConfig.AdditionalChannelInitializer() {
        @Override
        public void initChannel(io.netty.channel.Channel channel) throws IOException {
            channel.pipeline().addFirst("log", new io.netty.handler.logging.LoggingHandler("debug"));
        }
    };
    ahcBuilder.setHttpAdditionalChannelInitializer(logging);

    WSClient customWSClient = new play.libs.ws.ahc.AhcWSClient(ahcBuilder.build(), materializer);

    customWSClient.close();
    return customWSClient;
}

请求处理程序

Future future = executorService.submit(new Runnable() {
                @Override
                public void run() {
                    WSRequest wsRequest = wsClient.url(url);

                    WSRequest complexRequest = wsRequest.setHeader(header.getKey(), header.getValue())
                            .setRequestTimeout(120000).setMethod(requestMethod);

                    CompletionStage<WSResponse> responsePromise = complexRequest.get();

                    CompletionStage<Result> promiseResult = responsePromise.thenApplyAsync(responseAfter -> {

                        int responseStatus = responseAfter.getStatus();
                        String body = responseAfter.getBody();
                        restResponse.setBody(body);
                        restResponse.setStatus(responseStatus);

                        return ok();
                    });

                }
            });
            future.get();

            executorService.shutdown();

我也ExecutorService用于异步处理。我到处寻找这个问题,但我仍然没有找到任何解决方案。

调试错误

调试错误

新的调试错误

未完成错误

4

1 回答 1

0

这里的问题是 WSClient 被关闭customWSClient.close()。在此之后,无法提出请求。

于 2016-07-20T21:39:02.130 回答