liujian16 edited a comment on issue #1838: New Programming Model of TransactionMQProducer URL: https://github.com/apache/rocketmq/issues/1838#issuecomment-598168943 Combined with some Spring Boot magic, TransactionMQProducer can be used just like ordinary producer. ```java class FooService{ @Autowired SpringTransactionMQProducer producer; @Transactional public void doSomething(){ // 1. do you business businessLogic(); // 2. send out message, that's it Message message = buildMessage(...); producer.sendTransactionMessage(message); } } ``` The following is what make the magic happens provide that we make some change on TransactionMQProducer. ```java @Component class SpringTransactionMQProducer{ @Autowired TransactionMQProducer producer; @Autowired private ApplicationEventPublisher applicationEventPublisher; public sendTransactionMessage(Message message){ SendResult sendResult = producer.sendMessage(message); applicationEventPublisher.publishEvent(new TransactionMessageSendEvent(this, sendResult)); } @TransactionalEventListener(phase=AFTER_COMMIT) public void commitMessage(TransactionMessageSendEvent event){ producer.commitMessage(event.getSendResult()); } @TransactionalEventListener(phase=AFTER_ROLLBACK) public void rollbackMessage(TransactionMessageSendEvent event){ producer.rollbackMessage(event.getSendResult()); } } ``` ```java class TransactionMessageSendEvent extends ApplicationEvent{ SendResult sendResult; public TransactionMessageSendEvent(Object source, SendResult sendResult){ super(source); this.sendResult = sendResult; } public SendResult getSendResult(){ return sendResult; } } ```
---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected] With regards, Apache Git Services
