我根据Play文档创建了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
进行异步处理。
我到处寻找这个问题,我仍然没有找到任何解决方案。
调试错误
新调试错误
答案 0 :(得分:0)
这里的问题是WSClient正在以customWSClient.close()
关闭。在此之后,无法提出请求。