문제

I got the example below from the following post:

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)
  }
}

The question here is which execution context would the code above use? I tried using it in my Play application. Hers is what I have configured for the default execution context:

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

How could I attach a custom execution context to the sample code above?

도움이 되었습니까?

해결책

Currently the future from akka was merged with the one from scala std library so you probably should use it instead.

http://www.scala-lang.org/api/current/#scala.concurrent.Future$ <- look at the apply function:

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

it takes a second parameter the execution context so you can create one in your scope (this will be your controller), make it implicit and then it will be used.

Take a look here: http://www.playframework.com/documentation/2.2.x/ScalaAsync for more examples.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top