每天与Akka一起运行任务

时间:2014-09-26 12:57:48

标签: java akka actor playframework-2.2

我正在寻找使用Java的PlayFramework中使用Akka Aktor的简单示例/手册/ tutoral。 我已经尝试过本教程了: https://www.playframework.com/documentation/2.2.x/JavaAkka 但我不能让它编译。<​​/ p>

我想在每24个小时计算一些数据并从系统发送电子邮件。我想使用Aktor。

我使用playFramework 2.2.x

2 个答案:

答案 0 :(得分:2)

我的解决方案(适用于playFramework 2.2.4)基于帖子:https://stackoverflow.com/a/14706767/1066240

我在/app/Global.java中的Global.java类

import org.joda.time.DateTime;
import org.joda.time.Seconds;

import actions.ValidateApplicationLicence;
import akka.actor.ActorRef;
import akka.actor.Props;
import play.Application;
import play.GlobalSettings;
import play.Logger;
import play.libs.Akka;
import scala.concurrent.duration.Duration;

import java.util.concurrent.TimeUnit;

public class Global extends GlobalSettings {

    //ActorRef myActor = Akka.system().actorOf(new Props(ValidateApplicationLicence.class));

    @Override
    public void onStart(Application application) {

        Akka.system().scheduler().schedule(
                Duration.create(20, TimeUnit.SECONDS),
                Duration.create(5, TimeUnit.SECONDS),
                new Runnable() {
                    @Override
                    public void run() {
                        Logger.info("After 10 sec and after EVERY 5 sec  ---    " + controllers.common.Index.getDate(null));
                    }
                },
                Akka.system().dispatcher()
        );

        Akka.system().scheduler().scheduleOnce(
                Duration.create(10, TimeUnit.MILLISECONDS),
                new Runnable() {
                    public void run() {
                        Logger.info("ON START ---    " + controllers.common.Index.getDate(null));
                    }
                },
                Akka.system().dispatcher()
        );
//      Akka.system().scheduler().schedule(
//                Duration.create(0, TimeUnit.MILLISECONDS), //Initial delay 0 milliseconds
//                Duration.create(30, TimeUnit.MINUTES),     //Frequency 30 minutes
//                myActor,
//                "tick",
//                Akka.system().dispatcher(),
//                null
//        );
    }
    public static int nextExecutionInSeconds(int hour, int minute){
        return Seconds.secondsBetween(
                new DateTime(),
                nextExecution(hour, minute)
        ).getSeconds();
    }

    public static DateTime nextExecution(int hour, int minute){
        DateTime next = new DateTime()
                .withHourOfDay(hour)
                .withMinuteOfHour(minute)
                .withSecondOfMinute(0)
                .withMillisOfSecond(0);

        return (next.isBeforeNow())
                ? next.plusHours(24)
                : next;
    }
}

答案 1 :(得分:1)

Play 2.0.4 in Java有一个样本,无论如何2.1中的一些变化(主要是进口)