播放 2.5 - 运行 一个 Java 方法在一天中的特定时间 (cron)
Play 2.5 - Run a Java method at certain times of day (cron)
我正在开发一个 Play 2.5 应用程序,它需要在每天中午、下午 2 点、下午 4 点自动 运行 一个方法。
到目前为止,我已经按照 进行了大部分操作。 application.conf 文件已更新以查看模块文件,该文件正确绑定到 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();
}
}
}
根据您的方法,您需要模块、参与者和调度程序这三样东西。
第一步:创建演员
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);
}
}
第二步:创建调度器
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);
}
}
第 3 步:将您的调度程序和 actor 与模块绑定。
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();
}
}
第 4 步:在 aaplication.conf
中配置您的模块
play.modules.enabled += "com.rits.modules.SchedulerModule"
确保不用担心 onStart 模块像往常一样离开它,这个模块只是为你调度任务的目的服务。
我正在开发一个 Play 2.5 应用程序,它需要在每天中午、下午 2 点、下午 4 点自动 运行 一个方法。
到目前为止,我已经按照
我认为问题与 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();
}
}
}
根据您的方法,您需要模块、参与者和调度程序这三样东西。
第一步:创建演员
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);
}
}
第二步:创建调度器
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);
}
}
第 3 步:将您的调度程序和 actor 与模块绑定。
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();
}
}
第 4 步:在 aaplication.conf
中配置您的模块play.modules.enabled += "com.rits.modules.SchedulerModule"
确保不用担心 onStart 模块像往常一样离开它,这个模块只是为你调度任务的目的服务。