Class TransactionOutbox
- java.lang.Object
-
- com.gruelbox.transactionoutbox.TransactionOutbox
-
public class TransactionOutbox extends Object
An implementation of the Transactional Outbox pattern for Java. See README for usage instructions.
-
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
flush()
Identifies any stale tasks queued usingschedule(Class)
(those which were queued more thanattemptFrequency
ago and have been tried less thanblacklistAfterAttempts
times) and attempts to resubmit them.void
processNow(TransactionOutboxEntry entry)
Processes an entry immediately in the current thread.<T> T
schedule(Class<T> clazz)
The main entry point for submitting new transaction outbox tasks.boolean
whitelist(String entryId)
Marks a blacklisted entry back to not blacklisted and resets the attempt count.
-
-
-
Method Detail
-
schedule
public <T> T schedule(Class<T> clazz)
The main entry point for submitting new transaction outbox tasks.Returns a proxy of
T
which, when called, will instantly return and schedule a call of the real method to occur after the current transaction is committed (as such a transaction needs to be active and accessible fromtransactionManager
)Usage:
transactionOutbox.schedule(MyService.class) .runMyMethod("with", "some", "arguments");
This will write a record to the database using the supplied
Persistor
andInstantiator
, using the current database transaction, which will get rolled back if the rest of the transaction is, and thus never processed. However, if the transaction is committed, the real method will be called immediately afterwards using the suppliedsubmitter
. Should that fail, the call will be reattempted wheneverflush()
is called, provided at leastattemptFrequency
has passed since the time the task was last attempted.- Type Parameters:
T
- The type to proxy.- Parameters:
clazz
- The class to proxy.- Returns:
- The proxy of
T
.
-
flush
public boolean flush()
Identifies any stale tasks queued usingschedule(Class)
(those which were queued more thanattemptFrequency
ago and have been tried less thanblacklistAfterAttempts
times) and attempts to resubmit them.As long as
submitter
is non-blocking (e.g. uses a bounded queue with aRejectedExecutionHandler
which throws such asThreadPoolExecutor.AbortPolicy
), this method will return quickly. However, ifsubmitter
uses a bounded queue with a blocking policy, this method could block for a long time, depending on how long the scheduled work takes and how largeflushBatchSize
is.Calls
TransactionManager.inTransactionReturns(TransactionalSupplier)
to start a new transaction for the fetch.- Returns:
- true if any work was flushed.
-
whitelist
public boolean whitelist(String entryId)
Marks a blacklisted entry back to not blacklisted and resets the attempt count. Requires an active transaction.- Parameters:
entryId
- The entry id.- Returns:
- True if the whitelisting request was successful. May return false if another thread whitelisted the entry first.
-
processNow
public void processNow(TransactionOutboxEntry entry)
Processes an entry immediately in the current thread. Intended for use in custom implementations ofSubmitter
and should not generally otherwise be called.- Parameters:
entry
- The entry.
-
-