简体   繁体   English

Java 多线程MySql数据库中任务项的任务调度

[英]Java task scheduling for task items in MySql database with Multi-threading

I am writing a task scheduling module in java spring to handle task items which are stored in Mysql database.我正在 java spring 中编写一个任务调度模块来处理存储在 Mysql 数据库中的任务项。

Schema structure of the Task table: Task表的Schema结构:

ID | TASK_UUID | TASK_CONTENT(VARCHAR) | CREATED_TS | UPDATED_TS | STATUS(NEW/PROCESSING/COMPLETE)

I would like to implement multiple task scheduler workers to get the tasks for execution from Task table.我想实现多个任务调度程序工作人员以从任务表中获取要执行的任务。 In what way I can ensure the task schedulers would not get the same task for execution at the same time?我可以通过什么方式确保任务调度程序不会同时执行相同的任务? Any good java framework I can make use of?我可以使用任何好的 java 框架吗?

#Edit 1: The task execution module is designed to be run by different machines, so sychronized methods may not work. #Edit 1:任务执行模块被设计为由不同的机器运行,因此同步方法可能不起作用。

#Edit 2: Each machine will get random or irregular numbers of task. #Edit 2:每台机器都会获得随机或不规则数量的任务。 So if auto-increment sequence is used, the allocation size of the index should be irregular too, otherwise there will be some tasks being never handled.所以如果使用自增序列,索引的分配大小也应该是不规则的,否则会有一些任务永远处理不了。

#Edit 3: Each machine is running with Quartz Scheduler , configured with a constant Task getting and executing job. #Edit 3:每台机器都使用Quartz Scheduler运行,配置有一个恒定的任务获取和执行作业。 The time interval between every job is about 10 seconds .每个作业之间的时间间隔约为10 秒 So, my goal is to ensure each machine scheduler can fetch at least 10 tasks in every quartz job run.因此,我的目标是确保每个机器调度程序在每次石英作业运行中都能获取至少10 个任务

You could create the method getTask as a synchronized method:您可以将方法getTask创建为同步方法:

Eg:例如:

synchronized Task getTask() {
  // get NEW task from DB
  // update status to PROCESSING
  // return task
}

#Edit 1: If so, just use SELECT FOR UPDATE query to block the others query to access the same task. #Edit 1:如果是这样,只需使用 SELECT FOR UPDATE 查询来阻止其他查询访问相同的任务。 Eg:例如:

SELECT * FROM Task t WHERE t.status = NEW ORDER BY t.created_ts LIMIT 1 FOR UPDATE;
UPDATE Task SET status = PROCESSING WHERE id = <the task id> .

You could create a procedure to wrap the queries.您可以创建一个过程来包装查询。

You can just work around the atomicity or transaction issue like this,您可以像这样解决原子性或事务问题,

Using the id of your task, assuming it's incremental.假设它是增量的,使用您的任务的 id。 If you have three machines runing the task scheduling.如果你有三台机器运行任务调度。 Then just mod the id by three and assign the tasks with result 0, 1,2 to a fixed machine.然后只需将 id 修改为 3 并将结果为 0、1,2 的任务分配给固定机器。 So different machines wont' interfere with each other (or race condition)所以不同的机器不会互相干扰(或竞争条件)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM