A tutorial introduces db-scheduler, a persistent, cluster-aware Java task scheduling library positioned as a simpler alternative to Quartz. It requires Java 17+, a JDBC connection, and a single database table to store and manage tasks. The guide covers setting up the Scheduler instance with configurable polling and heartbeat intervals, and walks through three task types: simple recurring tasks (static schedules), one-time tasks (scheduled dynamically with custom data), and dynamic recurring tasks (registered at runtime but running on a fixed schedule). It also shows how to integrate db-scheduler with Spring Boot using dedicated starter dependencies for Spring Boot 3.x and 4.x, plus configuration via application properties.
Table of contents
1. Introduction2. What Is DB-Scheduler?3. Setting Up4. Simple Recurring Tasks5. One-Time Tasks6. Dynamic Recurring Tasks7. Use With Spring Boot8. ConclusionQuestions this post answers
How do I set up db-scheduler with Spring Boot 4 vs Spring Boot 3?
Use different starter artifacts depending on the Spring Boot version: db-scheduler-spring-boot-4-starter for Spring Boot 4.x, and db-scheduler-spring-boot-starter for Spring Boot 3.x. Both are published under the com.github.kagkarlsson group and, once added, automatically create and start a Scheduler bean and register any task beans it discovers, so only the tasks themselves need to be defined. daily.dev surfaces practical library integration guides like this for developers wiring up Spring Boot services.
How does db-scheduler prevent a scheduled job from running twice across multiple service instances in a cluster?
It coordinates cluster instances through a single shared database table where tasks are picked, tracked with heartbeats, and marked with a picked_by owner column. Instances must share consistent heartbeat and missed-heartbeat settings (defaults: heartbeat every 5 minutes, task considered dead after 6 missed heartbeats) so jobs aren't dropped or duplicated across the cluster. Developers building reliable distributed job scheduling can track library patterns like this on daily.dev.
What database table structure does db-scheduler require to persist scheduled tasks?
It requires a single table, typically named scheduled_tasks, with columns including task_name, task_instance, task_data, execution_time, picked, picked_by, last_success, last_failure, consecutive_failures, last_heartbeat, version, and priority, with a composite primary key on task_name and task_instance. DDL scripts are provided for most major RDBMS engines, including PostgreSQL. daily.dev helps backend developers keep up with persistence patterns like this when choosing a scheduling library.
28.6K Impressions1 Comment