Skip to main content

dawdler-schedule-core

Module introduction​

The schedule core module, which provides the @Schedule annotation and Quartz-based scheduled task capabilities.

1. Add the dependency to the pom​

<groupId>club.dawdler</groupId>
<artifactId>dawdler-schedule-core</artifactId>

2. Schedule annotation​

Used to mark a method as executed by the scheduler.

/**
* @author jackson.song
* @version V1.0
* Scheduled task annotation
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Schedule {

/**
* Specifies the configured schedule; if not specified, the default configuration is used (note that Quartz is multi-threaded by default). If specified, the corresponding properties file is looked up on the classpath -- for example, setting it to single uses the configuration in single.properties.
*/
String fileName() default "";

/**
* Quartz cron expression
*/
String cron();

/**
* Whether to execute concurrently
*/
boolean concurrent() default true;

}

3. Quartz configuration​

Please refer to the official Quartz configuration.

Recommendation:

If you use a single thread, you can use the following configuration

org.quartz.threadPool.threadCount=1 #threadCount sets the number of threads to 1, i.e. single-threaded
org.quartz.jobStore.misfireThreshold=10

About misfireThreshold

misfireThreshold only takes effect when a job is blocked; if the thread pool has plenty of threads, this parameter is meaningless. So most of the time it is only meaningful for stateful jobs.

org.quartz.jobStore.misfireThreshold = 60000 #default 60 seconds; if you do not want overdue tasks executed after a backlog caused by the current task running too long, you can set this value smaller.

For example, the first task runs at 2022/08/17 08:10:30 and the task runs every 5 seconds. If it is set to single-threaded and the same task manager is used, the next task would run at 2022/08/17 08:10:35; if the first task takes 10 seconds to finish, the task will run twice in a row at the next execution.

The criterion for misfire: if the difference between two task executions is greater than misfireThreshold, it is regarded as a misfire.

4. Cluster mode​

The JobDataMap only stores serializable metadata (target class name / method name / parameter type names); the target instance is resolved locally on each executing node through a registry, because the target instance contains injected dependencies and inherently cannot and should not be serialized and transmitted across nodes. Jobs and triggers are registered idempotently with deterministic identities (class name + method signature); in a cluster, the registration of the same job by all nodes converges into a single job and does not fire repeatedly, and re-registration updates the existing trigger with the latest cron. Therefore you can directly use Quartz's JDBC job store cluster configuration and let the cluster handle scheduling uniformly:

org.quartz.jobStore.class=org.quartz.impl.jdbcjobstore.JobStoreTX
org.quartz.jobStore.driver=...
org.quartz.jobStore.dataSource=...
org.quartz.jobStore.isClustered=true

Note: in a cluster, a job may fire on any node; a node where the corresponding business module is not deployed skips that firing and logs an error, so every node must deploy the same business modules; when any node unloads the corresponding business module, the job is deleted entirely.