Class JobBuilder

java.lang.Object
org.jobrunr.scheduling.JobBuilder

public class JobBuilder extends Object
This class is used to build a Job using a job lambda or a .

You can use it as follows:

A job lambda example:

            MyService service = new MyService();
            jobScheduler.create(aJob()
                                  .withId(UUID.fromString(idRequestParam))
                                  .withAmountOfRetries(3)
                                  .scheduleIn(Duration.ofSeconds(10))
                                  .withDetails(() -> service.sendMail(toRequestParam, subjectRequestParam, bodyRequestParam));
       
A JobRequest example:

          jobRequestScheduler.create(aJob()
                                .withId(UUID.fromString(idRequestParam))
                                .withAmountOfRetries(3)
                                .scheduleIn(Duration.ofSeconds(10))
                                .withJobRequest(new SendMailRequest(toRequestParam, subjectRequestParam, bodyRequestParam));
     
  • Method Details

    • aJob

      public static JobBuilder aJob()
      Creates a new JobBuilder instance to create a Job using a Java 8 lambda.
      Returns:
      a new JobBuilder instance
    • withId

      public JobBuilder withId(UUID jobId)
      Allows to set the id of the job. If a job with that id already exists, JobRunr will not save it again.
      Parameters:
      jobId - the id of the job
      Returns:
      the same builder instance which provides a fluent api
    • withName

      public JobBuilder withName(String jobName)
      Allows to set the name of the job for the dashboard.
      Parameters:
      jobName - the name of the job for the dashboard
      Returns:
      the same builder instance which provides a fluent api
    • scheduleIn

      public JobBuilder scheduleIn(Duration duration)
      Allows to specify the duration after which the job should be enqueued.
      Parameters:
      duration - the duration after which the job should be enqueued
      Returns:
      the same builder instance which provides a fluent api
    • scheduleAt

      public JobBuilder scheduleAt(Instant scheduleAt)
      Allows to specify the instant on which the job will be enqueued.
      Parameters:
      scheduleAt - the instant on which the job will be enqueued
      Returns:
      the same builder instance which provides a fluent api
    • withAmountOfRetries

      public JobBuilder withAmountOfRetries(int amountOfRetries)
      Allows to specify the amount of retries for a job when it fails
      Parameters:
      amountOfRetries - the amount of retries that JobRunr will perform in case the job fails
      Returns:
      the same builder instance which provides a fluent api
    • withLabels

      public JobBuilder withLabels(String... labels)
      Allows to provide a set of labels to be shown in the dashboard. A maximum of 3 labels can be provided per job. Each label has a max length of 45 characters.
      Parameters:
      labels - an array of labels to be added to the recurring job
      Returns:
      the same builder instance which provides a fluent api
    • withLabels

      public JobBuilder withLabels(Set<String> labels)
      Allows to provide a set of labels to be shown in the dashboard. A maximum of 3 labels can be provided per job. Each label has a max length of 45 characters.
      Parameters:
      labels - an array of labels to be added to the recurring job
      Returns:
      the same builder instance which provides a fluent api
    • withDetails

      public JobBuilder withDetails(JobLambda jobLambda)
      Allows to provide the job details by means of Java 8 lambda.
      Parameters:
      jobLambda - the lambda which defines the job
      Returns:
      the same builder instance that can be given to the JobScheduler.create(JobBuilder) method
    • withDetails

      public <S> JobBuilder withDetails(IocJobLambda<S> jobLambda)
      Allows to provide the job details by means of Java 8 lambda. The IoC container will be used to resolve an actual instance of the requested service.
      Parameters:
      jobLambda - the lambda which defines the job
      Returns:
      the same builder instance that can be given to the JobScheduler.create(JobBuilder) method
    • withJobRequest

      public JobBuilder withJobRequest(JobRequest jobRequest)
      Allows to provide the job details by means of JobRequest.
      Parameters:
      jobRequest - the jobRequest which defines the job.
      Returns:
      the same builder instance that can be given to the JobRequestScheduler.create(JobBuilder) method
    • build

      protected Job build(JobDetailsGenerator jobDetailsGenerator)
      Not publicly visible as it will be used by the JobScheduler only.
      Returns:
      the actual Job to create
    • build

      protected Job build()
      Not publicly visible as it will be used by the JobRequestScheduler only.
      Returns:
      the actual Job to create