ROCKETMQ supports common messages, sequential messages, and also supports thing messages. The way to do this is to split a large transaction into multiple small things and execute them asynchronously, in which the message of things plays a role in bridging.
ROCKETMQ when sending a thing message, a prepared message is sent and the address of the message is returned. Then the local thing is executed, and the prepared message state is updated according to the result of the thing execution. Message receivers can only consume messages in a message cluster that have a message status of committed .
Things news Demo:
Transactionmqproducer producer = new Transactionmqproducer ("Unique_group_name");
Transactionchecklistener Transactionchecklistener = new Transactionchecklistenerimpl ();
Producer.settransactionchecklistener (Transactionchecklistener);
Transactionexecuterimpl tranexecuter = new Transactionexecuterimpl ();
Producer.start ();
Message msg =new message (...);
Sendresult Sendresult = producer.sendmessageintransaction (msg, tranexecuter, NULL);
When the acknowledgement message fails, ROCKETMQ periodically scans the message in the messages cluster, and if the prepared message is found, it will confirm to the message sender if the event is successful at this time to rollback or continue sending the acknowledgment message. The ROCKETMQ invokes the Transactionchecklistener implementation class to make the appropriate operation.
Transactionexecuterimpl–> the processing logic of local things
send a thing message source code (PSEUDOCODE)
Public Transactionsendresult sendmessageintransaction (...) {
//1. Send Message
sendresult Sendresult = this.send (msg);
Localtransactionstate localtransactionstate = Localtransactionstate.unknow;
Switch (Sendresult.getsendstatus ()) {
case SEND_OK: {
//2. If the message is sent successfully, the local transaction unit
associated with the message is processed Localtransactionstate = Tranexecuter.executelocaltransactionbranch (msg, arg);
}
3. End Transaction
this.endtransaction (Sendresult, Localtransactionstate, localexception);
}
Local thing State localtransactionstate:
Commit_message,
Rollback_message,
Unknow
public void Endtransaction (Sendresult sendresult, localtransactionstate Localtransactionstate, ...) {final String brokeraddr = This.mQClientFactory.findBrokerAddressInPublish (Sendresult.getmessagequeue (). Getbroker
Name ());
Endtransactionrequestheader Requestheader = new Endtransactionrequestheader (); Switch (localtransactionstate) {case COMMIT_MESSAGE:requestHeader.setCommitOrRollback (messag
Esysflag.transactioncommittype);
Break
Case ROLLBACK_MESSAGE:requestHeader.setCommitOrRollback (Messagesysflag.transactionrollbacktype);
Break
Case UNKNOW:requestHeader.setCommitOrRollback (Messagesysflag.transactionnottype);
Break
Default:break;
} this.mQClientFactory.getMQClientAPIImpl (). Endtransactiononeway (Brokeraddr, Requestheader, ...); }
Endtransaction gets the message address from the Sendresult, sends the request to the broker, and updates the final state of the thing message according to Localtransactionstate.
If the Endtransaction method fails, causing the data to not be sent to Broker,broker there will be a callback thread timed (default 1 minutes) to scan each stored transaction state of the table file, if the message has been committed or rolled back directly skip, If the Transactionnottype state is to initiate a checktransaction request to producer, producer calls Defaultmqproducerimpl.checktransactionstate ( ) method to handle the timing callback request of the broker, and Checktransactionstate invokes the decision method of our transaction settings, and finally calls Endtransactiononeway to let the broker update the final state of the message.
When sending a thing message, the GroupName must be set, and a producer is randomly selected according to the group when it is checked back.