gpt4 book ai didi

java - 使用 PlayWS 通过 Java 创建 WSClient - Materializer null

转载 作者:行者123 更新时间:2023-12-01 09:42:06 26 4
gpt4 key购买 nike

基本上,我正在尝试使用 Java 在我的 SBT 应用程序中创建自定义 WSClient。由于我不需要任何注入(inject)依赖项,因此我使用 documentation 中显示的方法。 。要创建自定义 WSClient,它需要一个 Akka.stream.Materializer 对象,但由于它是抽象的,我无法创建 Materializer 对象。

private Materializer materializer;


private WSClient wsClient() throws IOException {

scala.Option<String> noneString = scala.None$.empty();
WSClientConfig wsClientConfig = new WSClientConfig(
Duration.apply(120, TimeUnit.SECONDS), // connectionTimeout
Duration.apply(120, TimeUnit.SECONDS), // idleTimeout
Duration.apply(120, 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 Exception {

}
};
ahcBuilder.setHttpAdditionalChannelInitializer(logging);
// #ws-custom-client


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

// #ws-close-client
customWSClient.close();
return customWSClient;
}

注意 - 我不想使用依赖注入(inject)

我应该如何继续?

最佳答案

来自Akka Streams docs :

akka.actor.ActorSystem system = play.libs.Akka.system();
akka.stream.Materializer materializer = akka.stream.ActorMaterializer.create(system);

请记住,play.libs.Akka.system() 已弃用,并将在将来删除。

关于java - 使用 PlayWS 通过 Java 创建 WSClient - Materializer null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38370701/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com