One of the great things about the Play framework is that it is fully stateless and only request/response-oriented. This is really nice since it allows me to deploy my app to the cloud and scale the number of play instances behind my load balancer without having to worry about state (session) replication...
Recently, however, I needed to execute some application logic outside of an HTTP request and found out that Play has the possibility to define Jobs which are fully managed by the framework. Sounds brilliant but it raises the question: how do these jobs fit into the stateless model that is used by Play?
Say I have a maintenance task that needs to run every hour and I define a scheduled job for that. If I then deploy multiple Play instances behind a load balancer, will that job be started at the same time on each instance? And if so, what would be a good approach to handle jobs that need to run "exclusively"?
I was thinking of creating a new play instance on a non-clustered server, re-using the JPA model of the existing (clustered) instance (and thus connecting to the same database). This new instance would contain only the maintenance jobs and since it's hosted on a non-clustered server, there is no risk of a job running simultaneously. At the same time, this would allow me to keep my existing, clustered instance completely stateless and easy to host / load balance. Would this be a good approach?
Play Framework makes it easy to build web applications with Java & Scala. Play is based on a lightweight, stateless, web-friendly architecture. Built on Akka, Play provides predictable and minimal resource consumption (CPU, memory, threads) for highly-scalable applications.
Play handles every request in an asynchronous, non-blocking way. The default configuration is tuned for asynchronous controllers. In other words, the application code should avoid blocking in controllers, i.e., having the controller code wait for an operation.
Play Framework is an open-source web application framework which follows the model–view–controller (MVC) architectural pattern. It is written in Scala and usable from other programming languages that are compiled to JVM bytecode, e.g. Java.
Play is rock-solid and used by hundreds of thousands of Java and Scala developers every month. Play is still extremely relevant to today's application and web development and has a passionate and very capable community around it ensuring that it has many good years left.
I would recommend to cluster the job too. You could set a semaphore in the database to ensure that only one job is running. Another idea is to have a look at the Akka-Framework, which will be included in Play 2.0. I think it has build in mechanism with handle this problem, but I'm not sure. I haven't experiences with akka.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With