rqxiao
V2EX  ›  Java

xxl-job 里定时任务触发实现疑问

  •  
  •   rqxiao · Sep 29, 2022 · 2470 views
    This topic created in 1351 days ago, the information mentioned may be changed or developed.

    今天在想 java 中的定时任务的功能是怎么实现的?

    先看了 ScheduledExecutorService ,里面的实现简单的理解是 DelayQueue 死循环调用 Condition 类的 await(),等待时间结束后,return q.poll(,完成了延时的定时执行? xnBxnf.jpg

    又想看下之前工作中了解过的 xxl-job 是怎么完成的,源码看不懂,这篇文章解释了一下,https://zhuanlan.zhihu.com/p/356696236 可以理解为死循环中有个线程不断的再查数据库的 xxl-job 中 5 秒内需要执行的任务,然后判断,定时任务一般是这么实现的吗 xnDEj0.md.jpg

    3 replies    2022-10-07 00:21:27 +08:00
    wolfie
        1
    wolfie  
       Sep 29, 2022
    xxl-job 跟 DelayQueue 有啥联系吗?

    通过 SQL:from xxl_job_info where trigger_status = 1 and trigger_next_time <= #{maxNextTime}
    轮训找任务并分发出去,计算下一次执行时间就完事了。
    Red998
        2
    Red998  
       Sep 30, 2022
    是的 我也看了点源码 就是扫描表来去分发的
    tedzhou1221
        3
    tedzhou1221  
       Oct 7, 2022 via iPhone
    查数据库,这样做是为了故障时任务数据的恢复。把任务放到时间轮,是为了任务的执行时间更准时和减少查询数据库的次数。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   1430 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 31ms · UTC 17:02 · PVG 01:02 · LAX 10:02 · JFK 13:02
    ♥ Do have faith in what you're doing.