

§Integrating with Akka


§The application actor system



Note: Nothing prevents you from using another actor system from within a Play application. The provided default actor system is just a convenient way to start a few actors without having to set-up your own.

You can access the default application actor system using the play.libs.Akka helper:

ActorRef myActor = Akka.system().actorOf(new Props(MyActor.class));


The default actor system configuration is read from the Play application configuration file. For example to configure the default dispatcher of the application actor system, add these lines to the conf/application.conf file:


Note: You can also configure any other actor system from the same file, just provide a top configuration key.

§Converting Akka Future to Play Promise

When you interact asynchronously with an Akka actor we will get Future object. You can easily convert them to play Promise using the conversion method provided in play.libs.Akka.asPromise():

import static akka.pattern.Patterns.ask;
import play.libs.Akka;
import play.mvc.Result;
import static play.mvc.Results.async;
import play.libs.F.Function;

public static Result index() {
  return async(
    Akka.asPromise(ask(myActor,"hello", 1000)).map(
      new Function<Object,Result>() {
        public Result apply(Object response) {
          return ok(response.toString());

§Executing a block of code asynchronously


import static play.libs.Akka.future; import play.libs.F.*; import java.util.concurrent.Callable; public static Result index() { return async( future(new Callable<Integer>() { public Integer call() { return longComputation(); } }).map(new Function<Integer,Result>() { public Result apply(Integer i) { return ok("Got " + i); } }) ); }

§Scheduling asynchronous tasks

You can schedule sending messages to actors and executing tasks (functions or Runnable instances). You will get a Cancellable back that you can call cancel on to cancel the execution of the scheduled operation.


  Duration.create(0, TimeUnit.MILLISECONDS), //Initial delay 0 milliseconds
  Duration.create(30, TimeUnit.MINUTES),     //Frequency 30 minutes

Alternatively, to run a block of code ten seconds from now:

  Duration.create(10, TimeUnit.SECONDS),
  new Runnable() {
    public void run() {

Next: Internationalization