149

Introducing db-scheduler — a simple cluster-friendly scheduler for Java

 6 years ago
source link: https://blogg.bekk.no/introducing-db-scheduler-a-simple-cluster-friendly-scheduler-for-java-f880993399c8?gi=98c82c110053
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Introducing db-scheduler — a simple cluster-friendly scheduler for Java

1*oYyqg-noZEiIV_ZJOJJ08A.jpeg

Working a lot with Java “mini-services”, I have often found the need for some sort of job scheduler that can be run on multiple nodes for redundancy. It may be as simple as a recurring job for sending unsent emails in an email-table. It is not a difficult problem, but looking at the open-source solutions out there, I never really found one that hits the right balance between functionality and simplicity. Therefore I decided to package my ideas into a library of my own: db-scheduler.

Its main features are:

  • Supports both simple recurring jobs and ad-hoc jobs scheduled at runtime.
  • Cluster-friendly. Optimistic locking is used to ensure execution by a single node in the cluster.
  • Persistent. A single database table is used for the scheduled instances of jobs.
  • Simple and embeddable, meant to be embedded in an existing application. Has a single dependency on slf4j.

So, let’s look at some examples. And to avoid confusion, a job in db-scheduler is called a task, and a scheduled instance of a task is called an execution.

Setting up a scheduler with a recurring task is simple. First we set up a Task-class with an hourly schedule:

A RecurringTask is rescheduled according to its Schedule after every execution

Then we create and start a scheduler instance, instructing it to schedule the recurring task:

After every execution, the task’s schedule will be consulted to decide the next execution-time.

The other type of common tasks are ad-hoc tasks, which is scheduled at runtime and typically only have a single future execution. State relevant to the execution can be stored with the execution (for a full example, see github):

A OneTimeTask has a single execution

… and that’s basically it. Some more examples can be found on github, along with documentation on its inner workings.

If you try it out and feel some feature is missing, don’t hesitate to add a feature request on Github :)


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK