JobRunr + Spring Boot
In this article, we will discuss JobRunr. What is JobRunr? How does it work behind the scenes? We will discuss its use cases and how to integrate it with Spring Boot.
Join the DZone community and get the full member experience.
Join For FreeJobRunr
JobRunr is useful when we want to perform asynchronous tasks, and schedule/recurrently schedule a job. When we want to run multiple instances of one application and wish to schedule a task from any one server, in this case, JobRunr is useful.
JobRunr can easily integrated with new/existing applications. Simply put, when we have an asynchronous task using thread or anything else, it resides in our memory. We create multiple instances of the application for scaling purposes, it schedules tasks for each server.
Sometimes, a business requirement is to perform a job for a single server even though we have more than one server for managing load. In this situation, we can implement JobRunr.
For example, I have an online gaming application. I am required to send 1000s of people an update of the game in the background without interrupting the server. In this situation, I can schedule a job using JobRunr.
Another example could be that a welcome email should be sent as the user gets onboarded. asynchronously. We can delete existing jobs effectively using JobRunr.
JobRunr Pro provides fair use of resources and we can also set the task's priority using priority queues. It provides a dashboard to monitor jobs effortlessly. We can simply add a few properties to make the dashboard secure. We can even pass cron expression for scheduling jobs.
How Does JobRunr Work Behind the Scenes?
JobRunr enqueues/schedules or recurrently schedules jobs that are defined in code. It serializes the data in JSON format of method name, request body, etc., which will be stored in a storage provider defined by business logic.
Now JobRunr will return to the caller to check if there are any new jobs available. If yes, then save it into storage. This will prevent the blocking of the current thread.
Now, JobRunr has BackgroundJobServer
which polls the database to execute jobs. Once the job is executed, this process is repeated until there is no job to execute.
Here, the database stores job information so when we have multiple servers, it has access to the same database and if the job is scheduled by one server, its status is there. So, there is no duplication of tasks. If one job fails, then it has a retry filter which allows one to re-attempt a job even after getting failed.
Using JobRunr we can customize retry attempts which is by default 10.
Use Cases
- Fetch data from third-party applications like Salesforce at particular time intervals.
- Schedule only one-time tasks for multiple servers. And if any server goes down, jobrunr will still run the job using another server.
- Generating logs and sending them to the cloud every 30 minutes.
How To Integrate JobRunr to Spring Boot With an Example
In this application, we will enqueue/schedule and recurrently schedule a job that prints Hello World.
To start with, we first need to add the following dependency to pom.xml.
<dependency>
<groupId>org.jobrunr</groupId>
<artifactId>jobrunr-spring-boot-starter</artifactId>
<version>5.3.3</version>
</dependency>
This will add all the JobRunr-related classes to the application. We can configure JobRunr in application.properties using the properties below.
- org.jobrunr.background-job-server=true
This property will allow to schedule jobs using BackgroundJobServer
.
- org.jobrunr.dashboard.enabled=true
This property will enable the dashboard of JobRunr.
We can create a job using jobscheduler
, jobrequest
and BackgroundJob
. Here, we will use BackgroundJob
for instance,
@Service
public class JobExample{
public void enqueueExample(){
BackgroundJob.enqueue(()->System.out.println("Hello World"));
}
}
To call this, we will use API Endpoint.
@RestController
public class HelloController{
@Autowired
private JobExample jobExample;
@GetMapping("/helloWorld")
public void helloWorld(){
jobExample.enqueueExample();
}
}
This enqueue service is saved, and performed. It is like fire and forgets the job.
To implement a scheduler and recurrent jobscheulder
, we will use a Background job with time.
@Service
public class JobExample{
public void enqueueExample(){
BackgroundJob.schedule(Instant.now.plusMillis(60000),()->System.out.println("Hello World"));
}
}
We can create the same endpoint as above for scheduled jobs.
Pro Tips
- Give a name to the job when creating one. It makes it easy to track tasks.
- Put a limit on the retry of the failed job. It will help to improve CPU performance.
- Sometimes it is difficult to debug code as there are very generic logs. So you can keep debug points in class files.
To conclude, when we have asynchronous/scheduled tasks to manage. If we need to scale it in the future, then JobRunr is a very effective solution.
Opinions expressed by DZone contributors are their own.
Comments