Skip to content

Commit 9dfcca0

Browse files
committed
Allow to configure retry policy on a per-task basis // formatting and naming fixes
1 parent dc9833d commit 9dfcca0

File tree

2 files changed

+16
-13
lines changed

2 files changed

+16
-13
lines changed

transactionoutbox-core/src/main/java/com/gruelbox/transactionoutbox/RetryPolicyAware.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,15 @@
44

55
/**
66
* Defines a custom retry policy for tasks scheduled in the {@link TransactionOutbox}.
7-
* <p>
8-
* Implement this interface in the class that is passed to
9-
* {@link TransactionOutbox#schedule(Class)} to override the default retry behavior.
10-
* </p>
7+
*
8+
* <p>Implement this interface in the class that is passed to {@link
9+
* TransactionOutbox#schedule(Class)} to override the default retry behavior.
1110
*/
1211
public interface RetryPolicyAware {
1312
/**
1413
* Determines the wait duration before retrying a failed task.
1514
*
16-
* @param attempt The current retry attempt (starting from 1).
15+
* @param attempt The current retry attempt (starting from 1).
1716
* @param throwable The exception that caused the failure.
1817
* @return The duration to wait before the next retry.
1918
*/
@@ -22,10 +21,10 @@ public interface RetryPolicyAware {
2221
/**
2322
* Specifies the maximum number of retry attempts before blocking the task.
2423
*
25-
* @param attempt The current retry attempt (starting from 1).
24+
* @param attempt The current retry attempt (starting from 1).
2625
* @param throwable The exception that caused the failure.
27-
* @return The number of attempts after which the task should be blocked.
28-
* If the returned value is less than or equal to {@code attempt}, the task is blocked immediately.
26+
* @return The number of attempts after which the task should be blocked. If the returned value is
27+
* less than or equal to {@code attempt}, the task is blocked immediately.
2928
*/
3029
int blockAfterAttempts(int attempt, Throwable throwable);
3130
}

transactionoutbox-core/src/main/java/com/gruelbox/transactionoutbox/TransactionOutboxImpl.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -389,18 +389,22 @@ private Instant after(Duration duration) {
389389
}
390390

391391
private void updateAttemptCount(
392-
TransactionOutboxEntry entry, Throwable cause, RetryPolicyAware work) {
392+
TransactionOutboxEntry entry, Throwable cause, RetryPolicyAware retryPolicyAware) {
393393
try {
394394
entry.setAttempts(entry.getAttempts() + 1);
395395

396396
int blockAfterAttempts =
397-
work == null
397+
retryPolicyAware == null
398398
? this.blockAfterAttempts
399-
: work.blockAfterAttempts(entry.getAttempts(), cause);
399+
: retryPolicyAware.blockAfterAttempts(entry.getAttempts(), cause);
400400
Duration waitDuration =
401-
work == null ? this.attemptFrequency : work.waitDuration(entry.getAttempts(), cause);
401+
retryPolicyAware == null
402+
? this.attemptFrequency
403+
: retryPolicyAware.waitDuration(entry.getAttempts(), cause);
402404

403-
var blocked = (entry.getTopic() == null) && (entry.getAttempts() >= blockAfterAttempts);
405+
var blocked =
406+
(entry.getTopic() == null || retryPolicyAware != null)
407+
&& (entry.getAttempts() >= blockAfterAttempts);
404408
entry.setBlocked(blocked);
405409
transactionManager.inTransactionThrows(tx -> pushBack(tx, entry, waitDuration));
406410
listener.failure(entry, cause);

0 commit comments

Comments
 (0)