【问题标题】:Play WS standalone for 2.5.x独立播放 WS 2.5.x
【发布时间】:2016-08-02 18:31:27
【问题描述】:

我想在 Play 应用程序之外创建一个 Play 网络服务客户端。对于 Play WS 版本 2.4.x 很容易发现它是这样完成的:

val config = new NingAsyncHttpClientConfigBuilder().build()
val builder = new AsyncHttpClientConfig.Builder(config)
val client = new NingWSClient(builder.build)

但在 2.5.x 中,NingWSClient 现在已弃用 - 应使用 AhcWSClient

很遗憾,我没有找到一个完整的示例来解释在 Play 之外创建和使用 AhcWsClient。目前我喜欢这个:

import play.api.libs.ws.ahc.AhcWSClient
import akka.stream.ActorMaterializer
import akka.actor.ActorSystem

implicit val system = ActorSystem()
implicit val materializer = ActorMaterializer()
val ws = AhcWSClient()

val req = ws.url("http://example.com").get().map{
  resp => resp.body
}(system.dispatcher)

这是创建AhcWsClient 的正确方法吗?有没有办法在没有ActorSystem 的情况下创建AhcWSClient

【问题讨论】:

    标签: web-services scala playframework playframework-2.5


    【解决方案1】:

    您可能正在使用编译时依赖注入,否则您只会使用@Inject() (ws: WSClient),对吧?
    文档中有一个示例:https://www.playframework.com/documentation/2.5.x/ScalaWS#using-wsclient
    所以你可以在你的应用程序加载器中写这样的东西:

    lazy val ws = {
      import com.typesafe.config.ConfigFactory
      import play.api._
      import play.api.libs.ws._
      import play.api.libs.ws.ahc.{AhcWSClient, AhcWSClientConfig}
      import play.api.libs.ws.ahc.AhcConfigBuilder
      import org.asynchttpclient.AsyncHttpClientConfig
    
      val configuration = Configuration.reference ++ Configuration(ConfigFactory.parseString(
        """
          |ws.followRedirects = true
        """.stripMargin))
    
      val parser = new WSConfigParser(configuration, environment)
      val config = new AhcWSClientConfig(wsClientConfig = parser.parse())
      val builder = new AhcConfigBuilder(config)
      val logging = new AsyncHttpClientConfig.AdditionalChannelInitializer() {
        override def initChannel(channel: io.netty.channel.Channel): Unit = {
          channel.pipeline.addFirst("log", new io.netty.handler.logging.LoggingHandler("debug"))
        }
      }
      val ahcBuilder = builder.configure()
      ahcBuilder.setHttpAdditionalChannelInitializer(logging)
      val ahcConfig = ahcBuilder.build()
      new AhcWSClient(ahcConfig)
    }
    applicationLifecycle.addStopHook(() => Future.successful(ws.close))
    

    然后将ws 注入您的控制器。我不能 100% 确定这种方法,如果一些 Play 大师可以验证这一点,我会很高兴。
    关于ActorSystem,您只需要它来获得一个线程池来解决Future。您也可以只导入或注入默认执行上下文:
    play.api.libs.concurrent.Execution.Implicits.defaultContext.
    或者您也可以使用自己的:
    implicit val wsContext: ExecutionContext = actorSystem.dispatchers.lookup("contexts.your-special-ws-config")

    【讨论】:

    • 我没有使用依赖注入的 Web 服务,因为我想在 Play 应用程序之外使用 Web 服务。不过,文档的链接和配置解析器的扩展非常有用。
    【解决方案2】:

    AFAIK 这是创建 AhcWSClient 的正确方法 - 至少在 2.5.0 和 2.5.1 中 - 如在 Scala API 中看到的那样

    当然,你总是可以使用另一个 HTTP 客户端——Scala 有很多可用的——比如NewmanSpray client 等(虽然 Spray 也是基于 Akka,所以你必须创建一个 Actor 系统也)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-09
      • 1970-01-01
      • 2013-07-21
      • 2017-02-27
      • 1970-01-01
      • 2023-03-21
      • 1970-01-01
      • 2010-11-28
      相关资源
      最近更新 更多