play framework 2.1 - scheduling async tasks

后端 未结 3 722
感情败类
感情败类 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: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()
    );
    

提交回复
热议问题