【问题标题】:playframework running task everyday with Akkaplayframework 每天用 Akka 运行任务
【发布时间】:2014-09-26 12:57:47
【问题描述】:

我正在寻找在 PlayFramework with Java 中使用 Akka Aktor 的简单示例/手册/教程。 我已经尝试过本教程: https://www.playframework.com/documentation/2.2.x/JavaAkka 但我无法编译。

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

我使用 playFramework 2.2.x

【问题讨论】:

    标签: java akka actor playframework-2.2


    【解决方案1】:

    我的工作解决方案(针对 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;
        }
    }
    

    【讨论】:

      【解决方案2】:

      Play 2.0.4 in Java 有一个示例,无论如何在 2.1 中发生了一些变化(主要是导入)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-03-15
        • 2012-01-26
        • 1970-01-01
        • 2011-10-02
        • 1970-01-01
        • 1970-01-01
        • 2019-11-25
        • 1970-01-01
        相关资源
        最近更新 更多