Class BackgroundJob


  • public class BackgroundJob
    extends java.lang.Object
    Provides static methods for creating fire-and-forget, delayed and recurring jobs as well as to delete existing background jobs.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static void deleteRecurringly​(java.lang.String id)
      Deletes the recurring job based on the given id.
      static <TService,​TItem>
      void
      enqueue​(java.util.stream.Stream<TItem> input, IocJobLambdaFromStream<TService,​TItem> iocJobFromStream)
      Creates new fire-and-forget jobs for each item in the input stream using the lambda passed as jobFromStream.
      static <TItem> void enqueue​(java.util.stream.Stream<TItem> input, JobLambdaFromStream<TItem> jobFromStream)
      Creates new fire-and-forget jobs for each item in the input stream using the lambda passed as jobFromStream.
      static <TService> java.util.UUID enqueue​(IocJobLambda<TService> iocJob)
      Creates a new fire-and-forget job based on a given lambda.
      static java.util.UUID enqueue​(JobLambda job)
      Creates a new fire-and-forget job based on a given lambda.
      static <TService> java.util.UUID schedule​(IocJobLambda<TService> iocJob, java.time.Instant instant)
      Creates a new fire-and-forget job based on the given lambda and schedules it to be enqueued at the given moment of time.
      static <TService> java.util.UUID schedule​(IocJobLambda<TService> iocJob, java.time.LocalDateTime localDateTime)
      Creates a new fire-and-forget job based on the given lambda and schedules it to be enqueued at the given moment of time.
      static <TService> java.util.UUID schedule​(IocJobLambda<TService> iocJob, java.time.OffsetDateTime offsetDateTime)
      Creates a new fire-and-forget job based on the given lambda and schedules it to be enqueued at the given moment of time.
      static <TService> java.util.UUID schedule​(IocJobLambda<TService> iocJob, java.time.ZonedDateTime zonedDateTime)
      Creates a new fire-and-forget job based on the given lambda and schedules it to be enqueued at the given moment of time.
      static java.util.UUID schedule​(JobLambda job, java.time.Instant instant)
      Creates a new fire-and-forget job based on the given lambda and schedules it to be enqueued at the given moment of time.
      static java.util.UUID schedule​(JobLambda job, java.time.LocalDateTime localDateTime)
      Creates a new fire-and-forget job based on the given lambda and schedules it to be enqueued at the given moment of time.
      static java.util.UUID schedule​(JobLambda job, java.time.OffsetDateTime offsetDateTime)
      Creates a new fire-and-forget job based on the given lambda and schedules it to be enqueued at the given moment of time.
      static java.util.UUID schedule​(JobLambda job, java.time.ZonedDateTime zonedDateTime)
      Creates a new fire-and-forget job based on the given lambda and schedules it to be enqueued at the given moment of time.
      static <TService> java.lang.String scheduleRecurringly​(java.lang.String id, IocJobLambda<TService> iocJob, java.lang.String cron)
      Creates a new recurring job based on the given id, lambda and cron expression.
      static <TService> java.lang.String scheduleRecurringly​(java.lang.String id, IocJobLambda<TService> iocJob, java.lang.String cron, java.time.ZoneId zoneId)
      Creates a new recurring job based on the given id, lambda, cron expression and ZoneId.
      static java.lang.String scheduleRecurringly​(java.lang.String id, JobLambda job, java.lang.String cron)
      Creates a new recurring job based on the given id, lambda and cron expression.
      static java.lang.String scheduleRecurringly​(java.lang.String id, JobLambda job, java.lang.String cron, java.time.ZoneId zoneId)
      Creates a new recurring job based on the given id, lambda, cron expression and ZoneId.
      static <TService> java.lang.String scheduleRecurringly​(IocJobLambda<TService> iocJob, java.lang.String cron)
      Creates a new recurring job based on the given lambda and the given cron expression.
      static java.lang.String scheduleRecurringly​(JobLambda job, java.lang.String cron)
      Creates a new recurring job based on the given lambda and the given cron expression.
      static void setJobScheduler​(JobScheduler jobScheduler)  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • enqueue

        public static java.util.UUID enqueue​(JobLambda job)
        Creates a new fire-and-forget job based on a given lambda.
        An example:
        
                    MyService service = new MyService();
                    BackgroundJob.enqueue(() -> service.doWork());
               
        Parameters:
        job - the lambda which defines the fire-and-forget job
        Returns:
        the id of the job
      • enqueue

        public static <TItem> void enqueue​(java.util.stream.Stream<TItem> input,
                                           JobLambdaFromStream<TItem> jobFromStream)
        Creates new fire-and-forget jobs for each item in the input stream using the lambda passed as jobFromStream.
        An example:
        
              MyService service = new MyService();
              Stream<UUID> workStream = getWorkStream();
              BackgroundJob.enqueue(workStream, (uuid) -> service.doWork(uuid));
         
        Parameters:
        input - the stream of items for which to create fire-and-forget jobs
        jobFromStream - the lambda which defines the fire-and-forget job to create for each item in the input
      • enqueue

        public static <TService> java.util.UUID enqueue​(IocJobLambda<TService> iocJob)
        Creates a new fire-and-forget job based on a given lambda. The IoC container will be used to resolve MyService.
        An example:
        
                    BackgroundJob.<MyService>enqueue(x -> x.doWork());
               
        Parameters:
        iocJob - the lambda which defines the fire-and-forget job
        Returns:
        the id of the job
      • enqueue

        public static <TService,​TItem> void enqueue​(java.util.stream.Stream<TItem> input,
                                                          IocJobLambdaFromStream<TService,​TItem> iocJobFromStream)
        Creates new fire-and-forget jobs for each item in the input stream using the lambda passed as jobFromStream. The IoC container will be used to resolve MyService.
        An example:
        
              Stream<UUID> workStream = getWorkStream();
              BackgroundJob.<MyService, UUID>enqueue(workStream, (x, uuid) -> x.doWork(uuid));
         
        Parameters:
        input - the stream of items for which to create fire-and-forget jobs
        iocJobFromStream - the lambda which defines the fire-and-forget job to create for each item in the input
      • schedule

        public static java.util.UUID schedule​(JobLambda job,
                                              java.time.ZonedDateTime zonedDateTime)
        Creates a new fire-and-forget job based on the given lambda and schedules it to be enqueued at the given moment of time.
        An example:
        
              MyService service = new MyService();
              BackgroundJob.schedule(() -> service.doWork(), ZonedDateTime.now().plusHours(5));
         
        Parameters:
        job - the lambda which defines the fire-and-forget job
        zonedDateTime - The moment in time at which the job will be enqueued.
      • schedule

        public static <TService> java.util.UUID schedule​(IocJobLambda<TService> iocJob,
                                                         java.time.ZonedDateTime zonedDateTime)
        Creates a new fire-and-forget job based on the given lambda and schedules it to be enqueued at the given moment of time. The IoC container will be used to resolve MyService.
        An example:
        
              BackgroundJob.<MyService>schedule(x -> x.doWork(), ZonedDateTime.now().plusHours(5));
         
        Parameters:
        iocJob - the lambda which defines the fire-and-forget job
        zonedDateTime - The moment in time at which the job will be enqueued.
      • schedule

        public static java.util.UUID schedule​(JobLambda job,
                                              java.time.OffsetDateTime offsetDateTime)
        Creates a new fire-and-forget job based on the given lambda and schedules it to be enqueued at the given moment of time.
        An example:
        
              MyService service = new MyService();
              BackgroundJob.schedule(() -> service.doWork(), OffsetDateTime.now().plusHours(5));
         
        Parameters:
        job - the lambda which defines the fire-and-forget job
        offsetDateTime - The moment in time at which the job will be enqueued.
      • schedule

        public static <TService> java.util.UUID schedule​(IocJobLambda<TService> iocJob,
                                                         java.time.OffsetDateTime offsetDateTime)
        Creates a new fire-and-forget job based on the given lambda and schedules it to be enqueued at the given moment of time. The IoC container will be used to resolve MyService.
        An example:
        
              BackgroundJob.<MyService>schedule(x -> x.doWork(), OffsetDateTime.now().plusHours(5));
         
        Parameters:
        iocJob - the lambda which defines the fire-and-forget job
        offsetDateTime - The moment in time at which the job will be enqueued.
      • schedule

        public static java.util.UUID schedule​(JobLambda job,
                                              java.time.LocalDateTime localDateTime)
        Creates a new fire-and-forget job based on the given lambda and schedules it to be enqueued at the given moment of time.
        An example:
        
              MyService service = new MyService();
              BackgroundJob.schedule(() -> service.doWork(), LocalDateTime.now().plusHours(5));
         
        Parameters:
        job - the lambda which defines the fire-and-forget job
        localDateTime - The moment in time at which the job will be enqueued. It will use the systemDefault ZoneId to transform it to an UTC Instant
      • schedule

        public static <TService> java.util.UUID schedule​(IocJobLambda<TService> iocJob,
                                                         java.time.LocalDateTime localDateTime)
        Creates a new fire-and-forget job based on the given lambda and schedules it to be enqueued at the given moment of time. The IoC container will be used to resolve MyService.
        An example:
        
              BackgroundJob.<MyService>schedule(x -> x.doWork(), LocalDateTime.now().plusHours(5));
         
        Parameters:
        iocJob - the lambda which defines the fire-and-forget job
        localDateTime - The moment in time at which the job will be enqueued. It will use the systemDefault ZoneId to transform it to an UTC Instant
      • schedule

        public static java.util.UUID schedule​(JobLambda job,
                                              java.time.Instant instant)
        Creates a new fire-and-forget job based on the given lambda and schedules it to be enqueued at the given moment of time.
        An example:
        
              MyService service = new MyService();
              BackgroundJob.schedule(() -> service.doWork(), Instant.now().plusHours(5));
         
        Parameters:
        job - the lambda which defines the fire-and-forget job
        instant - The moment in time at which the job will be enqueued.
      • schedule

        public static <TService> java.util.UUID schedule​(IocJobLambda<TService> iocJob,
                                                         java.time.Instant instant)
        Creates a new fire-and-forget job based on the given lambda and schedules it to be enqueued at the given moment of time. The IoC container will be used to resolve MyService.
        An example:
        
              BackgroundJob.<MyService>schedule(x -> x.doWork(), Instant.now().plusHours(5));
         
        Parameters:
        iocJob - the lambda which defines the fire-and-forget job
        instant - The moment in time at which the job will be enqueued.
      • scheduleRecurringly

        public static java.lang.String scheduleRecurringly​(JobLambda job,
                                                           java.lang.String cron)
        Creates a new recurring job based on the given lambda and the given cron expression. The jobs will be scheduled using the systemDefault timezone.
        An example:
        
              MyService service = new MyService();
              BackgroundJob.scheduleRecurringly(() -> service.doWork(), Cron.daily());
         
        Parameters:
        job - the lambda which defines the fire-and-forget job
        cron - The cron expression defining when to run this recurring job
        Returns:
        the id of this recurring job which can be used to alter or delete it
        See Also:
        Cron
      • scheduleRecurringly

        public static <TService> java.lang.String scheduleRecurringly​(IocJobLambda<TService> iocJob,
                                                                      java.lang.String cron)
        Creates a new recurring job based on the given lambda and the given cron expression. The IoC container will be used to resolve MyService. The jobs will be scheduled using the systemDefault timezone.
        An example:
        
              BackgroundJob.<MyService>scheduleRecurringly(x -> x.doWork(), Cron.daily());
         
        Parameters:
        iocJob - the lambda which defines the fire-and-forget job
        cron - The cron expression defining when to run this recurring job
        Returns:
        the id of this recurring job which can be used to alter or delete it
        See Also:
        Cron
      • scheduleRecurringly

        public static java.lang.String scheduleRecurringly​(java.lang.String id,
                                                           JobLambda job,
                                                           java.lang.String cron)
        Creates a new recurring job based on the given id, lambda and cron expression. The jobs will be scheduled using the systemDefault timezone
        An example:
        
              MyService service = new MyService();
              BackgroundJob.scheduleRecurringly("my-recurring-job", () -> service.doWork(), Cron.daily());
         
        Parameters:
        id - the id of this recurring job which can be used to alter or delete it
        job - the lambda which defines the fire-and-forget job
        cron - The cron expression defining when to run this recurring job
        Returns:
        the id of this recurring job which can be used to alter or delete it
        See Also:
        Cron
      • scheduleRecurringly

        public static <TService> java.lang.String scheduleRecurringly​(java.lang.String id,
                                                                      IocJobLambda<TService> iocJob,
                                                                      java.lang.String cron)
        Creates a new recurring job based on the given id, lambda and cron expression. The IoC container will be used to resolve MyService. The jobs will be scheduled using the systemDefault timezone
        An example:
        
              BackgroundJob.<MyService>scheduleRecurringly("my-recurring-job", x -> x.doWork(), Cron.daily());
         
        Parameters:
        id - the id of this recurring job which can be used to alter or delete it
        iocJob - the lambda which defines the fire-and-forget job
        cron - The cron expression defining when to run this recurring job
        Returns:
        the id of this recurring job which can be used to alter or delete it
        See Also:
        Cron
      • scheduleRecurringly

        public static java.lang.String scheduleRecurringly​(java.lang.String id,
                                                           JobLambda job,
                                                           java.lang.String cron,
                                                           java.time.ZoneId zoneId)
        Creates a new recurring job based on the given id, lambda, cron expression and ZoneId.
        An example:
        
              MyService service = new MyService();
              BackgroundJob.scheduleRecurringly("my-recurring-job", () -> service.doWork(), Cron.daily(), ZoneId.of("Europe/Brussels"));
         
        Parameters:
        id - the id of this recurring job which can be used to alter or delete it
        job - the lambda which defines the fire-and-forget job
        cron - The cron expression defining when to run this recurring job
        zoneId - The zoneId (timezone) of when to run this recurring job
        Returns:
        the id of this recurring job which can be used to alter or delete it
        See Also:
        Cron
      • scheduleRecurringly

        public static <TService> java.lang.String scheduleRecurringly​(java.lang.String id,
                                                                      IocJobLambda<TService> iocJob,
                                                                      java.lang.String cron,
                                                                      java.time.ZoneId zoneId)
        Creates a new recurring job based on the given id, lambda, cron expression and ZoneId. The IoC container will be used to resolve MyService.
        An example:
        
              BackgroundJob.<MyService>scheduleRecurringly("my-recurring-job", x -> x.doWork(), Cron.daily(), ZoneId.of("Europe/Brussels"));
         
        Parameters:
        id - the id of this recurring job which can be used to alter or delete it
        iocJob - the lambda which defines the fire-and-forget job
        cron - The cron expression defining when to run this recurring job
        zoneId - The zoneId (timezone) of when to run this recurring job
        Returns:
        the id of this recurring job which can be used to alter or delete it
        See Also:
        Cron
      • deleteRecurringly

        public static void deleteRecurringly​(java.lang.String id)
        Deletes the recurring job based on the given id.
        An example:
        
              BackgroundJob.deleteRecurringly("my-recurring-job"));
         
        Parameters:
        id - the id of the recurring job to delete
      • setJobScheduler

        public static void setJobScheduler​(JobScheduler jobScheduler)