Job Result
Do you need to get the result of a job? This is now easier than ever with JobRunr JobResults
If your job returns a result, JobRunr serializes it and stores it via the StorageProvider
in the database of your choice.
You can then fetch the result of that job using the id of the job.
void startWeatherPrediction(UUID cityId) {
Observation observation = observationService.getLatestObservation(cityId); // the original observation
BackgroundJob.enqueue(myId, () -> weatherService.predictWeather(cityId, observation));
}
WeatherPrediction getWeatherPrediction(UUID cityId) {
JobResultWithBackOffInfo jobResult = BackgroundJob.getJobResult(jobId);
if(jobResult.isAvailable()) return jobResult.getResult();
throw new BackOffException(Instant.now().plus(jobResult.backoffPeriod()));
}
How to return jobs from your Jobs?
Returning jobs from Java 8 lambdas
WeatherPrediction predictWeather(UUID cityId) {
// take input and do some heavy calculations
// then, just return the result
return new WeatherPredication(result);
}
Returning jobs from JobRequest
public class WeatherPredictionRequest implements JobRequest {
private UUID cityId;
public WeatherPredictionRequest(UUID cityId) {
this.cityId = cityId;
}
public UUID getCityId() {
return cityId;
}
@Override
public Class<WeatherPredictionRequestHandler> getJobRequestHandler() {
return WeatherPredictionRequestHandler.class;
}
}
public class WeatherPredictionRequestHandler implements JobResultRequestHandler<WeatherPredictionRequest> {
@Override
public WeatherPredication runAndReturn(WeatherPredictionRequest jobRequest) throws Exception {
// take input and do some heavy calculations
// then, just return the result
return new WeatherPredication(result);
}
}
Important: a Job can only have one result. When a
SUCCEEDED
job with a Job Result is retried, only the last result is kept.