Pro Visual C++ 2005 for C# Developers

Before diving in to programming Message Queuing, this section discusses the basic concepts of messaging and compares it to synchronous and asynchronous programming. With synchronous programming, when a method is invoked the caller has to wait until the method is completed. With asynchronous programming the calling thread starts the method that runs concurrently. Asynchronous programming can be done with delegates, class libraries that already support asynchronous methods (for example, Web service proxies, System.Net, System.IO classes), or by using custom threads (see Chapter 13, "Threading"). With both synchronous and asynchronous programming the client and the server must be running at the same time.

Although Message Queuing operates asynchronously, because the client (sender) does not wait for the server (receiver) to read the data sent to it, there is a crucial difference between Messaging Queuing and asynchronous programming: Message Queuing can be done in a disconnected environment. At the time data is sent, the receiver can be offline. Later, when the receiver goes online, it receives the data without the sending application to intervene.

You can compare connected and disconnected programming with talking to someone on the phone and sending an e-mail. When talking to someone on the phone, both participants must be connected at the same time. The communication is synchronous. With an e-mail, the sender isn't sure when the e-mail will be dealt with. People using this technology are working in a disconnected mode. Of course thee-mail may never be dealt with — it may be ignored. That's in the nature of disconnected communication. To avoid this problem it is possible to ask for a reply to confirm that the e-mail has been read. If the answer doesn't arrive within a time limit, you may be required to deal with this "exception." This is also possible with Message Queuing.

In some ways Message Queuing is e-mail for application-to-application communication, instead of person-to-person communication. However, this gives you a lot of features that are not available with mailing services, such as guaranteed delivery, transactions, confirmations, express mode using memory, and so on. As you see in the next section, Message Queuing has a lot of features useful for communication between applications.

With Message Queuing you can send, receive, and route messages in a connected or disconnected environment. Figure 31-1 shows a very simple way of using messages. The sender sends messages to the message queue, and the receiver receives messages from the queue.

Figure 31-1

When to Use Message Queuing

One case where Messaging Queuing is useful is when the client application is often disconnected from the network (for example with a salesperson visiting customers onsite). The salesperson can enter order data directly at the customer's site. The application sends a message for each order to the message queue that is located on the client's system (see Figure 31-2). As soon as the salesperson is back in the office, the order is automatically transferred from the message queue of the client system to the message queue of the target system where the message is processed.

Figure 31-2

In addition to using a laptop, the salesperson could use a Pocket Windows device where Message Queuing is available.

Message Queuing can also be useful in a connected environment. Imagine an e-commerce site (see Figure 31-3) where the server is fully loaded with order transactions at certain times, for example early evening and weekends, but the load is low at night time. A solution would be to buy a faster server or to add additional servers to the system so that the peaks can be handled. But there's a cheaper solution: Flatten the peak loads by moving transactions from the times with higher loads to the times with lower loads. In this scheme, orders are sent to the message queue, and the receiving side reads the orders at the rates that are useful for the database system. The load of the system is now flattened over time so that the server dealing with the transactions can be less expensive than an upgrade of the database server(s).

Figure 31-3

Message Queuing Features

Message Queuing is part of the Windows operating system. The main features of this service are as follows:

The reminder of this chapter discusses how these features can be used.

Категории