play framework 2.1 - scheduling async tasks

后端 未结 3 707
感情败类
感情败类 2020-12-15 08:34

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         


        
相关标签:
3条回答
  • 2020-12-15 09:03

    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
    
    0 讨论(0)
  • 2020-12-15 09:19

    For example run a task every Saturday at 15 AM in java:

    DateTime now = new DateTime();
    
    DateTime plannedStart = new DateTime()
        .withDayOfWeek(DateTimeConstants.SATURDAY)
        .withHourOfDay(15)
        .withMinuteOfHour(0)
        .withSecondOfMinute(0)
        .withMillisOfSecond(0);
    
    DateTime nextRun = (now.isAfter(plannedStart))
        ? plannedStart.plusDays(7)
        : plannedStart;
    
    Long nextRunInSeconds = (long) secondsBetween(now, nextRun).getSeconds();
    
    Akka.system().scheduler().schedule(
        Duration.create(nextRunInSeconds, TimeUnit.SECONDS),
        Duration.create(7, TimeUnit.DAYS) ,
        new Runnable() {
            public void run() {
                Logger.info("-------------------------scheduler start : " + new DateTime());
            }
        },
        Akka.system().dispatcher()
    );
    
    0 讨论(0)
  • 2020-12-15 09:23

    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")
      }
    })
    
    0 讨论(0)
提交回复
热议问题