使用PlayWS使用Java创建WSClient - Materializer null

时间:2016-07-14 09:37:15

标签: java scala playframework playframework-2.0 akka-stream

基本上,我尝试使用Java在我的SBT应用程序中创建自定义WSClient。由于我不想要任何注入依赖项,因此我使用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;
}

注意 - 我不想使用依赖注入

我该如何处理?

1 个答案:

答案 0 :(得分:1)

来自Akka Streams docs

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

请注意,play.libs.Akka.system()已弃用,将来会被删除。