Java库类来处理“回调”的调度执行? [英] Java library class to handle scheduled execution of "callbacks"?

查看:215
本文介绍了Java库类来处理“回调”的调度执行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的程序有一个组件 - 被称为调度程序 - 让其他组件注册时间点,他们想要回调。这应该像Unix cron服务一样工作, e。你告诉调度程序在每一个小时后十分钟通知我。

My program has a component - dubbed the Scheduler - that lets other components register points in time at which they want to be called back. This should work much like the Unix cron service, i. e. you tell the Scheduler "notify me at ten minutes past every full hour".

我意识到Java中没有真正的回调。

I realize there are no real callbacks in Java.

这是我的方法,有一个已经做这个东西的图书馆?

Here's my approach, is there a library which already does this stuff? Feel free to suggest improvements, too.

注册电话到调度通行证:

Register call to Scheduler passes:


  • 包含小时,分钟,秒,年月,dom,dow的时间规范,其中每个项目可以是未指定的,意味着每小时/分钟等执行 (就像crontabs)

  • 一个包含数据的对象,它将告诉调用对象在调度程序通知它时该做什么。

  • 对调用对象的引用

启动时或在新的注册请求之后,调度程序以当前系统时间的Calendar对象开始,并检查数据库中是否存在与此时间点匹配的任何条目。如果存在,则它们被执行并且该过程重新开始。如果没有,则Calendar对象中的时间增加一秒,并重新检查entreis。这重复,直到有一个或多个匹配项。 (离散事件模拟)

Upon startup, or after a new registration request, the Scheduler starts with a Calendar object of the current system time and checks if there are any entries in the database that match this point in time. If there are, they are executed and the process starts over. If there aren't, the time in the Calendar object is incremented by one second and the entreis are rechecked. This repeats until there is one entry or more that match(es). (Discrete Event Simulation)

然后,调度程序将记住时间戳,睡眠和每秒唤醒以检查它是否已经存在。如果它发生了醒来,时间已经过去,它开始,同样如果时间到了,工作已经执行。

The Scheduler will then remember that timestamp, sleep and wake every second to check if it is already there. If it happens to wake up and the time has already passed, it starts over, likewise if the time has come and the jobs have been executed.

编辑:感谢您指点我的Quartz。

Edit: Thanks for pointing me to Quartz. I'm looking for something much smaller, however.

推荐答案

如果您的需求很简单,请考虑使用 java.util.Timer

If your needs are simple, consider using java.util.Timer:

public class TimerDemo {

  public static void main(String[] args) {
    // non-daemon threads prevent termination of VM
    final boolean isDaemon = false;
    Timer timer = new Timer(isDaemon);

    final long threeSeconds = 3 * 1000;
    final long delay = 0;
    timer.schedule(new HelloTask(), delay, threeSeconds);

    Calendar calendar = Calendar.getInstance();
    calendar.add(Calendar.MINUTE, 1);
    Date oneMinuteFromNow = calendar.getTime();

    timer.schedule(new KillTask(timer), oneMinuteFromNow);
  }

  static class HelloTask extends TimerTask {
    @Override
    public void run() {
      System.out.println("Hello");
    }
  }

  static class KillTask extends TimerTask {

    private final Timer timer;

    public KillTask(Timer timer) {
      this.timer = timer;
    }

    @Override
    public void run() {
      System.out.println("Cancelling timer");
      timer.cancel();
    }
  }

}

注明 ExecutorService java.util.concurrent 提供了更丰富的API如果你需要它。

As has been noted, the ExecutorService of java.util.concurrent offers a richer API if you need it.

这篇关于Java库类来处理“回调”的调度执行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆