Skip to content

A lightweight, JPA-based task scheduling solution designed for Spring Boot applications

License

Notifications You must be signed in to change notification settings

sterlp/spring-persistent-tasks

Repository files navigation

Java CI with Maven Spring Boot License

Spring Persistent Tasks

A simple task management framework designed to queue and execute asynchronous tasks with support for database persistence and a user-friendly interface. It can be used to implement scheduling patterns or outbound patterns.

Focus is the usage with spring boot and JPA.

Secondary goal is to support Poor mans Workflow

Key Features ✨

  1. JPA-Powered Persistence - Automatically persists tasks to your database
  2. Spring Boot Auto-Configuration - Simple setup with @EnableSpringPersistentTasks
  3. Clustering Support - Built-in lock management for distributed environments
  4. Type-Safe Tasks - Generic task definitions with state objects
  5. Retry Mechanisms - Automatic retry policies for failed executions
  6. Transactional Integration - Works with Spring's transaction management
  7. Queue Management - Intelligent task queuing and prioritization
  8. Different DB Supports --

Documentation

Use for more advanced doc the WIKI. The README contains a shorter how to use.

Known issues

  • spring-boot-devtools: cause java.lang.ClassCastException exceptions during the state serialization - this is a java/spring issue

Known limitations

DBMS have missing bad row lock implementation

The framework briefly locks a row or trigger to ensure that each trigger is started only on a single node when a cluster is in use. However, some databases still do not support proper row locking and instead lock the entire table. This is not a critical issue but can slow down task selection in very large clusters.

  • mySQL
  • Azure SQL Edge (maybe also MSSQL)

DBs for storage

Tested in the pipeline

  • H2
  • Azure SQL Edge
  • PostgreSQL
  • MariaDB

History

Supported in theory

  • MSSQL, as azure-sql-edge is tested

Not supported

  • mySQL: sequences are not supported

JavaDoc

Quickstart

Setup with Maven

<dependency>
    <groupId>org.sterl.spring</groupId>
    <artifactId>spring-persistent-tasks-core</artifactId>
    <version>1.x.x</version>
</dependency>

Setup Spring

@SpringBootApplication
@EnableSpringPersistentTasks
public class ExampleApplication {

Create a Task

@Bean
PersistentTask<Vehicle> task1(VehicleHttpConnector vehicleHttpConnector) {
    return v -> vehicleHttpConnector.send(v);
}

Trigger a task

@Autowired
PersistentTaskService persistentTaskService;

public void triggerTask1(Vehicle vehicle) {
    persistentTaskService.runOrQueue(
        TriggerBuilder.newTrigger("task1").state(vehicle).build());
}

JUnit Tests

Setup DB with Liquibase

Liquibase is supported. Either import all or just the required versions.

Maven

Option 1: Just include the master file

<include file="spring-persistent-tasks/db.changelog-master.xml" />

Option 2: import changesets on by one

<include file="spring-persistent-tasks/db/pt-changelog-v1.xml" />

Setup UI

Maven

<dependency>
    <groupId>org.sterl.spring</groupId>
    <artifactId>spring-persistent-tasks-ui</artifactId>
    <version>1.x.x</version>
</dependency>

Setup Spring

@SpringBootApplication
@EnableSpringPersistentTasks
@EnableSpringPersistentTasksUI
public class ExampleApplication {

Open the UI

Schedulers

Schedulers

Triggers

Triggers

History

History

Alternatives

  • quartz
  • db-scheduler
  • jobrunr