K
- the key type.V
- the value type.public class KafkaTransactionManager<K,V> extends org.springframework.transaction.support.AbstractPlatformTransactionManager implements KafkaAwareTransactionManager<K,V>
PlatformTransactionManager
implementation for a
single Kafka ProducerFactory
. Binds a Kafka producer from the specified
ProducerFactory to the thread, potentially allowing for one thread-bound producer per
ProducerFactory.
This local strategy is an alternative to executing Kafka operations within, and synchronized with, external transactions. This strategy is not able to provide XA transactions, for example in order to share transactions between messaging and database access.
Application code is required to retrieve the transactional Kafka resources via
ProducerFactoryUtils.getTransactionalResourceHolder(ProducerFactory, String, java.time.Duration)
.
Spring's KafkaTemplate
will auto
detect a thread-bound Producer and automatically participate in it.
The use of DefaultKafkaProducerFactory
as a target for this transaction manager is strongly
recommended. Because it caches producers for reuse.
Transaction synchronization is turned off by default, as this manager might be used alongside a datastore-based Spring transaction manager such as the JDBC org.springframework.jdbc.datasource.DataSourceTransactionManager, which has stronger needs for synchronization.
Constructor and Description |
---|
KafkaTransactionManager(ProducerFactory<K,V> producerFactory)
Create a new KafkaTransactionManager, given a ProducerFactory.
|
Modifier and Type | Method and Description |
---|---|
protected void |
doBegin(java.lang.Object transaction,
org.springframework.transaction.TransactionDefinition definition) |
protected void |
doCleanupAfterCompletion(java.lang.Object transaction) |
protected void |
doCommit(org.springframework.transaction.support.DefaultTransactionStatus status) |
protected java.lang.Object |
doGetTransaction() |
protected void |
doResume(java.lang.Object transaction,
java.lang.Object suspendedResources) |
protected void |
doRollback(org.springframework.transaction.support.DefaultTransactionStatus status) |
protected void |
doSetRollbackOnly(org.springframework.transaction.support.DefaultTransactionStatus status) |
protected java.lang.Object |
doSuspend(java.lang.Object transaction) |
ProducerFactory<K,V> |
getProducerFactory()
Get the producer factory.
|
java.lang.Object |
getResourceFactory()
Deprecated.
- in a future release
KafkaAwareTransactionManager will not be
a sub interface of
ResourceTransactionManager . TODO: Remove in 3.0 |
protected boolean |
isExistingTransaction(java.lang.Object transaction) |
void |
setCloseTimeout(java.time.Duration closeTimeout)
Set the maximum time to wait when closing a producer; default 5 seconds.
|
void |
setTransactionIdPrefix(java.lang.String transactionIdPrefix)
Set a transaction id prefix to override the prefix in the producer factory.
|
commit, determineTimeout, getDefaultTimeout, getTransaction, getTransactionSynchronization, invokeAfterCompletion, isFailEarlyOnGlobalRollbackOnly, isGlobalRollbackOnParticipationFailure, isNestedTransactionAllowed, isRollbackOnCommitFailure, isValidateExistingTransaction, newTransactionStatus, prepareForCommit, prepareSynchronization, prepareTransactionStatus, registerAfterCompletionWithExistingTransaction, resume, rollback, setDefaultTimeout, setFailEarlyOnGlobalRollbackOnly, setGlobalRollbackOnParticipationFailure, setNestedTransactionAllowed, setRollbackOnCommitFailure, setTransactionSynchronization, setTransactionSynchronizationName, setValidateExistingTransaction, shouldCommitOnGlobalRollbackOnly, suspend, triggerBeforeCommit, triggerBeforeCompletion, useSavepointForNestedTransaction
public KafkaTransactionManager(ProducerFactory<K,V> producerFactory)
producerFactory
- the ProducerFactory to usepublic void setTransactionIdPrefix(java.lang.String transactionIdPrefix)
transactionIdPrefix
- the prefix.public ProducerFactory<K,V> getProducerFactory()
getProducerFactory
in interface KafkaAwareTransactionManager<K,V>
public void setCloseTimeout(java.time.Duration closeTimeout)
closeTimeout
- the close timeout.@Deprecated public java.lang.Object getResourceFactory()
KafkaAwareTransactionManager
will not be
a sub interface of
ResourceTransactionManager
. TODO: Remove in 3.0getResourceFactory
in interface org.springframework.transaction.support.ResourceTransactionManager
protected java.lang.Object doGetTransaction()
doGetTransaction
in class org.springframework.transaction.support.AbstractPlatformTransactionManager
protected boolean isExistingTransaction(java.lang.Object transaction)
isExistingTransaction
in class org.springframework.transaction.support.AbstractPlatformTransactionManager
protected void doBegin(java.lang.Object transaction, org.springframework.transaction.TransactionDefinition definition)
doBegin
in class org.springframework.transaction.support.AbstractPlatformTransactionManager
protected java.lang.Object doSuspend(java.lang.Object transaction)
doSuspend
in class org.springframework.transaction.support.AbstractPlatformTransactionManager
protected void doResume(java.lang.Object transaction, java.lang.Object suspendedResources)
doResume
in class org.springframework.transaction.support.AbstractPlatformTransactionManager
protected void doCommit(org.springframework.transaction.support.DefaultTransactionStatus status)
doCommit
in class org.springframework.transaction.support.AbstractPlatformTransactionManager
protected void doRollback(org.springframework.transaction.support.DefaultTransactionStatus status)
doRollback
in class org.springframework.transaction.support.AbstractPlatformTransactionManager
protected void doSetRollbackOnly(org.springframework.transaction.support.DefaultTransactionStatus status)
doSetRollbackOnly
in class org.springframework.transaction.support.AbstractPlatformTransactionManager
protected void doCleanupAfterCompletion(java.lang.Object transaction)
doCleanupAfterCompletion
in class org.springframework.transaction.support.AbstractPlatformTransactionManager