All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.breuninger.boot.jobs.definition.DefaultJobDefinition Maven / Gradle / Ivy

There is a newer version: 2.0.5.RELEASE
Show newest version
package com.breuninger.boot.jobs.definition;

import java.time.Duration;
import java.util.Optional;

import org.springframework.scheduling.support.CronSequenceGenerator;

public final class DefaultJobDefinition implements JobDefinition {

  private final String jobType;
  private final String jobName;
  private final String description;
  private final Optional maxAge;
  private final Optional fixedDelay;
  private final Optional cron;
  private final int restarts;
  private final int retries;
  private final Optional retryDelay;

  private DefaultJobDefinition(final String jobType, final String jobName, final String description,
                               final Optional maxAge, final Optional fixedDelay, final Optional cron,
                               final int restarts, final int retries, final Optional retryDelay) {
    this.jobType = jobType;
    this.jobName = jobName;
    this.description = description;
    this.maxAge = maxAge;
    this.fixedDelay = fixedDelay;
    this.cron = cron;
    this.restarts = restarts;
    this.retries = retries;
    this.retryDelay = retryDelay;
    cron.ifPresent(DefaultJobDefinition::validateCron);
  }

  public static JobDefinition manuallyTriggerableJobDefinition(final String jobType, final String jobName,
                                                               final String description, final int restarts,
                                                               final Optional maxAge) {
    return new DefaultJobDefinition(jobType, jobName, description, maxAge, Optional.empty(), Optional.empty(), restarts, 0,
      Optional.empty());
  }

  public static JobDefinition cronJobDefinition(final String jobType, final String jobName, final String description,
                                                final String cron, final int restarts, final Optional maxAge) {
    return new DefaultJobDefinition(jobType, jobName, description, maxAge, Optional.empty(), Optional.of(cron), restarts, 0,
      Optional.empty());
  }

  public static JobDefinition retryableCronJobDefinition(final String jobType, final String jobName, final String description,
                                                         final String cron, final int restarts, final int retries,
                                                         final Duration retryDelay, final Optional maxAge) {
    return new DefaultJobDefinition(jobType, jobName, description, maxAge, Optional.empty(), Optional.of(cron), restarts, retries,
      Optional.of(retryDelay));
  }

  public static DefaultJobDefinition fixedDelayJobDefinition(final String jobType, final String jobName, final String description,
                                                             final Duration fixedDelay, final int restarts,
                                                             final Optional maxAge) {
    return new DefaultJobDefinition(jobType, jobName, description, maxAge, Optional.of(fixedDelay), Optional.empty(), restarts, 0,
      Optional.empty());
  }

  public static DefaultJobDefinition retryableFixedDelayJobDefinition(final String jobType, final String jobName,
                                                                      final String description, final Duration fixedDelay,
                                                                      final int restarts, final int retries,
                                                                      final Optional retryDelay,
                                                                      final Optional maxAge) {
    return new DefaultJobDefinition(jobType, jobName, description, maxAge, Optional.of(fixedDelay), Optional.empty(), restarts,
      retries, retryDelay);
  }

  public static void validateCron(final String cron) {
    new CronSequenceGenerator(cron);
  }

  @Override
  public String jobType() {
    return jobType;
  }

  @Override
  public String jobName() {
    return jobName;
  }

  @Override
  public String description() {
    return description;
  }

  @Override
  public Optional maxAge() {
    return maxAge;
  }

  @Override
  public Optional fixedDelay() {
    return fixedDelay;
  }

  @Override
  public Optional cron() {
    return cron;
  }

  @Override
  public int restarts() {
    return restarts;
  }

  @Override
  public int retries() {
    return retries;
  }

  @Override
  public Optional retryDelay() {
    return retryDelay;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy