Play Framework – Integrating with Akka

Integrating with Akka in Play Framework

Akka utilizes the Actor Model to raise the reflection level and give a finer stage to manufacture right simultaneous and adaptable applications. For deficiency tolerance it receives the ‘Let it crash’ model, which has been utilized with extraordinary accomplishment as a part of the telecoms business to manufacture applications that self-recuperate – frameworks that never stop. Performers likewise give the reflection to transparent dispersion and the premise for positively adaptable and shortcoming tolerant applications.

A Play application characterizes an extraordinary actor framework to be utilized by the application. This actor framework takes after the application life-cycle and restarts consequently when the application restarts. Consequently for running jobs we can use the default Actor System gave by play. Since the Actor System is as of now accessible, we simply need to make an Actor and design it to run at defined interims.

The Application Actor Framework

Akka 2.2.0 can work with a few compartments called ActorSystems. An actor framework deals with the assets it is arranged to use to run the actors it contains.

A Play application characterizes an extraordinary actor framework to be utilized by the application. This actor framework takes after the application life-cycle and restarts consequently when the application restarts.

You can get to the default application actor framework utilizing the play.libs.akka assistant.

Design

The default actor framework design is perused from the Play application arrangement document. Case in point to arrange the default dispatcher of the application actor framework, add these lines to the conf/application.conf record:

akka.default-dispatcher.fork-join-executor.pool-size-max = 64
akka.actor.debug.receive = on

Note: You can likewise design another actor framework from the same record, simply give a top arrangement key.

Changing Over Akka Future to Play Promise

When you associate no concurrently with an Akka actor you will get a Future article. You can without much of a stretch change over a Future to a Play Promise by calling the play.libs.f.promise.wrap() technique:

import akka.actor.*;
import play.mvc.*;
import play.libs.akka;
import play.libs.F.Function;
import play.libs.F.Promise;
public class Application extends Controller {

public static Promise<result> record() {
Actorselection myactor = Akka.system().actorselection("client/myac");
return Promise.wrap(ask(myactor, "Hello", 100)).map(
new Function<object, Result>() {

public Result apply(object reaction) {
return ok(response.tostring());

}
}
);
}
}

Executing a Piece of Code in Asynchronous Manner

A typical use case inside Akka is to have some processing performed simultaneously without requiring the additional utility of an Actor. It is faster and easier way to performing calculation –

import play.libs.f.*;
import play.mvc.*;
import java.util.concurrent.callable;

public class Application extends Controller {
public static Promise<result> record() {
return promise(new Function {

public Integer apply() {
return longcomputation();
}
}).map(new Function<integer,result>() {

public Result apply(integer i) {
return ok("got " + i);
}
});
}
}

Planning offbeat assignments

You can timetable sending messages to on-screen characters and executing assignments

Case in point, to make an impression on the testactor like clockwork:

Akka.system().scheduler().schedule(
Duration.create(0, Timeunit.milliseconds),/Initial postpone 0 milliseconds
Duration.create(20, Timeunit.minutes), /Frequency 20 minutes
testactor,
"Hi",
Akka.system().dispatcher(),
invalid
);

On the other hand, to run a piece of code ten milliseconds from now:

Akka.system().scheduler().scheduleonce(
Duration.create(20, Timeunit.milliseconds),
new Runnable() {
open void run() {
file.delete();
}
},

Akka.system().dispatcher()

);

Defining Dialects upheld by your application

To define your application’s dialects, you require a substantial dialect code, detailed by a legitimate ISO Language Code, alternatively emulated by a substantial ISO Country Code.

To begin, you have to define the dialects that your application upholds in its conf document:

application.langs="en,en-Us,fr"

Notes on Punctuations

Messages utilizes java.text.messageformat, please try to remain careful that solitary quotes are utilized as for getting away parameter substitute.

info.error=you aren''t logged in!
example.formatting=when utilizing Messageformat, """{0}""" is supplanted with the first parameter.