Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Hyeon-moGu/VtScheduler

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

VtScheduler: Lightweight Java Virtual Thread Scheduler (JDK 21+)

VtScheduler is an in-memory scheduler built using Java Virtual Threads (JDK 21+). We designed it to be a simple, dependency-free solution for managing periodic or background tasks without the need for complex frameworks, databases, or external services.

Key Features

  • Virtual Thread–Powered: This is our core focus. Leveraging the "blocking is cheap" principle of Virtual Threads, the scheduler can handle a high volume of concurrent tasks with minimal resource usage, especially in I/O-bound scenarios.
  • Simple, Pure Java API: Provides clean methods like schedule(), scheduleAtFixedRate(), and scheduleWithDelay() for effortless integration.
  • CronTrigger Support: Easily set flexible schedules using standard six-field Cron expressions (e.g. 0 15 * * * *).
  • Zero Dependencies: It's pure Java, making it easy to embed into any JVM application.
  • Graceful Shutdown: Safely waits for ongoing tasks to complete before stopping, minimizing the risk of half-finished operations.

Example

import java.time.Duration;

var scheduler = VtScheduler.create()
        .onError((jobId, error) -> {
            // Handle exceptions that occur during job execution
            System.err.printf("[Error] Job %s failed: %s%n", jobId, error.getMessage());
        });

// Schedules a Cron job to run every day at 3:00 PM
scheduler.schedule(() ->
        System.out.println("Cron job executed."),
        new CronTrigger("0 15 * * * *")); 

// Schedules a task to run repeatedly every 5 seconds
scheduler.scheduleAtFixedRate(() ->
        System.out.println("Heartbeat pulse"),
        Duration.ofSeconds(5));

Graceful Shutdown

When shutting down, VtScheduler:

  1. Stops accepting new jobs
  2. Waits up to 10 seconds for running jobs to complete
  3. Cancels any jobs that do not finish in time
  4. Reports any timeouts through the onError() handler

This ensures ongoing tasks can finish cleanly before the scheduler stops.


Notes

  • Internally uses DelayQueue for precise timing
  • All jobs run on virtual threads, making blocking operations inexpensive
  • Runs entirely in-memory — no database or persistence layer

About

Lightweight in-memory scheduler built with Java Virtual Threads

Topics

Resources

License

Stars

Watchers

Forks

Languages