0

我从以下帖子中获得了以下示例:

http://blog.tksfz.org/2012/10/12/websockets-echo-using-play-scala-and-actors-part-i/

import play.api.libs.concurrent.Akka
import play.api.Play.current // needed by Akka.future

def simpleAsyncWebSocket = WebSocket.async[String] {
  Akka.future {
    val out = Enumerator.imperative[String]()
    val in = Iteratee.foreach[String] {
      msg =>
        out.push(msg)
    }
    (in, out)
  }
}

这里的问题是上面的代码将使用哪个执行上下文?我尝试在我的 Play 应用程序中使用它。Hers 是我为默认执行上下文配置的:

play {
  akka {
    event-handlers = ["akka.event.slf4j.Slf4jEventHandler"]
    loglevel = WARNING
    actor {
      default-dispatcher = {
        fork-join-executor {
          parallelism-factor = 3.0
          parallelism-max = 256
        }
      }
    }
  }
}

如何将自定义执行上下文附加到上面的示例代码?

4

1 回答 1

2

目前,来自 akka 的 future 已与来自 scala std 库的 future 合并,因此您可能应该改用它。

http://www.scala-lang.org/api/current/#scala.concurrent.Future$<- 查看应用函数:

def apply[T](body: ⇒ T)(implicit execctx: ExecutionContext): Future[T]

它需要第二个参数执行上下文,因此您可以在您的范围内创建一个(这将是您的控制器),使其隐含然后将被使用。

看看这里: http://www.playframework.com/documentation/2.2.x/ScalaAsync更多示例。

于 2014-01-27T08:57:03.420 回答