Implementation of semi-synchronous semi-asynchronous mode

Source: Internet
Author: User

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

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.