Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

play framework 2.1 - scheduling async tasks

In play's 2.0.x doc you can see how to schedule asynchronous tasks:

http://www.playframework.org/documentation/2.0.4/ScalaAkka

Akka.system.scheduler.schedule(0 seconds, 30 minutes, testActor, "tick")

How can you achieve the same thing withthe recently releades Play 2.1???

The whole akka API seems to have changed...

I've checked:

https://github.com/playframework/Play20/wiki/Highlights https://github.com/playframework/Play20/wiki/Migration and also http://doc.akka.io/docs/akka/2.1.0-RC1/project/migration-guide-2.0.x-2.1.x.html

also asked here: https://groups.google.com/d/topic/play-framework/7VcwNea6QlM/discussion

like image 344
opensas Avatar asked Nov 20 '12 03:11

opensas


People also ask

How to implement scheduling and asynchronous tasks in Spring Framework?

Both can be activated by adding @EnableScheduling and @EnableAsync annotations to configuration (annotated with @Configuration) class. If we've it, we can start to implement scheduling and asynchronous tasks. To implement the first category of tasks, we can use @Scheduled annotation. It's placed in org.springframework.scheduling.annotation package.

What is the difference between @ asynchronous and @scheduled tasks?

Annotation that marks a method or a class (by marking a class, we mark automatically all its methods) as asynchronous, is @Async. Unlike @Scheduled, asynchronous tasks accept parameters and may return something. With this knowledge, we can pass to write asynchronous and scheduled tasks. All mandatory comments will be placed inside test case.

How do I create an asynchronous method in the Windows Runtime?

By using those two keywords, you can use resources in .NET Framework, .NET Core, or the Windows Runtime to create an asynchronous method almost as easily as you create a synchronous method. Asynchronous methods that you define by using the async keyword are referred to as async methods. The following example shows an async method.

How are simple asynchronous tasks executed in Java?

Simple asynchronous tasks are executed in background, without the possibility to configure execution frequency. Because they're two different task types, they're two different executors too. The first one looks like Java's concurrency executors, saw in the article about executors in Java.


2 Answers

Using sample code and Akka API I made fast test, works for me.

Comparing code between 2.0.4 and 2.1RC1 I can see there're only two changes in case of scheduler:

  1. replaced import

    // import akka.util.duration._
    import scala.concurrent.duration._
    
  2. added import:

    import play.api.libs.concurrent.Execution.Implicits._
    

app/controllers/Application.scala

package controllers

import play.api._
import play.api.mvc._
import play.libs.Akka

import akka.actor._
import scala.concurrent.duration._
import play.api.libs.concurrent.Execution.Implicits._

object Application extends Controller {

  def index = Action {

    // say hello
    Logger.info("hello, index action started")

    val Tick = "tick"
    val Tack = "tack"

    val tickActor = Akka.system.actorOf(Props(new Actor {
      def receive = {
        case Tick => Logger.info("that still ticks!")
        case Tack => Logger.warn("... 7 seconds after start, only once")
      }
    }))

    // Repeat every 5 seconds, start 5 seconds after start
    Akka.system.scheduler.schedule(
      5 seconds,
      5 seconds,
      tickActor,
      Tick
    )

    // do only once, 7 seconds after start
    Akka.system.scheduler.scheduleOnce(7 seconds, tickActor, Tack)

    Ok(views.html.index("Your new application is ready."))
  }

}

Edit

Nota bene, as I can see from Julien's post on the group, that's enough to import defaultContext only:

import play.api.libs.concurrent.Execution.Implicits.defaultContext
like image 185
biesior Avatar answered Oct 19 '22 03:10

biesior


biesior's answer is great

However, you don't have to go through an actor if you don't want to. Here's the same answer using good old java.lang.Runnable:

Akka.system.scheduler.schedule(5 minutes, 5 minutes, new Runnable {
  def run() {
    Logger.info("that still ticks!")
  }
})
Akka.system.scheduler.scheduleOnce(7 minutes, new Runnable {
  def run() {
    Logger.warn("... 7 seconds after start, only once")
  }
})
like image 9
ripper234 Avatar answered Oct 19 '22 03:10

ripper234