The so-called semi-synchronous semi-asynchronous means that in a method call, some code lines are synchronous execution methods and some code lines are asynchronous execution methods. Here is an example, for example, haha.
The main logic of PlaceOrder:
}
Let's make the following decision:
}
As shown above, if we only use Async (multi-thread, msmq, rabbitq) in "adding OrderInfo to Warehouse", if only this asynchronous execution is triggered, when the function returns, it is very likely that this asynchronous operation has not been completed yet, but the UI Layer (or other functions) needs some information, such as OrderID. Therefore, in this function, in addition to initiating an asynchronous call, wait is also required until the asynchronous call is completed. In fact, this function is equivalent to completing two threads for parallel execution, however, the final return condition must be that the execution of both threads is complete.
Let's take a look.
EventWaitHandle signal = EventWaitHandle( OrderInfo returnedOrderInfo = msgId= returnedOrderInfo = ThreadPool.QueueUserWorkItem( signal.WaitOne(); (.returnedOrderInfo != && !
Note that the e-mail address must be placed at the end. Imagine that the database insertion fails, and The msmq response queue will return the corresponding failure message, in this case, you need to determine whether to send an email based on the result (this is not taken into account in the above Code ).
In consideration of power failure, you need to create a Transaction queue for msmq as follows:
OrderQueueConnectionString = OrderResponseQueueConnectionString = MessageQueue.Create(OrderResponseQueueConnectionString, } }
As you can see, there are actually two queues to be created. One is the sending queue and the other is the response queue; the subthread in ThreadPool is used to Monitor whether there is a message in the response queue. Let's look at the Code:
CheckResponseQueue( msgId = ( sMessageConnectionString_ResponseQueue == = XmlMessageFormatter( Type[] { (= foundMsgId = (Message msg (msg.Label === (foundMsgId != = OrderInfo replyOrderInfo== returnedOrderInfo.FirstName ==== }
Next let's take a look at the real background processing program (a Console program) after the message is sent ):
Main( msmq = (!= = mq.Formatter = XmlMessageFormatter( Type[] { (==+=+= (msg.ResponseQueue != { Message msg_reply = === System.Messaging.XmlMessageFormatter( Type[] { msg.ResponseQueue.Send(msg_reply, tx); tx.Commit(); } Console.WriteLine(
Let's take a look at the main program:
Main(= = = = + += success= ++
Now let's take a look:
Code