Monday, January 27, 2025

Running Jobs with Jobrunr

Scheduling Distributed Jobs with JobRunr in Java

In modern application development, scheduling jobs in a distributed and scalable manner is a critical requirement. JobRunr, a modern framework for job scheduling, provides an elegant and powerful solution for scheduling background jobs in a distributed pattern using Java.

In this blog, we’ll walk through a simple use case to demonstrate how you can schedule a job to run after a specified time using JobRunr.

Setting Up JobRunr

To get started, include the necessary dependencies in your pom.xml file. Below are the Maven dependencies required for JobRunr and its integration with Jackson for serialization.

<dependency>

    <groupId>org.jobrunr</groupId>

    <artifactId>jobrunr</artifactId>

    <version>7.3.1</version>

</dependency>

<dependency>

    <groupId>com.fasterxml.jackson.core</groupId>

    <artifactId>jackson-databind</artifactId>

    <version>2.11.0</version>

</dependency>

<dependency>

    <groupId>com.fasterxml.jackson.core</groupId>

    <artifactId>jackson-core</artifactId>

    <version>2.11.1</version>

</dependency>

Example Use Case: Scheduling a Delayed Job

In this example, we’ll configure JobRunr to schedule a job that executes after a 60-second delay. The job will simply print a message to the console.

Here’s the sample code:

import org.jobrunr.configuration.JobRunr;

import org.jobrunr.scheduling.JobScheduler;

import org.jobrunr.storage.InMemoryStorageProvider;


import java.time.Instant;


public class JobRunrExample {

    public static void main(String[] args) {

        // Configure JobRunr with an in-memory storage provider

        JobScheduler jobScheduler = JobRunr.configure()

                .useStorageProvider(new InMemoryStorageProvider())

                .useBackgroundJobServer()

                .initialize()

                .getJobScheduler();


        // Schedule a job to run after 60 seconds

        jobScheduler.schedule(Instant.now().plusSeconds(60), 

            () -> System.out.println("Hello!"));

    }

}

Explanation

  1. Dependency Configuration:
    1. The jobrunr dependency provides the core functionality for scheduling jobs.
    2. The jackson-databind and jackson-core dependencies handle serialization of job parameters, enabling JobRunr to store and execute jobs across distributed servers.
  2. Initialization:
    1. The JobRunr.configure() method sets up JobRunr with an in-memory storage provider.
    2. In a production environment, you can replace the in-memory storage provider with a persistent provider such as SQL databases for scalability.
  3. Scheduling the Job:
    1. The schedule() method is used to set the execution time for the job.
    2. The job logic, in this case, is a simple System.out.println statement that prints "Hello!" after 60 seconds.

Conclusion

JobRunr simplifies the process of scheduling background tasks in Java applications, making it a great choice for distributed systems. This framework not only supports delayed job execution but also offers features like retry mechanisms, distributed execution, and integration with popular storage solutions.


Running Jobs with Jobrunr

Scheduling Distributed Jobs with JobRunr in Java In modern application development, scheduling jobs in a distributed and scalable manner is ...