-
Notifications
You must be signed in to change notification settings - Fork 518
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
1,096 additions
and
214 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
src/main/java/org/springframework/retry/backoff/LastBackoffPeriodSupplier.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package org.springframework.retry.backoff; | ||
|
||
import java.util.function.Supplier; | ||
|
||
public interface LastBackoffPeriodSupplier extends Supplier<Long> { | ||
} |
22 changes: 22 additions & 0 deletions
22
src/main/java/org/springframework/retry/backoff/RememberPeriodSleeper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package org.springframework.retry.backoff; | ||
|
||
import org.apache.commons.logging.Log; | ||
import org.apache.commons.logging.LogFactory; | ||
|
||
public class RememberPeriodSleeper implements Sleeper, LastBackoffPeriodSupplier { | ||
|
||
private static final Log logger = LogFactory.getLog(RememberPeriodSleeper.class); | ||
|
||
private volatile Long lastBackoffPeriod; | ||
|
||
@Override | ||
public void sleep(long backOffPeriod) { | ||
logger.debug("Remembering a sleeping period instead of sleeping: " + backOffPeriod); | ||
lastBackoffPeriod = backOffPeriod; | ||
} | ||
|
||
@Override | ||
public Long get() { | ||
return lastBackoffPeriod; | ||
} | ||
} |
84 changes: 84 additions & 0 deletions
84
src/main/java/org/springframework/retry/support/AsyncRetryResultProcessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/* | ||
* Copyright 2019 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.retry.support; | ||
|
||
import java.util.concurrent.CompletableFuture; | ||
import java.util.concurrent.CompletionException; | ||
import java.util.concurrent.ExecutionException; | ||
import java.util.concurrent.Future; | ||
import java.util.concurrent.ScheduledExecutorService; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.function.Consumer; | ||
import java.util.function.Function; | ||
import java.util.function.Supplier; | ||
|
||
import org.apache.commons.logging.Log; | ||
import org.apache.commons.logging.LogFactory; | ||
import org.springframework.retry.RetryContext; | ||
import org.springframework.retry.RetryException; | ||
import org.springframework.retry.backoff.LastBackoffPeriodSupplier; | ||
|
||
/** | ||
* @author Dave Syer | ||
*/ | ||
public abstract class AsyncRetryResultProcessor<T> implements RetryResultProcessor<T> { | ||
private static final Log logger = LogFactory.getLog(AsyncRetryResultProcessor.class); | ||
|
||
protected T doNewAttempt(Supplier<Result<T>> supplier) throws Throwable { | ||
logger.debug("Performing the next async callback invocation..."); | ||
return supplier.get().getOrThrow(); | ||
} | ||
|
||
protected abstract T scheduleNewAttemptAfterDelay( | ||
Supplier<Result<T>> supplier, | ||
ScheduledExecutorService reschedulingExecutor, | ||
long rescheduleAfterMillis, | ||
RetryContext ctx | ||
) throws Throwable; | ||
|
||
protected T handleException(Supplier<Result<T>> supplier, | ||
Consumer<Throwable> handler, | ||
Throwable throwable, | ||
ScheduledExecutorService reschedulingExecutor, | ||
LastBackoffPeriodSupplier lastBackoffPeriodSupplier, | ||
RetryContext ctx) { | ||
try { | ||
handler.accept(unwrapIfNeed(throwable)); | ||
|
||
if (reschedulingExecutor == null || lastBackoffPeriodSupplier == null) { | ||
return doNewAttempt(supplier); | ||
} else { | ||
long rescheduleAfterMillis = lastBackoffPeriodSupplier.get(); | ||
logger.debug("Scheduling a next retry with a delay = " + rescheduleAfterMillis + " millis..."); | ||
return scheduleNewAttemptAfterDelay(supplier, reschedulingExecutor, rescheduleAfterMillis, ctx); | ||
} | ||
} | ||
catch (Throwable t) { | ||
throw RetryTemplate.runtimeException(unwrapIfNeed(t)); | ||
} | ||
} | ||
|
||
static Throwable unwrapIfNeed(Throwable throwable) { | ||
if (throwable instanceof ExecutionException | ||
|| throwable instanceof CompletionException | ||
|| throwable instanceof RetryException) { | ||
return throwable.getCause(); | ||
} else { | ||
return throwable; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.