undo
Go Beyond the Code
arrow_forward_ios

Integrating JobRunr into a Java Spring Boot App

July 22, 2024

Job scheduling is one of the core functionalities that is needed in modern, enterprise-scale applications. In this context, Quartz and and JobRunr are two mature solutions on the top of Java backend implementations - as we have in several of our projects. However, while Quartz is a well-proven and mature solution, JobRunr is a more modern framework and provides several advantages in comparison to Quartz. For that reason, we have decided to migrate some of our existing job scheduling functionalities to JobRunr, since it simplifies several aspects of our daily needs. This article outlines the migration process, highlighting the setup, migration steps, and deactivation of Quartz, ensuring a seamless transition and improved application performance.

Motivation

In more concrete terms, the main motivation of this migration was to simplify job scheduling, improve performance, and provide developers with a more powerful and flexible tool for managing background tasks. Additionally, JobRunr introduces the concept of "requested" jobs, which significantly enhances the flexibility of job scheduling. Unlike Quartz, JobRunr allows for the manual queuing of jobs, enabling developers to schedule tasks dynamically based on specific conditions, thus avoiding the unnecessary execution of jobs at fixed intervals as required by Quartz. This means that instead of running jobs on a scheduled basis regardless of necessity, jobs can be queued and executed only when certain conditions are met, reducing resource usage and improving efficiency.

Let's jump into the particular steps required to perform the migration from the implementation perspective.

Solution

Setting Up JobRunr

1. Dependency Management
Add the necessary JobRunr dependencies to the pom.xml file to include the JobRunr Spring Boot starter

2. Configuration
Configure JobRunr settings in the application.properties file. In this case, we enable the server and we set the polling to 1 second. This will mean that a query will be run against the DB to look fo rescheduled jobs.

3. Job Definition
Define a requested job using JobRunr’s API:

Introducing a Scalable Approach

The code snippet above demonstrates the simplicity and ease of queuing a task using JobRunr within a Spring Boot application. However, in larger applications, this approach can become cumbersome as the complexity and number of jobs increase. To address this, we can adopt a more scalable method for defining requested jobs by handling the logic in separate files.

1. Request Definition
First, we define our DoSomething request. This will provide the necessary parameters to execute the instruction later. The class implements the JobRequest interface provided by JobRunr, which links the handler that we will implement in the next step:

2. Handler Definition
It is time to declare the handler, which contains the instruction to be executed as well as the dependencies necessary to execute it. The class implements the JobRequestHandler interface provided by JobRunr and receives the request class we defined above:

3. Job Implementation
Finally, we can execute it anywhere in our code, queuing or scheduling the execution:

Migrating from Quartz

Now, let's describe how we migrate from Quartz to Jobrunr

1. Deactivating Quartz
Disable Quartz configurations in the application.properties file to deactivate Quartz:

2. Removing Quartz Jobs
Remove existing Quartz job definitions and schedules from the application to prevent conflicts with JobRunr - this means basically removing classes.

3. Rewriting Jobs
Rewrite existing Quartz jobs using JobRunnr’s API. Here is an example of how a typical Quartz job can be migrated to JobRunr. Before:

After:


Conclusion

Migrating from Quartz to JobRunr in a Java Spring Boot application enhances job scheduling capabilities and simplifies management. This process involves setting up JobRunr, deactivating Quartz, and rewriting jobs using JobRunnr’s API. The transition ensures improved performance, scalability, and a more intuitive approach to managing background tasks, making JobRunr a valuable addition to any developer's toolkit.

Matias Appella
Software Engineer & Solver

Start Your Digital Journey Now!

Which capabilities are you interested in?
You may select more than one.
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.