4
4
import static com .gruelbox .transactionoutbox .spi .Utils .uncheckedly ;
5
5
import static java .time .temporal .ChronoUnit .MILLIS ;
6
6
import static java .time .temporal .ChronoUnit .MINUTES ;
7
+ import static java .util .Objects .requireNonNullElse ;
7
8
8
9
import com .gruelbox .transactionoutbox .spi .ProxyFactory ;
9
10
import com .gruelbox .transactionoutbox .spi .Utils ;
@@ -82,7 +83,7 @@ public void initialize() {
82
83
83
84
@ Override
84
85
public <T > T schedule (Class <T > clazz ) {
85
- return schedule (clazz , null , null , null );
86
+ return schedule (clazz , null , null , null , null , null );
86
87
}
87
88
88
89
@ Override
@@ -219,7 +220,8 @@ public boolean unblock(String entryId, Object transactionContext) {
219
220
}
220
221
221
222
private <T > T schedule (
222
- Class <T > clazz , String uniqueRequestId , String topic , Duration delayForAtLeast ) {
223
+ Class <T > clazz , String uniqueRequestId , String topic , Duration delayForAtLeast ,
224
+ Duration attemptFrequencyOverride , Integer blockAfterAttemptsOverride ) {
223
225
if (!initialized .get ()) {
224
226
throw new IllegalStateException ("Not initialized" );
225
227
}
@@ -240,6 +242,8 @@ private <T> T schedule(
240
242
if (delayForAtLeast != null ) {
241
243
entry .setNextAttemptTime (entry .getNextAttemptTime ().plus (delayForAtLeast ));
242
244
}
245
+ entry .setAttemptFrequency (requireNonNullElse (attemptFrequencyOverride , attemptFrequency ));
246
+ entry .setBlockAfterAttempts (requireNonNullElse (blockAfterAttemptsOverride , blockAfterAttempts ));
243
247
validator .validate (entry );
244
248
persistor .save (extracted .getTransaction (), entry );
245
249
extracted
@@ -253,7 +257,7 @@ private <T> T schedule(
253
257
submitNow (entry );
254
258
log .debug (
255
259
"Scheduled {} for post-commit execution" , entry .description ());
256
- } else if (delayForAtLeast .compareTo (attemptFrequency ) < 0 ) {
260
+ } else if (delayForAtLeast .compareTo (entry . getAttemptFrequency () ) < 0 ) {
257
261
scheduler .schedule (
258
262
() -> submitNow (entry ),
259
263
delayForAtLeast .toMillis (),
@@ -360,7 +364,7 @@ private void pushBack(Transaction transaction, TransactionOutboxEntry entry)
360
364
throws OptimisticLockException {
361
365
try {
362
366
entry .setLastAttemptTime (clockProvider .get ().instant ());
363
- entry .setNextAttemptTime (after (attemptFrequency ));
367
+ entry .setNextAttemptTime (after (requireNonNullElse ( entry . getAttemptFrequency (), attemptFrequency ) ));
364
368
validator .validate (entry );
365
369
persistor .update (transaction , entry );
366
370
} catch (OptimisticLockException e ) {
@@ -377,7 +381,7 @@ private Instant after(Duration duration) {
377
381
private void updateAttemptCount (TransactionOutboxEntry entry , Throwable cause ) {
378
382
try {
379
383
entry .setAttempts (entry .getAttempts () + 1 );
380
- var blocked = (entry .getTopic () == null ) && (entry .getAttempts () >= blockAfterAttempts );
384
+ var blocked = (entry .getTopic () == null ) && (entry .getAttempts () >= requireNonNullElse ( entry . getBlockAfterAttempts (), blockAfterAttempts ) );
381
385
entry .setBlocked (blocked );
382
386
transactionManager .inTransactionThrows (tx -> pushBack (tx , entry ));
383
387
listener .failure (entry , cause );
@@ -445,13 +449,16 @@ private class ParameterizedScheduleBuilderImpl implements ParameterizedScheduleB
445
449
private String uniqueRequestId ;
446
450
private String ordered ;
447
451
private Duration delayForAtLeast ;
452
+ private Duration attemptFrequency ;
453
+ private Integer blockAfterAttempts ;
448
454
449
455
@ Override
450
456
public <T > T schedule (Class <T > clazz ) {
451
457
if (uniqueRequestId != null && uniqueRequestId .length () > 250 ) {
452
458
throw new IllegalArgumentException ("uniqueRequestId may be up to 250 characters" );
453
459
}
454
- return TransactionOutboxImpl .this .schedule (clazz , uniqueRequestId , ordered , delayForAtLeast );
460
+ return TransactionOutboxImpl .this .schedule (clazz , uniqueRequestId , ordered , delayForAtLeast ,
461
+ attemptFrequency , blockAfterAttempts );
455
462
}
456
463
}
457
464
}
0 commit comments