Spring - JobRunr

Overview

Spring boot integration with JobRunr

Github: https://github.com/gitorko/project59

JobRunr

Perform fire-and-forget, delayed, scheduled and recurring jobs inside Java applications using only Java 8 lambda's

  1. It lets you schedule background jobs using lambda.
  2. The jobs can run on a distributed nodes, more node that join, the work gets distributed.
  3. It serializes the lambda as JSON and stores it in db.
  4. It also contains an automatic retry feature with an exponential back-off policy for failed jobs.
  5. There is also a built-in dashboard that allows you to monitor all jobs.
  6. It is self-maintaining, Successful jobs are automatically deleted after a configurable amount of time, so there is no need to perform manual storage cleanup.
  7. The job details are stored in db.

Code

 1package com.demo.project59;
 2
 3import java.time.LocalDateTime;
 4import java.time.format.DateTimeFormatter;
 5
 6import lombok.extern.slf4j.Slf4j;
 7import org.jobrunr.scheduling.JobScheduler;
 8import org.springframework.beans.factory.annotation.Autowired;
 9import org.springframework.http.ResponseEntity;
10import org.springframework.web.bind.annotation.GetMapping;
11import org.springframework.web.bind.annotation.PathVariable;
12import org.springframework.web.bind.annotation.RestController;
13
14@RestController
15@Slf4j
16public class HomeController {
17
18    @Autowired
19    private JobScheduler jobScheduler;
20
21    @Autowired
22    private AppService appService;
23
24    DateTimeFormatter format = DateTimeFormatter.ofPattern("ddMMyyyyHHmmss");
25
26    @GetMapping("/api/{number}")
27    public ResponseEntity isPrime(@PathVariable int number) {
28        for (int i = 0; i < number; i++) {
29            String jobName = "job_" + format.format(LocalDateTime.now());
30            jobScheduler.enqueue(() -> appService.doWork1(jobName));
31            jobScheduler.enqueue(() -> appService.doWork2(jobName));
32        }
33        log.info("Job submitted!");
34        return ResponseEntity.ok().build();
35    }
36}
37
 1package com.demo.project59;
 2
 3import lombok.extern.slf4j.Slf4j;
 4import org.jobrunr.jobs.annotations.Job;
 5import org.springframework.stereotype.Component;
 6
 7@Component
 8@Slf4j
 9public class AppService {
10
11    public void doWork1(String job) {
12        log.info("doWork1: {}", job);
13    }
14
15    @Job(name = "Work2 job name: %0", retries = 2)
16    public void doWork2(String job) {
17        log.info("doWork2: {}", job);
18    }
19
20    @Job(name = "Cron job name: %0", retries = 0)
21    public void doWork3(String job) {
22        log.info("doWork3: {}", job);
23    }
24}

Open dashboard: http://localhost:8000/dashboard/

Setup

Project 59

Spring Boot JobRunr

https://gitorko.github.io/spring-jobrunr/

Version

Check version

1$java --version
2openjdk 17.0.3 2022-04-19 LTS

Postgres DB

1docker run -p 5432:5432 --name pg-container -e POSTGRES_PASSWORD=password -d postgres:9.6.10
2docker ps
3docker exec -it pg-container psql -U postgres -W postgres
4CREATE USER test WITH PASSWORD 'test@123';
5CREATE DATABASE "test-db" WITH OWNER "test" ENCODING UTF8 TEMPLATE template0;
6grant all PRIVILEGES ON DATABASE "test-db" to test;
7
8docker stop pg-container
9docker start pg-container

Dev

To run the code

1./gradlew clean build
2./gradlew bootRun
1curl --location --request GET 'http://localhost:8080/api/500'

References

https://www.jobrunr.io/en/

comments powered by Disqus