【问题标题】:Schedule a JOb on specific date in Akka Scheduler在 Akka Scheduler 中的特定日期安排 JOb
【发布时间】:2016-12-10 07:59:18
【问题描述】:

我有一个线程需要在特定日期运行,可以说它是每个月的第一天。 由于我们从一开始就使用 Akka 调度器,所以我只想知道我们是否可以通过 Akka 来实现。(Quartz 调度器可以轻松解决这个问题。)

我可以在 Akka scheduler schedule 函数中看到,我们需要在参数中传递重启时间。但是这个特定日期的重启时间并不相同,因为有些月份是 30 天、31 天等。 因此,我的调度程序在每次重启时间过后都在运行。

Cron Expression: cron_expression=0 0 06 1 * ?
restart_time =86400 (here it is 24 hours)

Akka.system().scheduler().schedule(validCronExpressionTime, Duration.create(restart, TimeUnit.SECONDS), thread, Akka.system().dispatcher());

这里validCronExpressionTime会根据文件中的cron_expression设置找到有效日期。

在 akka 调度器中,第一个参数仅用于在特定时间启动调度器,之后它会根据重新启动时间重复。

【问题讨论】:

  • 下面的回答对你有帮助吗?
  • 嗨,阿米特,您的代码似乎只会在每月 1 日运行一次线程,但我想在每月 1 日运行它。
  • 不,这将在每个月的第一个日期运行,您需要将相同的代码放入您想要在每个第一个日期运行的 actor 中。继续询问是否有任何其他问题。
  • 您设置的持续时间始终检查当月 1 日与当前时间之间的差异,但情况并非总是如此,Schedule once 将在每次特定持续时间后执行 runnable。这里的持续时间将被设置一次,每次它都会在该持续时间间隔之后继续调度。 ??
  • 我们无法按您的意愿安排 cron。我们需要在每个前一个事件开始时安排下一个事件。这是我们在 akka schedule 中可以做的唯一方法。如果您在实施此方法时有任何问题,请告诉我

标签: java akka scheduled-tasks


【解决方案1】:

创建一个演员,说测试演员,并从您的主类和该演员内部告诉它,在下面的代码中接收使用。

Calendar cal = Calendar.getInstance();
cal.set(Calendar.DATE, <choose date of your choice>);
cal.set(Calendar.MONTH, <choose month of your choice>);

Calendar cal2 = Calendar.getInstance();

long duration =(cal2.getTimeInMillis()-cal.getTimeInMillis())/1000;

ActorRef testActorRef = getContext().actorOf(Props.create(Test.class));
getContext().system().scheduler().scheduleOnce(Duration.create(duration, TimeUnit.SECONDS), testActorRef, messege, getContext().system().dispatcher(), null);

【讨论】:

  • 如果有任何问题,请告诉我。
  • 您先设置日期 1,然后添加 1 个月。
【解决方案2】:

我正在使用 akka 调度程序,如下代码所示。

每个用户都可以登录到我的应用程序并可以为后台任务创建调度程序。为此我使用 Akka 调度程序。

public Cancellable buildScheduler(String actorName, SchedulerActorMessage message, long interval, TimeUnit timeUnit, long initialDelay, String actorMapKey) {
    ActorRef daemonRef = actorSystem.actorOf(Props.create(SchedulerActor.class), actorName);
    Cancellable cancellableActor = actorSystem.scheduler().schedule(FiniteDuration.apply(initialDelay, timeUnit),
            FiniteDuration.apply(interval, timeUnit), daemonRef, message,
            actorSystem.dispatcher(), ActorRef.noSender());
    actorMap.put(actorMapKey, cancellableActor);
    return cancellableActor;
}

现在,在 SchedulerActor 中,我定义了如下作业,

public class SchedulerActor extends AbstractActor {
    @Override
    public Receive createReceive() {
        return receiveBuilder().match(SchedulerActorMessage.class, message -> {
            CompletableFuture<String> jobResult =
                    message.getJob().scheduleJob(message.Id());
            jobResult.thenAccept(state -> {
                if (state.equals(DONE)) {
                    //do post action
                } else {
                    //handle exception
                }
            });
        }).build();
    }

我在 SchedulerActorMessage 中传递我的 Job 实例。

【讨论】:

    猜你喜欢
    • 2014-02-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-06
    • 2023-01-20
    • 2018-12-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多