有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

RESTJava。lang.illegalstateexception:使用WSRequest:Play Java关闭

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

以下是我的代码

WS-Client

 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进行异步处理。 我到处寻找这个问题,但仍然没有找到任何解决办法

调试错误

Debug Error

新调试错误

Not Completed Error


共 (1) 个答案

  1. # 1 楼答案

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