eBook – Guide Spring Cloud – NPI EA (cat=Spring Cloud)
announcement - icon

Let's get started with a Microservice Architecture with Spring Cloud:

>> Join Pro and download the eBook

eBook – Mockito – NPI EA (tag = Mockito)
announcement - icon

Mocking is an essential part of unit testing, and the Mockito library makes it easy to write clean and intuitive unit tests for your Java code.

Get started with mocking and improve your application tests using our Mockito guide:

Download the eBook

eBook – Java Concurrency – NPI EA (cat=Java Concurrency)
announcement - icon

Handling concurrency in an application can be a tricky process with many potential pitfalls. A solid grasp of the fundamentals will go a long way to help minimize these issues.

Get started with understanding multi-threaded applications with our Java Concurrency guide:

>> Download the eBook

eBook – Reactive – NPI EA (cat=Reactive)
announcement - icon

Spring 5 added support for reactive programming with the Spring WebFlux module, which has been improved upon ever since. Get started with the Reactor project basics and reactive programming in Spring Boot:

>> Join Pro and download the eBook

eBook – Java Streams – NPI EA (cat=Java Streams)
announcement - icon

Since its introduction in Java 8, the Stream API has become a staple of Java development. The basic operations like iterating, filtering, mapping sequences of elements are deceptively simple to use.

But these can also be overused and fall into some common pitfalls.

To get a better understanding on how Streams work and how to combine them with other language features, check out our guide to Java Streams:

>> Join Pro and download the eBook

eBook – Jackson – NPI EA (cat=Jackson)
announcement - icon

Do JSON right with Jackson

Download the E-book

eBook – HTTP Client – NPI EA (cat=Http Client-Side)
announcement - icon

Get the most out of the Apache HTTP Client

Download the E-book

eBook – Maven – NPI EA (cat = Maven)
announcement - icon

Get Started with Apache Maven:

Download the E-book

eBook – Persistence – NPI EA (cat=Persistence)
announcement - icon

Working on getting your persistence layer right with Spring?

Explore the eBook

eBook – RwS – NPI EA (cat=Spring MVC)
announcement - icon

Building a REST API with Spring?

Download the E-book

Course – LS – NPI EA (cat=Jackson)
announcement - icon

Get started with Spring and Spring Boot, through the Learn Spring course:

>> LEARN SPRING
Course – RWSB – NPI EA (cat=REST)
announcement - icon

Explore Spring Boot 3 and Spring 6 in-depth through building a full REST API with the framework:

>> The New “REST With Spring Boot”

Course – LSS – NPI EA (cat=Spring Security)
announcement - icon

Yes, Spring Security can be complex, from the more advanced functionality within the Core to the deep OAuth support in the framework.

I built the security material as two full courses - Core and OAuth, to get practical with these more complex scenarios. We explore when and how to use each feature and code through it on the backing project.

You can explore the course here:

>> Learn Spring Security

Course – All Access – NPI EA (cat= Spring)
announcement - icon

All Access is finally out, with all of my Spring courses. Learn JUnit is out as well, and Learn Maven is coming fast. And, of course, quite a bit more affordable. Finally.

>> GET THE COURSE
Course – LSD – NPI EA (tag=Spring Data JPA)
announcement - icon

Spring Data JPA is a great way to handle the complexity of JPA with the powerful simplicity of Spring Boot.

Get started with Spring Data JPA through the guided reference course:

>> CHECK OUT THE COURSE

Partner – Moderne – NPI EA (cat=Spring Boot)
announcement - icon

Refactor Java code safely — and automatically — with OpenRewrite.

Refactoring big codebases by hand is slow, risky, and easy to put off. That’s where OpenRewrite comes in. The open-source framework for large-scale, automated code transformations helps teams modernize safely and consistently.

Each month, the creators and maintainers of OpenRewrite at Moderne run live, hands-on training sessions — one for newcomers and one for experienced users. You’ll see how recipes work, how to apply them across projects, and how to modernize code with confidence.

Join the next session, bring your questions, and learn how to automate the kind of work that usually eats your sprint time.

Course – LJB – NPI EA (cat = Core Java)
announcement - icon

Code your way through and build up a solid, practical foundation of Java:

>> Learn Java Basics

1. Introduction

In this tutorial, we’ll take a look at db-scheduler, a simpler alternative to Quartz. We’ll see what it is, how to use it and what we can do with it.

2. What Is DB-Scheduler?

DB-Scheduler is a cluster-aware, persistent scheduling library. It’s designed to be simple to include in our application. Our only requirements are Java 17+ and a JDBC connection to a database with a single database table in which we store and manage our tasks.

Using db-scheduler, we can easily support:

  • One-time tasks: tasks that are scheduled to run once at a specific point in time
  • Simple recurring tasks: tasks that are set up to run on a statically defined regular schedule
  • Dynamic recurring tasks: tasks that run on a regular schedule, but that we can schedule dynamically at runtime

The db-scheduler library will then handle everything for us. This includes ensuring that these jobs run correctly across a cluster of services without dropping them or running them multiple times.

3. Setting Up

Before we can use db-scheduler, we need to first set it up in our application. This includes adding it to our project and getting it running.

3.1. Dependencies

Before using db-scheduler, we need to include the latest version in our build, which is 16.12.0 at the time of writing.

If we’re using Maven, we can include this dependency in our pom.xml file:

<dependency>
    <groupId>com.github.kagkarlsson</groupId>
    <artifactId>db-scheduler</artifactId>
    <version>16.12.0</version>
</dependency>

This will bring in everything that we need to be able to use it in our application. The only required dependencies are JSpecify and SLF4J – everything else is self-contained.

3.2. Database Schema

In order to work, db-scheduler needs a single database table in the database we connect it to. DDL scripts are provided for most major RDBMS engines, though it might work on others as well. For example, to use with PostgreSQL, we’d use DDL like this:

CREATE TABLE scheduled_tasks (
  task_name TEXT NOT NULL,
  task_instance TEXT NOT NULL,
  task_data BYTEA,
  execution_time TIMESTAMP WITH TIME ZONE NOT NULL,
  picked BOOLEAN NOT NULL,
  picked_by TEXT,
  last_success TIMESTAMP WITH TIME ZONE,
  last_failure TIMESTAMP WITH TIME ZONE,
  consecutive_failures INT,
  last_heartbeat TIMESTAMP WITH TIME ZONE,
  version BIGINT NOT NULL,
  priority SMALLINT,
  PRIMARY KEY (task_name, task_instance)
);

CREATE INDEX execution_time_idx ON scheduled_tasks (execution_time);
CREATE INDEX last_heartbeat_idx ON scheduled_tasks (last_heartbeat);
CREATE INDEX priority_execution_time_idx on scheduled_tasks (priority desc, execution_time asc);

This creates our scheduled_tasks table with the expected structure, ready for db-scheduler to use.

3.3. Scheduler

Finally, before we can use db-scheduler, we need to actually set up a Scheduler instance. This is the central class that manages scheduling all of our jobs.

We create an instance of this by providing a DataSource pointing to the database containing our scheduled_tasks table, and a collection of all the tasks that we want to manage. We’ll see later how to create these:

Scheduler scheduler = Scheduler.create(dataSource)
  .startTasks(tasks)
  .registerShutdownHook()
  .build();

This comes with a reasonable set of default configuration values:

  • Poll every 10 seconds
  • Heartbeat every 5 minutes
  • Consider a task dead if it misses 6 heartbeats
  • Run jobs across 10 worker threads

And many more. We can configure all of these when we create our scheduler:

Scheduler scheduler = Scheduler.create(dataSource)
  .startTasks(tasks)
  .registerShutdownHook()
  .pollingInterval(Duration.ofSeconds(2))
  .heartbeatInterval(Duration.ofMinutes(2))
  .missedHeartbeatsLimit(10)
  .threads(5)
  .build();

It’s important that multiple instances in the same cluster use at least the same heartbeat settings. If not, db-scheduler might incorrectly consider some jobs to be dead.

Once we have a scheduler, we need to start it running:

scheduler.start();

At this point, the scheduler will now manage all of the tasks assigned to it. This includes ensuring they’re correctly scheduled and running them at the appropriate times.

4. Simple Recurring Tasks

The easiest tasks to work with are simple recurring tasks. For these, we need a unique name, a frequency at which the task runs, and the task itself:

RecurringTask<Void> task = Tasks.recurring("my-hourly-task", FixedDelay.ofHours(1))
  .execute((instance, context) -> {
    LOG.info("Executed!");
  });

If this is a new task that the scheduler has never seen before, it will be scheduled to run immediately. After that, the scheduler will run it according to the provided schedule. In this case, that means 1 hour after the previous iteration finished. We can also use Daily for running at a particular time every day, and CronSchedule for using a more complicated cron expression.

Our task itself is provided as an instance of VoidExecutionHandler<T>, which is itself a functional interface and can be implemented by a lambda if we wish. The only method of this interface takes two parameters:

  • TaskInstance<T> instance – Details about this instance of the task. Useful if we’ve got multiple schedules using the same task implementation.
  • ExecutionContext context – Details about the executor itself. Useful if we want to access the scheduler itself from within our task.

Note that we have VoidExecutionHandler and RecurringTask<Void> here. This is because our example task is stateless. We can also support stateful tasks, which have an initial state that can be updated as a result of the task executing. However, these are out of the scope of this article.

5. One-Time Tasks

One-time tasks work a bit differently. In this case, we need to distinguish between the task itself and a descriptor that identifies the task:

TaskDescriptor<String> taskDescriptor = TaskDescriptor.of("my-onetime-task", String.class);

Task<String> task = Tasks.oneTime(taskDescriptor)
  .execute((inst, ctx) -> {
    LOG.info("Executed! Custom data {}, Instance {}", inst.getData(), inst.getId());
  });

This task definition accepts some data that we provide when we schedule the task, and can act on that data as appropriate. In this case, the data is a String, but it can be any type that db-scheduler is able to serialize. By default, this uses Java Serialization but db-scheduler also supports other mechanisms, such as Jackson and Gson.

We then need to register the task with our scheduler. However, we do this by passing the task to the Scheduler.create() method so that db-scheduler knows about it but doesn’t immediately start running it:

Scheduler scheduler = Scheduler.create(dataSource, task)
  .build();

At this point, we can run our task at any time using our scheduler and task descriptor:

scheduler.schedule(taskDescriptor.instance(UUID.randomUUID().toString())
  .data("Hello")
  .scheduledTo(Instant.now().plusSeconds(5))
);

Here we have to provide a unique instance ID for our task, and indicate when the task should run. We’ve also provided some data for our task to act on. The scheduler will then ensure that this task runs correctly at the desired time.

6. Dynamic Recurring Tasks

Dynamic recurring tasks are tasks that are registered dynamically at runtime, similar to our one-time tasks. However, once registered, they’ll continue to run on a schedule similar to our simple recurring tasks.

We create and start these exactly the same as for our one-time task, only using Tasks.recurring() instead:

TaskDescriptor<String> taskDescriptor = TaskDescriptor.of("my-dynamic-recurring-task", String.class);

Task<String> task = Tasks.recurring(taskDescriptor, new CronSchedule("*/5 * * * * ?", ZoneId.of("UTC")))
  .execute((inst, ctx) -> {
    LOG.info("Executed! Custom data {}, Instance {}", inst.getData(), inst.getId());
  });

scheduler.schedule(taskDescriptor.instance(UUID.randomUUID().toString())
  .data("Hello")
  .scheduledTo(Instant.now().plusSeconds(15))
);

This will now start our task at the given time and then, once started, it’ll run at the given schedule. In this case, it first runs 15 seconds after the current time, and then every 5 seconds after that.

If we wish, we can schedule as many different instances of these as we need, and they’ll all run on their own schedule without interfering with each other.

7. Use With Spring Boot

If we’re using Spring Boot, db-scheduler provides a starter dependency that we can use:

<dependency>
    <groupId>com.github.kagkarlsson</groupId>
    <artifactId>db-scheduler-spring-boot-4-starter</artifactId>
    <version>16.12.0</version>
</dependency>

If we’re using Spring Boot 4.x then we need to use db-scheduler-spring-boot-4-starter. If we’re still on Spring Boot 3.x then instead it’s db-scheduler-spring-boot-starter.

This will automatically create and start our Scheduler instance as a Spring bean. On doing this, it’ll also automatically discover any tasks that have been created as Spring beans and register them with this scheduler. This means that the only thing we need to do is create the tasks themselves.

If we want to configure the Scheduler in any way, we can do this using properties in our standard way:

db-scheduler.enabled=true
db-scheduler.polling-interval=5s
db-scheduler.heartbeat-interval=2m
db-scheduler.missed-heartbeats-limit=10
db-scheduler.threads=5

Everything else about using db-scheduler, including how we create and schedule tasks, is exactly the same as before. The starter just makes it simpler to get started in the first place.

8. Conclusion

In this article, we took a very quick look at db-scheduler, including how to schedule recurring and one-time tasks. There’s a lot more that we can do with this. Next time we need to manage scheduled jobs for your applications, why not give it a try?

As always, all the code from this article is available over on GitHub.

Baeldung Pro – NPI EA (cat = Baeldung)
announcement - icon

Baeldung Pro comes with both absolutely No-Ads as well as finally with Dark Mode, for a clean learning experience:

>> Explore a clean Baeldung

eBook – HTTP Client – NPI EA (cat=HTTP Client-Side)
announcement - icon

The Apache HTTP Client is a very robust library, suitable for both simple and advanced use cases when testing HTTP endpoints. Check out our guide covering basic request and response handling, as well as security, cookies, timeouts, and more:

>> Download the eBook

eBook – Java Concurrency – NPI EA (cat=Java Concurrency)
announcement - icon

Handling concurrency in an application can be a tricky process with many potential pitfalls. A solid grasp of the fundamentals will go a long way to help minimize these issues.

Get started with understanding multi-threaded applications with our Java Concurrency guide:

>> Download the eBook

eBook – Java Streams – NPI EA (cat=Java Streams)
announcement - icon

Since its introduction in Java 8, the Stream API has become a staple of Java development. The basic operations like iterating, filtering, mapping sequences of elements are deceptively simple to use.

But these can also be overused and fall into some common pitfalls.

To get a better understanding on how Streams work and how to combine them with other language features, check out our guide to Java Streams:

>> Join Pro and download the eBook

eBook – Persistence – NPI EA (cat=Persistence)
announcement - icon

Working on getting your persistence layer right with Spring?

Explore the eBook

Course – LS – NPI EA (cat=REST)

announcement - icon

Get started with Spring Boot and with core Spring, through the Learn Spring course:

>> CHECK OUT THE COURSE

Partner – Moderne – NPI EA (tag=Refactoring)
announcement - icon

Modern Java teams move fast — but codebases don’t always keep up. Frameworks change, dependencies drift, and tech debt builds until it starts to drag on delivery. OpenRewrite was built to fix that: an open-source refactoring engine that automates repetitive code changes while keeping developer intent intact.

The monthly training series, led by the creators and maintainers of OpenRewrite at Moderne, walks through real-world migrations and modernization patterns. Whether you’re new to recipes or ready to write your own, you’ll learn practical ways to refactor safely and at scale.

If you’ve ever wished refactoring felt as natural — and as fast — as writing code, this is a good place to start.

eBook Jackson – NPI EA – 3 (cat = Jackson)
guest
0 Comments
Oldest
Newest
Inline Feedbacks
View all comments