Quartz is an open-source Java job scheduling framework that provides powerful capabilities for scheduling tasks. In DolphinScheduler, Quartz is used to implement task scheduling and management. DolphinScheduler integrates with Quartz through the QuartzExecutorImpl class, combining workflow and schedule management operations with Quartz's scheduling framework to achieve task execution.
\ This article provides a detailed analysis of Quartz's principles and implementation within DolphinScheduler.
Quartz Entity-Relationship Diagram\
QRTZ_JOB_DETAILS and QRTZ_TRIGGERS are the central tables, defining the relationship between jobs and triggers.
\
The QRTZ_TRIGGERS table links with multiple trigger-type tables, such as QRTZ_SIMPLE_TRIGGERS and QRTZ_CRON_TRIGGERS, to enable different trigger mechanisms.
\
QRTZ_FIRED_TRIGGERS records execution history, associating with both the job and trigger tables.
\
QRTZ_CALENDARS defines calendar exclusion rules for triggers, while QRTZ_PAUSED_TRIGGER_GRPS manages the pause state of trigger groups.
\
QRTZ_SCHEDULER_STATE and QRTZ_LOCKS are used for scheduling coordination in clustered environments, ensuring high availability.
We will focus on the principle analysis of the use of quartz in DolphinScheduler, so the steps for using quartz in DolphinScheduler will be introduced briefly. Generally speaking, there are 4 steps:
\
At its core, this operation inserts a new entry into the schedule table, as shown below: \n
Activating a Schedule org.apache.dolphinscheduler.api.controller.SchedulerController#publishScheduleOnline --org.apache.dolphinscheduler.api.service.impl.SchedulerServiceImpl#onlineScheduler ----org.apache.dolphinscheduler.api.service.impl.SchedulerServiceImpl#doOnlineScheduler ------org.apache.dolphinscheduler.scheduler.quartz.QuartzScheduler#insertOrUpdateScheduleTask // Simplified code: JobKey jobKey = QuartzTaskUtils.getJobKey(schedule.getId(), projectId); MapJob Details Table: Stores detailed information about each task.
Trigger Base Table: Stores basic information for all trigger types.
Cron Trigger Table: Stores information about Cron expression triggers.
Schedule Execution org.apache.dolphinscheduler.scheduler.quartz.ProcessScheduleTask protected void executeInternal(JobExecutionContext context) { JobDataMap dataMap = context.getJobDetail().getJobDataMap(); int projectId = dataMap.getInt(QuartzTaskUtils.PROJECT_ID); int scheduleId = dataMap.getInt(QuartzTaskUtils.SCHEDULE_ID); Date scheduledFireTime = context.getScheduledFireTime(); Date fireTime = context.getFireTime(); Command command = new Command(); command.setCommandType(CommandType.SCHEDULER); command.setExecutorId(schedule.getUserId()); command.setFailureStrategy(schedule.getFailureStrategy()); command.setProcessDefinitionCode(schedule.getProcessDefinitionCode()); command.setScheduleTime(scheduledFireTime); command.setStartTime(fireTime); command.setWarningGroupId(schedule.getWarningGroupId()); String workerGroup = StringUtils.isEmpty(schedule.getWorkerGroup()) ? Constants.DEFAULT_WORKER_GROUP : schedule.getWorkerGroup(); command.setWorkerGroup(workerGroup); command.setTenantCode(schedule.getTenantCode()); command.setEnvironmentCode(schedule.getEnvironmentCode()); command.setWarningType(schedule.getWarningType()); command.setProcessInstancePriority(schedule.getProcessInstancePriority()); command.setProcessDefinitionVersion(processDefinition.getVersion()); commandService.createCommand(command); }Essentially, this is a callback function in Quartz that ultimately generates a Command.
All Rights Reserved. Copyright , Central Coast Communications, Inc.