1

I have created a WSClient according to the Play Documentation. And using that client object I use WSRequest to get my response. But all I'm getting is a null body and 0 as the server response code. And when I debug to where i request get() it says java.lang.illegalstateexception: closed.

Following is my code.

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;
}

Request Handler

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();

I also use ExecutorService for asynchronous handling. I searched everywhere for this problem and I still haven't found any solution for it.

Error in Debug

Debug Error

New Debug Error

Not Completed Error

Akila Randil
  • 205
  • 1
  • 10

1 Answers1

0

The problem here is that the WSClient is being closed with customWSClient.close(). After this it is not possible to make requests.

Salem
  • 12,808
  • 4
  • 34
  • 54
  • I fixed it but now i am getting another error. It says scala.concurrent.java8.FuturesConvertersImpl$CF@7c2312fa[Not completed]. I have edited my question with the new debug. – Akila Randil Jul 25 '16 at 09:04