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

com.mageddo.kafka.client.RetryPolicy Maven / Gradle / Ivy

There is a newer version: 1.0.2
Show newest version
package com.mageddo.kafka.client;

import java.time.Duration;
import java.util.Arrays;
import java.util.Collection;
import java.util.LinkedHashSet;

import lombok.Builder;
import lombok.NonNull;
import lombok.Value;

@Value
@Builder
public class RetryPolicy {

  @Builder.Default
  private int maxTries = 0;

  @NonNull
  private Duration delay;

  private Duration maxDelay;

  private Collection> retryableExceptions;

  public Duration calcMaxTotalWaitTime(){
    if(this.delay.equals(Duration.ZERO) || this.maxTries == 0){
      return Duration.ZERO;
    }
    return Duration.ofMillis(this.delay.toMillis() * maxTries);
  }

  public static class RetryPolicyBuilder {

    public RetryPolicyBuilder() {
      this.retryableExceptions = new LinkedHashSet<>();
      this.retryableExceptions.add(Exception.class);
    }

      /**
       * Replace all before handled exceptions by the informed
     */
    public RetryPolicyBuilder handleExceptions(Class... exceptions) {
      this.retryableExceptions.clear();
      this.retryableExceptions.addAll(Arrays.asList(exceptions));
      return this;
    }

    public RetryPolicyBuilder addRetryableException(Class e) {
      this.retryableExceptions.add(e);
      return this;
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy