java - Play 2.5 - 在一天中的特定时间运行 Java 方法 (cron)

标签 java scala cron playframework-2.0 sbt

我正在开发一个 Play 2.5 应用程序,该应用程序需要每天中午、下午 2 点、4 点自主运行一个方法。

到目前为止我已经关注了another answer on here这让我度过了大部分时间。 application.conf 文件已更新以查看模块文件,该文件正确绑定(bind)到 OnStartup() 方法。

我认为问题与 OnStartup() 方法中的代码有关,我已包含以下代码 - 这是在一天中的特定时间运行某些内容的正确方法吗?

package controllers;

import com.google.inject.Inject;
import com.google.inject.Singleton;

import java.util.Calendar;


@Singleton
public class OnStartup {

    @Inject
    public OnStartup() {

        Calendar cal = Calendar.getInstance();

        String hour = String.valueOf(cal.get(Calendar.HOUR_OF_DAY));
        String minute = String.valueOf(cal.get(Calendar.MINUTE));

        String dateTime = hour + ":" + minute;
        String time = "12:00";
        String time1 = "14:00";
        String time2 = "16:00";

        if (dateTime.equals(time) || dateTime.equals(time1) || dateTime.equals(time2)){
            System.out.print(dateTime);
            myAmazingClass.doSomethingWonderful();
        }
    }
}

最佳答案

根据您的方法,您需要三样东西:模块、 Actor 和调度程序。

第 1 步:创建 Actor

import akka.actor.UntypedActor;
import play.Logger;

public class DoSomethingActor extends UntypedActor{

    @Override
    public void onReceive(final Object message) throws Throwable {
        Logger.info("Write your crone task or simply call your method here that perform your task"+message);

    }

}

第2步:创建调度程序

import java.util.concurrent.TimeUnit;

import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Singleton;

import akka.actor.ActorRef;
import akka.actor.ActorSystem;
import akka.actor.Cancellable;
import scala.concurrent.duration.Duration;

@Singleton
public class DoSomethingScheduler {

    @Inject
    public DoSomethingScheduler(final ActorSystem system,
            @Named("do-something-actor") final ActorRef doSomethingActor) {
        final Cancellable scheduler;
        final int timeDelayFromAppStart = 0;
        final int timeGapInSeconds = 1; //Here you provide the time delay for every run
        system.scheduler().schedule(
                Duration.create(timeDelayFromAppStart, TimeUnit.MILLISECONDS), //Initial delay when system start
                Duration.create(timeGapInSeconds, TimeUnit.SECONDS),     //Frequency delay for next run
                doSomethingActor,
                "message for onreceive method of doSomethingActor",
                system.dispatcher(),
                null);
    }
}

第三步:将调度程序和参与者与模块绑定(bind)。

import com.google.inject.AbstractModule;

import play.libs.akka.AkkaGuiceSupport;

public class SchedulerModule extends AbstractModule implements AkkaGuiceSupport{

    @Override
    protected void configure() {
        this.bindActor(DoSomethingActor.class, "do-something-actor");
        this.bind(DoSomethingScheduler.class).asEagerSingleton();
    }

}

第四步:在 aaplication.conf 中配置模块

play.modules.enabled += "com.rits.modules.SchedulerModule"

确保无需担心 onStart 模块,像往常一样,该模块仅用于您调度任务的目的。

关于java - Play 2.5 - 在一天中的特定时间运行 Java 方法 (cron),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42139866/

相关文章:

linux - 如何使服务不需要 super 用户密码输入?

java - 在 TomCat 服务器上使用 JavaServlets 的 Cronjob

java - 程序在到达基本代码之前终止

与 R data.frame 类似的 Java 对象

java - 比较两种反向链表算法内部的空间复杂度

scala - 使用 def 方法和 val 函数压平 Vs flatMap

java - 序列化 LinkedList<Object> 的语法

Scala 向右折叠和向左折叠

scala - 如何禁用 Scopt 的 "Unknown options"异常?

linux - Cron 作业未在 Linux Mint 12 中运行