Package com.gruelbox.transactionoutbox
Interface Persistor
- All Known Implementing Classes:
DefaultPersistor
,StubPersistor
public interface Persistor
Saves and loads
TransactionOutboxEntry
s. For most use cases, just use DefaultPersistor
. It is parameterisable and designed for extension, so can be easily modified.
Creating completely new implementations of Persistor
should be reserved for cases where
the underlying data store is of a completely different nature entirely.-
Method Summary
Modifier and TypeMethodDescriptionboolean
Checks the connection status of a transaction.void
clear
(Transaction tx) Clears the database.void
delete
(Transaction tx, TransactionOutboxEntry entry) Deletes aTransactionOutboxEntry
.int
deleteProcessedAndExpired
(Transaction tx, int batchSize, Instant now) Deletes records which have processed and passed their expiry time, in specified batch sizes.static DefaultPersistor
forDialect
(Dialect dialect) Uses the default relational persistor.boolean
lock
(Transaction tx, TransactionOutboxEntry entry) Attempts to pessimistically lock an existingTransactionOutboxEntry
.void
migrate
(TransactionManager transactionManager) Upgrades any database schema used by the persistor to the latest version.nextInTopic
(Transaction tx, String topic) Fetches and locks the next available piece of work on the specified topic.void
save
(Transaction tx, TransactionOutboxEntry entry) Saves a newTransactionOutboxEntry
.Selects the list of topics with work awaiting processing.selectBatch
(Transaction tx, int batchSize, Instant now) Selects up to a specified maximum number of non-blocked records which have passed theirTransactionOutboxEntry.getNextAttemptTime()
.boolean
unblock
(Transaction tx, String entryId) Clears the blocked flag and resets the attempt count to zero.void
update
(Transaction tx, TransactionOutboxEntry entry) Modifies an existingTransactionOutboxEntry
.
-
Method Details
-
forDialect
Uses the default relational persistor. Shortcut for:DefaultPersistor.builder().dialect(dialect).build();
- Parameters:
dialect
- The database dialect.- Returns:
- The persistor.
-
migrate
Upgrades any database schema used by the persistor to the latest version. Called on creation of aTransactionOutbox
.- Parameters:
transactionManager
- The transactoin manager.
-
save
Saves a newTransactionOutboxEntry
. Should throwAlreadyScheduledException
if the record already exists based on theid
oruniqueRequestId
(the latter of which should not treat nulls as duplicates).- Parameters:
tx
- The currentTransaction
.entry
- The entry to save. All properties on the object should be saved recursively.- Throws:
Exception
- Any exception.
-
delete
Deletes aTransactionOutboxEntry
.A record should only be deleted if both the
id
andversion
on the database match that on the object. If no such record is found,OptimisticLockException
should be thrown.- Parameters:
tx
- The currentTransaction
.entry
- The entry to be deleted.- Throws:
OptimisticLockException
- If no such record is found.Exception
- Any other exception.
-
update
Modifies an existingTransactionOutboxEntry
. Performs an optimistic lock check on any existing record via a compare-and-swap operation and throwsOptimisticLockException
if the lock is failed.TransactionOutboxEntry.setVersion(int)
is called before returning containing the new version of the entry.- Parameters:
tx
- The currentTransaction
.entry
- The entry to be updated.- Throws:
OptimisticLockException
- If no record with same id and version is found.Exception
- Any other exception.
-
lock
Attempts to pessimistically lock an existingTransactionOutboxEntry
.- Parameters:
tx
- The currentTransaction
.entry
- The entry to be locked- Returns:
- true if the lock was successful.
- Throws:
OptimisticLockException
- If no record with same id and version is found.Exception
- Any other exception.
-
unblock
Clears the blocked flag and resets the attempt count to zero.- Parameters:
tx
- The currentTransaction
.entryId
- The entry id.- Returns:
- true if the update was successful. This will be false if the record was no longer blocked or didn't exist anymore.
- Throws:
Exception
- Any other exception.
-
selectBatch
List<TransactionOutboxEntry> selectBatch(Transaction tx, int batchSize, Instant now) throws Exception Selects up to a specified maximum number of non-blocked records which have passed theirTransactionOutboxEntry.getNextAttemptTime()
. Until a subsequent call tolock(Transaction, TransactionOutboxEntry)
, these records may be selected by another instance for processing.- Parameters:
tx
- The currentTransaction
.batchSize
- The number of records to select.now
- The time to use when selecting records.- Returns:
- The records.
- Throws:
Exception
- Any exception.
-
selectActiveTopics
Selects the list of topics with work awaiting processing.- Parameters:
tx
- The currentTransaction
.- Returns:
- The topics.
- Throws:
Exception
- Any exception.
-
nextInTopic
Fetches and locks the next available piece of work on the specified topic.- Parameters:
tx
- The currentTransaction
.topic
- The topic.- Returns:
- The next available piece of work on the selected topic.
- Throws:
Exception
- ANy exception.
-
deleteProcessedAndExpired
Deletes records which have processed and passed their expiry time, in specified batch sizes.- Parameters:
tx
- The currentTransaction
.batchSize
- The number of records to select.now
- The time to use when selecting records.- Returns:
- The number of records deleted.
- Throws:
Exception
- Any exception.
-
checkConnection
Checks the connection status of a transaction.- Parameters:
tx
- The currentTransaction
.- Returns:
- true if connected and working.
- Throws:
Exception
-
clear
Clears the database. For testing only.- Parameters:
tx
- The currentTransaction
.- Throws:
Exception
-