add file drop handler
You can define a folder and ingest files dropped into it.
This commit is contained in:
120
pdb-utils/src/main/java/org/lucares/utils/Retry.java
Normal file
120
pdb-utils/src/main/java/org/lucares/utils/Retry.java
Normal file
@@ -0,0 +1,120 @@
|
||||
package org.lucares.utils;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
public class Retry {
|
||||
|
||||
public interface RetryCallable {
|
||||
public void call();
|
||||
}
|
||||
|
||||
public static class RetryException extends Exception {
|
||||
|
||||
private static final long serialVersionUID = 9178079589334153298L;
|
||||
|
||||
public RetryException(final Throwable cause, final Collection<Throwable> suppressed) {
|
||||
super(cause);
|
||||
|
||||
for (final Throwable throwable : suppressed) {
|
||||
addSuppressed(throwable);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public interface Strategy {
|
||||
public long apply(final long oldDely);
|
||||
}
|
||||
|
||||
public static class Builder {
|
||||
int maxRetries = 1;
|
||||
long delayInMs = 10;
|
||||
long maxDelayInMs = 1000;
|
||||
Strategy strategy = d -> d * 2;
|
||||
|
||||
public Builder maxRetries(final int maxRetries) {
|
||||
this.maxRetries = maxRetries;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder delay(final Duration delay) {
|
||||
this.delayInMs = delay.toMillis();
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder maxDelay(final Duration delay) {
|
||||
this.delayInMs = delay.toMillis();
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder constant() {
|
||||
strategy = d -> d;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder linear(final Duration increment) {
|
||||
strategy = d -> d + increment.toMillis();
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder expontential() {
|
||||
strategy = d -> d * 2;
|
||||
return this;
|
||||
}
|
||||
|
||||
public void retry(final RetryCallable retryCallable) throws RetryException, InterruptedException {
|
||||
|
||||
final List<Throwable> suppressed = new ArrayList<>();
|
||||
|
||||
for (int i = 0; i <= maxRetries; i++) {
|
||||
try {
|
||||
retryCallable.call();
|
||||
break;
|
||||
} catch (final Throwable e) {
|
||||
if (i == maxRetries) {
|
||||
throw new RetryException(e, suppressed);
|
||||
} else {
|
||||
suppressed.add(e);
|
||||
}
|
||||
|
||||
TimeUnit.MILLISECONDS.sleep(delayInMs);
|
||||
delayInMs = strategy.apply(delayInMs);
|
||||
delayInMs = Math.min(delayInMs, maxDelayInMs);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void retry(final RetryCallable retryCallable) throws RetryException, InterruptedException {
|
||||
new Builder().retry(retryCallable);
|
||||
}
|
||||
|
||||
public static Builder maxRetries(final int maxRetries) {
|
||||
return new Builder().maxRetries(maxRetries);
|
||||
}
|
||||
|
||||
public static Builder maxDelay(final Duration maxDelay) {
|
||||
return new Builder().maxDelay(maxDelay);
|
||||
}
|
||||
|
||||
public Builder delay(final Duration delay) {
|
||||
return new Builder().delay(delay);
|
||||
}
|
||||
|
||||
public Builder constant() {
|
||||
return new Builder().constant();
|
||||
}
|
||||
|
||||
public Builder linear(final Duration increment) {
|
||||
return new Builder().linear(increment);
|
||||
}
|
||||
|
||||
public Builder expontential() {
|
||||
return new Builder().expontential();
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user