I've read a few tutorials on how to deal with concurrency in Play and found some examples:
Asynchronous Job
import scala.concurrent.{ExecutionContext, future}
def sendEmailAsync(from: String, to: String, subject: String, body: String) = {
import ExecutionContext.Implicits.global // in scala.concurrent
future {
EmailHelper.sendEmail(from, to, subject, body)
}
}
Scheduled Job
import play.api.libs.concurrent.{Akka, Execution}
def sendEmailOnSchedule(from: String, to: String, subject: String, body: String) = {
import scala.concurrent.duration._
import Execution.Implicits.defaultContext // in play.api.libs.concurrent
Akka.system.scheduler.scheduleOnce(10 seconds) {
EmailHelper.sendEmail(from, to, subject, body)
}
}
Well, I'm a bit confused... the first example uses scala.concurrent.ExecutionContext.Implicits.global
while the second example uses play.api.libs.concurrent.Execution.Implicits.defaultContext
. Why? Could someone explain me what is going on behind the scene?
ExecutingContext
was something like java's ExecutorService(Thread Pool), you can even create it yourself if you like. For example, play-slick module use a separated context to execution db operations. github.com/freekh/play-slick/blob/master/src/main/scala/play/… – Lawlor