重头戏2.5-在一天中的特定时间运行Java方法(cron)
我正在开发一个Play 2.5应用程序,它需要在每天中午、下午2点、下午4点自动运行一个方法 到目前为止,我一直在遵循这一原则,而这一原则让我走了大部分路。application.conf文件已更新,以查看模块文件,该文件正确绑定到OnStartup()方法 我相信问题在于OnStartup()方法中的代码,我已经在下面介绍了代码——这是在一天中的某些时间运行某些程序的正确方法吗重头戏2.5-在一天中的特定时间运行Java方法(cron),java,scala,cron,playframework-2.0,sbt,Java,Scala,Cron,Playframework 2.0,Sbt,我正在开发一个Play 2.5应用程序,它需要在每天中午、下午2点、下午4点自动运行一个方法 到目前为止,我一直在遵循这一原则,而这一原则让我走了大部分路。application.conf文件已更新,以查看模块文件,该文件正确绑定到OnStartup()方法 我相信问题在于OnStartup()方法中的代码,我已经在下面介绍了代码——这是在一天中的某些时间运行某些程序的正确方法吗 package controllers; import com.google.inject.Inject; imp
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();
}
}
}
根据您的方法,您需要三件事:模块、参与者和调度器 步骤1:创建演员 步骤2:创建调度程序 步骤3:将调度程序和参与者与模块绑定。 步骤4:在aaapplication.conf中配置模块
请确保无需担心onStart模块,请注意,与往常一样,此模块仅用于调度任务。根据您的方法,您需要三件事:模块、参与者和调度器 步骤1:创建演员 步骤2:创建调度程序 步骤3:将调度程序和参与者与模块绑定。 步骤4:在aaapplication.conf中配置模块
确保无需担心onStart模块,让它像往常一样,此模块仅用于调度任务。Akka
Scheduler
可以使用:我使用AWS Lambda函数触发某些端点(带有秘密查询字符串和几个其他安全块),这真的很简单——这意味着,对于运行应用程序的每台机器,该过程只调用一次,而不是调用一次。可以使用Akka调度程序
:我使用AWS Lambda函数触发某些端点(使用秘密查询字符串和几个其他安全块),这真的很简单——这意味着该过程只被调用一次,而不是对运行应用程序的每台机器调用一次。
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);
}
}
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();
}
}
play.modules.enabled += "com.rits.modules.SchedulerModule"