search

Home  >  Q&A  >  body text

java - 一个项目在集群中运行,如何让定时任务只运行一次?

一个项目在集群中运行,如何让定时任务只运行一次?不加限制就会导致重复计算的问题。

黄舟黄舟2882 days ago1132

reply all(8)I'll reply

  • PHP中文网

    PHP中文网2017-04-18 10:34:05

    For Java, it is recommended to use quartz.
    1. Quartz is very convenient for doing various scheduled tasks.
    2. It supports clusters. The principle is to ensure that the same task is only executed on one service through database tables.

    reply
    0
  • ringa_lee

    ringa_lee2017-04-18 10:34:05

    Assuming you use it in the simplest wayquartz, then you need to modify it in the code and add the operation of acquiring the lock in the job logic. This lock can be applied to the database. The specific method is: insert a primary key record with business significance and execute , only one of the same jobs in the cluster will be inserted successfully, which means that it has obtained the lock and can be executed. Otherwise, the job will be exited and not executed.

    reply
    0
  • PHP中文网

    PHP中文网2017-04-18 10:34:05

    Configure Clustering with JDBC-JobStore

    Quartz’s clustering features bring both high availability and scalability to your scheduler via fail-over and load balancing functionality.

    quartz itself supports clustering, fault tolerance, and load balancing. This can be achieved without changing any code.

    reply
    0
  • 巴扎黑

    巴扎黑2017-04-18 10:34:05

    quartz framework supports clustering

    reply
    0
  • ringa_lee

    ringa_lee2017-04-18 10:34:05

    You can make a middleware system that specifically manages the triggering of scheduled tasks and the subscription and consumption of messages. When the time is triggered, a message will be sent out, and the cluster system will subscribe to this message. Messages can be randomly delivered to any machine in the cluster.

    reply
    0
  • ringa_lee

    ringa_lee2017-04-18 10:34:05

    When a machine starts timing, it must broadcast messages to other machines. But the time interval of each machine is a problem. Before the message is sent, they are all turned on regularly...

    reply
    0
  • 阿神

    阿神2017-04-18 10:34:05

    1. Use zookeeper, redis, etc. to manually store scheduled task execution status and other information to ensure that it is only executed once.

    2. Extract scheduled tasks into a separate project (run on a single machine), but the specific content of the task calls specific distributed services through remote calls (dubbo, mq, etc.). That is to say, scheduled tasks are only used for task triggering alone, and specific tasks are executed in a distributed manner.

    3. Use various distributed task scheduling systems, such as Uncode-Schedule

    reply
    0
  • PHP中文网

    PHP中文网2017-04-18 10:34:05

    I tend to pull it out and make a stand-alone program

    reply
    0
  • Cancelreply