Question

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?

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top