我正在使用 Play Framework 2.6.x 创建一个在特定时间(例如凌晨 3 点)运行任务的简单 cron 作业。但现在我陷入了一个简单的计划任务:
我创建了一个演员:
package actors;
import akka.actor.UntypedActor;
import org.slf4j.LoggerFactory;
public class DoSomethingActor extends UntypedActor {
private static final org.slf4j.Logger log = LoggerFactory.getLogger(DoSomethingActor.class);
@Override
public void onReceive(final Object message) throws Throwable {
log.info("Write your crone task or simply call your method here that perform your task " + message);
}
}
然后我创建了一个 Schedule 类,每次我设置时调用 Actor:
package tasks;
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);
}
}
最后,我将这些类绑定到一个模块类中:
package modules;
import actors.DoSomethingActor;
import com.google.inject.AbstractModule;
import play.libs.akka.AkkaGuiceSupport;
import tasks.DoSomethingScheduler;
在这些事情之后,我运行了该应用程序,但它并没有像我预期的那样工作。我希望它每 1 秒显示一次日志记录,但没有任何反应。
你能帮我修一下吗?太感谢了!
开心每一天1111
相关分类