[BAEL-4639] Running background jobs in Spring with JobRunr (#10089)

* [BAEL-4639] Running background jobs in Spring with JobRunr

* [BAEL-4639] Background jobs in Spring with JobRunr - update dependencies and readme

* [BAEL-4639] Background jobs in Spring with JobRunr - add required newline to application.properties

* [BAEL-4639] Background jobs in Spring with JobRunr - Make sure link is correct

* [BAEL-4639] Background jobs in Spring with JobRunr - Cleanup and LiveTest added

* [BAEL-4639] - Feedback

* [BAEL-4639] Update test with feedback
This commit is contained in:
Ronald Dehuysser
2020-09-26 23:14:11 +02:00
committed by GitHub
parent 7c8b656a7d
commit 70203a988b
8 changed files with 218 additions and 0 deletions
@@ -0,0 +1,37 @@
package com.baeldung.jobrunr;
import com.baeldung.jobrunr.service.SampleJobService;
import org.jobrunr.jobs.mappers.JobMapper;
import org.jobrunr.scheduling.JobScheduler;
import org.jobrunr.scheduling.cron.Cron;
import org.jobrunr.storage.InMemoryStorageProvider;
import org.jobrunr.storage.StorageProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import javax.annotation.PostConstruct;
@SpringBootApplication
public class JobRunrSpringBootApp {
@Autowired
private JobScheduler jobScheduler;
public static void main(String[] args) {
SpringApplication.run(JobRunrSpringBootApp.class, args);
}
@Bean
public StorageProvider storageProvider(JobMapper jobMapper) {
InMemoryStorageProvider storageProvider = new InMemoryStorageProvider();
storageProvider.setJobMapper(jobMapper);
return storageProvider;
}
@PostConstruct
public void scheduleRecurrently() {
jobScheduler.<SampleJobService>scheduleRecurrently(x -> x.executeSampleJob("a recurring job"), Cron.every5minutes());
}
}
@@ -0,0 +1,43 @@
package com.baeldung.jobrunr.controller;
import com.baeldung.jobrunr.service.SampleJobService;
import org.jobrunr.scheduling.JobScheduler;
import org.springframework.boot.context.properties.bind.DefaultValue;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.time.LocalDateTime;
@RestController
@RequestMapping("/jobrunr")
public class JobRunrController {
private JobScheduler jobScheduler;
private SampleJobService sampleJobService;
private JobRunrController(JobScheduler jobScheduler, SampleJobService sampleJobService) {
this.jobScheduler = jobScheduler;
this.sampleJobService = sampleJobService;
}
@GetMapping(value = "/enqueue/{input}", produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<String> enqueue(@PathVariable("input") @DefaultValue("default-input") String input) {
jobScheduler.enqueue(() -> sampleJobService.executeSampleJob(input));
return okResponse("job enqueued successfully");
}
@GetMapping(value = "/schedule/{input}", produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<String> schedule(
@PathVariable("input") @DefaultValue("default-input") String input,
@RequestParam("scheduleAt") @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime scheduleAt) {
jobScheduler.schedule(() -> sampleJobService.executeSampleJob(input), scheduleAt);
return okResponse("job scheduled successfully");
}
private ResponseEntity<String> okResponse(String feedback) {
return new ResponseEntity<>(feedback, HttpStatus.OK);
}
}
@@ -0,0 +1,36 @@
package com.baeldung.jobrunr.service;
import org.jobrunr.jobs.annotations.Job;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import java.util.concurrent.atomic.AtomicInteger;
@Service
public class SampleJobService {
public static final long EXECUTION_TIME = 5000L;
private Logger logger = LoggerFactory.getLogger(getClass());
private AtomicInteger count = new AtomicInteger();
@Job(name = "The sample job with variable %0", retries = 2)
public void executeSampleJob(String variable) {
logger.info("The sample job has begun. The variable you passed is {}", variable);
try {
Thread.sleep(EXECUTION_TIME);
} catch (InterruptedException e) {
logger.error("Error while executing sample job", e);
} finally {
count.incrementAndGet();
logger.info("Sample job has finished...");
}
}
public int getNumberOfInvocations() {
return count.get();
}
}