@@ -87,7 +87,7 @@ use crate::producer::request_response_future::RequestResponseFuture;
8787use crate :: producer:: send_callback:: SendMessageCallback ;
8888use crate :: producer:: send_result:: SendResult ;
8989use crate :: producer:: send_status:: SendStatus ;
90- use crate :: producer:: transaction_listener:: TransactionListener ;
90+ use crate :: producer:: transaction_listener:: ArcTransactionListener ;
9191use crate :: producer:: transaction_send_result:: TransactionSendResult ;
9292use tokio:: task:: JoinHandle ;
9393
@@ -266,7 +266,7 @@ pub struct DefaultMQProducerImpl {
266266 semaphore_async_send_num : Arc < Semaphore > ,
267267 semaphore_async_send_size : Arc < Semaphore > ,
268268 default_mqproducer_impl_inner : Option < WeakArcMut < DefaultMQProducerImpl > > ,
269- transaction_listener : Option < Arc < Box < dyn TransactionListener > > > ,
269+ transaction_listener : Option < ArcTransactionListener > ,
270270}
271271
272272#[ allow( unused_must_use) ]
@@ -2190,7 +2190,7 @@ impl DefaultMQProducerImpl {
21902190 self . default_mqproducer_impl_inner = Some ( default_mqproducer_impl_inner) ;
21912191 }
21922192
2193- pub fn set_transaction_listener ( & mut self , transaction_listener : Arc < Box < dyn TransactionListener > > ) {
2193+ pub fn set_transaction_listener ( & mut self , transaction_listener : ArcTransactionListener ) {
21942194 self . transaction_listener = Some ( transaction_listener) ;
21952195 }
21962196}
@@ -2210,7 +2210,7 @@ impl MQProducerInner for DefaultMQProducerImpl {
22102210 true
22112211 }
22122212
2213- fn get_check_listener ( & self ) -> Option < Arc < Box < dyn TransactionListener > > > {
2213+ fn get_check_listener ( & self ) -> Option < ArcTransactionListener > {
22142214 self . transaction_listener . clone ( )
22152215 }
22162216
@@ -2235,16 +2235,17 @@ impl MQProducerInner for DefaultMQProducerImpl {
22352235 let broker_addr = broker_addr. clone ( ) ;
22362236 let group = self . producer_config . producer_group ( ) . clone ( ) ;
22372237
2238- // Spawn independent task without storing handle (matches Java's executor.submit behavior)
2239- tokio:: spawn ( async move {
2238+ // Use spawn_blocking to avoid blocking Tokio worker threads (matches Java's ExecutorService
2239+ // behavior)
2240+ tokio:: task:: spawn_blocking ( move || {
22402241 let mut unique_key = msg. property ( & CheetahString :: from_static_str (
22412242 MessageConst :: PROPERTY_UNIQ_CLIENT_MESSAGE_ID_KEYIDX ,
22422243 ) ) ;
22432244 if unique_key. is_none ( ) {
22442245 unique_key = Some ( msg. msg_id . clone ( ) ) ;
22452246 }
22462247
2247- // Check local transaction state with exception handling
2248+ // Check local transaction state with exception handling (synchronous execution)
22482249 let transaction_state = match std:: panic:: catch_unwind ( std:: panic:: AssertUnwindSafe ( || {
22492250 transaction_listener. check_local_transaction ( & msg)
22502251 } ) ) {
@@ -2258,48 +2259,53 @@ impl MQProducerInner for DefaultMQProducerImpl {
22582259 LocalTransactionState :: Unknown
22592260 }
22602261 } ;
2261- let request_header = EndTransactionRequestHeader {
2262- topic : check_request_header. topic . clone ( ) . unwrap_or_default ( ) ,
2263- producer_group : CheetahString :: from_string (
2264- producer_impl_inner. producer_config . producer_group ( ) . to_string ( ) ,
2265- ) ,
2266- tran_state_table_offset : check_request_header. commit_log_offset as u64 ,
2267- commit_log_offset : check_request_header. commit_log_offset as u64 ,
2268- commit_or_rollback : match transaction_state {
2269- LocalTransactionState :: CommitMessage => MessageSysFlag :: TRANSACTION_COMMIT_TYPE ,
2270- LocalTransactionState :: RollbackMessage => MessageSysFlag :: TRANSACTION_ROLLBACK_TYPE ,
2271- LocalTransactionState :: Unknown => MessageSysFlag :: TRANSACTION_NOT_TYPE ,
2272- } ,
2273- from_transaction_check : true ,
2274- msg_id : unique_key. clone ( ) . unwrap_or_default ( ) ,
2275- transaction_id : check_request_header. transaction_id . clone ( ) ,
2276- rpc_request_header : RpcRequestHeader {
2277- broker_name : check_request_header. rpc_request_header . unwrap_or_default ( ) . broker_name ,
2278- ..Default :: default ( )
2279- } ,
2280- } ;
2281- // Execute end transaction hook
2282- producer_impl_inner. do_execute_end_transaction_hook (
2283- & msg. message ,
2284- unique_key. as_ref ( ) . unwrap ( ) ,
2285- & broker_addr,
2286- transaction_state,
2287- true ,
2288- ) ;
22892262
2290- // Send end transaction request with error handling
2291- if let Err ( e) = producer_impl_inner
2292- . client_instance
2293- . as_mut ( )
2294- . unwrap ( )
2295- . mq_client_api_impl
2296- . as_mut ( )
2297- . unwrap ( )
2298- . end_transaction_oneway ( & broker_addr, request_header, CheetahString :: from_static_str ( "" ) , 3000 )
2299- . await
2300- {
2301- tracing:: error!( "endTransactionOneway exception: {:?}" , e) ;
2302- }
2263+ // Switch back to async context for network I/O
2264+ let handle = tokio:: runtime:: Handle :: current ( ) ;
2265+ handle. spawn ( async move {
2266+ let request_header = EndTransactionRequestHeader {
2267+ topic : check_request_header. topic . clone ( ) . unwrap_or_default ( ) ,
2268+ producer_group : CheetahString :: from_string (
2269+ producer_impl_inner. producer_config . producer_group ( ) . to_string ( ) ,
2270+ ) ,
2271+ tran_state_table_offset : check_request_header. commit_log_offset as u64 ,
2272+ commit_log_offset : check_request_header. commit_log_offset as u64 ,
2273+ commit_or_rollback : match transaction_state {
2274+ LocalTransactionState :: CommitMessage => MessageSysFlag :: TRANSACTION_COMMIT_TYPE ,
2275+ LocalTransactionState :: RollbackMessage => MessageSysFlag :: TRANSACTION_ROLLBACK_TYPE ,
2276+ LocalTransactionState :: Unknown => MessageSysFlag :: TRANSACTION_NOT_TYPE ,
2277+ } ,
2278+ from_transaction_check : true ,
2279+ msg_id : unique_key. clone ( ) . unwrap_or_default ( ) ,
2280+ transaction_id : check_request_header. transaction_id . clone ( ) ,
2281+ rpc_request_header : RpcRequestHeader {
2282+ broker_name : check_request_header. rpc_request_header . unwrap_or_default ( ) . broker_name ,
2283+ ..Default :: default ( )
2284+ } ,
2285+ } ;
2286+ // Execute end transaction hook
2287+ producer_impl_inner. do_execute_end_transaction_hook (
2288+ & msg. message ,
2289+ unique_key. as_ref ( ) . unwrap ( ) ,
2290+ & broker_addr,
2291+ transaction_state,
2292+ true ,
2293+ ) ;
2294+
2295+ // Send end transaction request with error handling
2296+ if let Err ( e) = producer_impl_inner
2297+ . client_instance
2298+ . as_mut ( )
2299+ . unwrap ( )
2300+ . mq_client_api_impl
2301+ . as_mut ( )
2302+ . unwrap ( )
2303+ . end_transaction_oneway ( & broker_addr, request_header, CheetahString :: from_static_str ( "" ) , 3000 )
2304+ . await
2305+ {
2306+ tracing:: error!( "endTransactionOneway exception: {:?}" , e) ;
2307+ }
2308+ } ) ;
23032309 } ) ;
23042310 }
23052311
0 commit comments