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

Viewed 404

I am writing a task scheduling module in java spring to handle task items which are stored in Mysql database.

Schema structure of the Task table:

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?

#Edit 1: The task execution module is designed to be run by different machines, so sychronized methods may not work.

#Edit 2: Each machine will get random or irregular numbers of task. 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. The time interval between every job is about 10 seconds. So, my goal is to ensure each machine scheduler can fetch at least 10 tasks in every quartz job run.

2 Answers

You could create the method getTask as a synchronized method:

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. 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. 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. So different machines wont' interfere with each other (or race condition)

Related