java - 一个项目在集群中运行,如何让定时任务只运行一次?
黄舟
黄舟 2017-04-18 10:32:05
0
8
1094

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

黄舟
黄舟

人生最曼妙的风景,竟是内心的淡定与从容!

reply all(8)
洪涛

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.

左手右手慢动作

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.

Ty80

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.

巴扎黑

quartz framework supports clustering

左手右手慢动作

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.

左手右手慢动作

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...

阿神
  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

Ty80

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

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template