我的 Play 应用程序使用 Akka 安排每日任务,如下所示:
public class Global extends GlobalSettings {
private void schedule() {
try {
Time.CronExpression e = new Time.CronExpression("0 0 0 ? * *");
Date nextValidTimeAfter = e.getNextValidTimeAfter(new Date());
FiniteDuration d = Duration.create(
nextValidTimeAfter.getTime() - System.currentTimeMillis(),
TimeUnit.MILLISECONDS);
Logger.debug("Scheduling to run at "+nextValidTimeAfter);
scheduler = Akka.system().scheduler().scheduleOnce(d, new Runnable() {
@Override
public void run() {
Logger.debug("Running scheduler");
//do something
schedule(); //Schedule for next time
}
}, Akka.system().dispatcher());
} catch (Exception e) {
Logger.error("", e);
}
}
}
任务按预期在本地主机上执行。但是,尽管它记录 Scheduling to run at ...
最佳答案
有两种可能性:要么 Heroku 由于不活动而使您的应用程序 hibernate ( this happens for free apps ),要么在作业运行之前重新启动您的 dyno(每 24 小时就会重新启动一次)。
最好使用 Heroku Scheduler来安排任务。这也更具可扩展性,因为您可能不想在应用程序的每个实例上运行此作业。
关于java - Play应用程序不执行heroku上的计划任务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30194578/