pub trait TransactionListener:
Send
+ Sync
+ 'static {
// Required methods
fn execute_local_transaction(
&self,
msg: &dyn MessageTrait,
arg: Option<&(dyn Any + Send + Sync)>,
) -> LocalTransactionState;
fn check_local_transaction(&self, msg: &MessageExt) -> LocalTransactionState;
}Expand description
Listener for handling transactional message operations.
This trait defines the callback interface for transactional message processing, allowing applications to integrate local transaction execution with distributed message transactions.
Implementations must be thread-safe as they may be invoked concurrently from different threads or async tasks.
§Examples
use rocketmq_client_rust::TransactionListener;
use rocketmq_client_rust::LocalTransactionState;
use rocketmq_model::common::message::MessageTrait;
use rocketmq_model::common::message::message_ext::MessageExt;
use std::any::Any;
struct OrderTransactionListener;
impl TransactionListener for OrderTransactionListener {
fn execute_local_transaction(
&self,
msg: &dyn MessageTrait,
arg: Option<&(dyn Any + Send + Sync)>,
) -> LocalTransactionState {
// Execute local database transaction
if insert_order_into_db(msg).is_ok() {
LocalTransactionState::CommitMessage
} else {
LocalTransactionState::RollbackMessage
}
}
fn check_local_transaction(&self, msg: &MessageExt) -> LocalTransactionState {
// Check transaction status from database
if order_exists_in_db(msg) {
LocalTransactionState::CommitMessage
} else {
LocalTransactionState::Unknown
}
}
}Required Methods§
Sourcefn execute_local_transaction(
&self,
msg: &dyn MessageTrait,
arg: Option<&(dyn Any + Send + Sync)>,
) -> LocalTransactionState
fn execute_local_transaction( &self, msg: &dyn MessageTrait, arg: Option<&(dyn Any + Send + Sync)>, ) -> LocalTransactionState
Executes the local transaction when sending a transactional message.
This method is invoked after the half message is successfully sent to the broker. The implementation should execute the local business transaction and return the transaction state to determine whether the message should be committed or rolled back.
§Parameters
msg- The message being sentarg- Optional user-defined argument passed from the send operation
§Returns
The local transaction state indicating whether to commit, rollback, or defer the decision:
CommitMessage- Commit the transaction and make the message visible to consumersRollbackMessage- Roll back the transaction and discard the messageUnknown- Transaction state is uncertain, broker will check later
Sourcefn check_local_transaction(&self, msg: &MessageExt) -> LocalTransactionState
fn check_local_transaction(&self, msg: &MessageExt) -> LocalTransactionState
Checks the status of a previously executed local transaction.
This method is invoked by the broker when it needs to verify the state of a
transaction whose initial state was Unknown or when the transaction check
timeout is reached.
The implementation should query the local transaction state (e.g., from a database) and return the current status.
§Parameters
msg- The message whose transaction status needs to be checked
§Returns
The current state of the local transaction:
CommitMessage- The local transaction was committed successfullyRollbackMessage- The local transaction failed or was rolled backUnknown- Transaction state cannot be determined at this time
Dyn Compatibility§
This trait is dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".