"Anatomy of the PetShop" Series III: PetShop data access Layer message processing

Source: Internet
Author: User
Tags connection pooling msmq

In the system design, in addition to security, business and other issues to give enough attention, performance is an unavoidable problem, especially a B/s structure of the software system, must fully consider the access, data flow, server load problems. To solve the bottleneck of performance, in addition to the hardware system upgrades, the rationality of software design is particularly important.

As I mentioned earlier, layered structure design can affect the performance of data access to some extent, but it is almost negligible compared to the benefits it brings to designers. To provide the performance of the entire system, you can also start from the optimization of the database, such as the use of connection pooling, indexing, optimization of query strategy, etc., such as in the PetShop in the use of the database cache, for large amounts of data on the order data, The order and inventory databases are created separately for them by means of a sub library. And in the software design, the more useful way is to use multithreading and asynchronous processing mode.

In PetShop4.0, Microsoft Messaging Queue (MSMQ) technology is used to complete asynchronous processing, using Message Queuing to temporarily hold data to be inserted so that data access provides access performance because it does not require access to the database, and for data in the queue, Wait for the system to be idle and then process it, eventually inserting it into the database.

The message processing in PetShop4.0 is mainly divided into the following parts: Message interface imessaging, message Factory messagingfactory, MSMQ implementation msmqmessaging and Data spooler application Orderprocessor.

From the modular point of view, PetShop fulfills the principle of "interface-oriented design", separates the interface of message processing from the implementation, and encapsulates the message to create the object through the Factory mode to achieve the goal of loose coupling.

Because only the processing of orders in PetShop uses asynchronous processing, in the message interface imessaging, only one Iorder interface is defined, and its class diagram is as follows:

In the implementation of the message interface, given that there will be other data objects in the future extension that will use MSMQ, a base class for queue is defined to implement the basic operations of message receive and send:

public virtual object Receive()
{
   try
{
     using (Message message = queue.Receive(timeout, transactionType))
       return message;
   }
   catch (MessageQueueException mqex)
{
     if (mqex.MessageQueueErrorCode == MessageQueueErrorCode.IOTimeout)
       throw new TimeoutException();
        throw;
   }
}
public virtual void Send(object msg)
{
   queue.Send(msg, transactionType);
}

Where the queue object is the System.Messaging.MessageQueue type, which is used to store the data. MSMQ queues are a durable queue, so you do not have to worry about the loss of order data due to user uninterrupted underground orders. The timeout value is set in Petshopqueue, and orderprocessor periodically scans the order data in the queue based on the timeout value.

In the Msmqmessaging module, the order object implements the interface Iorder defined in the Imessaging module, and it inherits the base class Petshopqueue, which is defined as follows:

public class Order:petshopqueue, PetShop.IMessaging.IOrder

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.