Title: Asynchronous Processing and COM
1Chapter 8
- Asynchronous Processing and COM
2Objectives
- Explain asynchronous processing
- Identify the need for asynchronous processing
- Discuss different ways of implementing
asynchronous processing - MSMQ
- COM Queued Components
3Types of Processing
4Reasons for Asynchronous Processing
5Ways of Implementing Asynchronous Processing
Develops asynchronous message-oriented Windows-
based applications
MSMQ
Follows a Recorder-Listener-Player architecture
COM Queued Components
Based upon loosely coupled events and models the
Publisher-Subscriber architecture
COM Events
6Messaging
Sender
Receiver
7Messaging (1)
Advantages
8Messaging (2)
Drawbacks
9Elements of Message-Oriented Middleware
10MSMQ MMC snap-in
MSMQ is an example of a MOM. It makes loosely
coupled asynchronous communication possible
between application components.
11Parts in MSMQ Application
Object that stores the message temporarily
Application component that sends the message
Application component that receives the message
12Object Types in MSMQ
13Parts of MSMQ Messages
14Queues in MSMQ
Queue Temporary storage area for messages until
they reach their destination
15Queue Types in MSMQ
16Application using MSMQ
Server Component (MSMQServer.cs)
using System.Messaging . pu
blic static void Main (String args) string
mqPath ".\\" args 0 / If Message Queue
does not exist, create it / if
(MessageQueue.Exists (mqPath) false)
MessageQueue.Create (mqPath) / Create
a MessageQueue Object for that Queue
/ MessageQueue mq new MessageQueue
(mqPath) / Send the Message to this Queue
/ mq.Send (args 1)
17Application using MSMQ (1)
Client Component (MSMQClient.cs)
using System.Messaging public static
void Main (String args) string mqPath
".\\" args 0 / If such a Message Queue
does not exist, give an error Message and Exit
/ if (MessageQueue.Exists (mqPath) false)
return / Create a
Queue / MessageQueue mq new MessageQueue
(mqPath) / Set the formatter for a
message read from the queue. / mq.Formatter
new XmlMessageFormatter(new String"System.Str
ing,mscorlib")
18Application using MSMQ (2)
Client Component (MSMQClient.cs) contd.
/ Set a Message Handler for this Queue
/ mq.ReceiveCompleted new ReceiveCompletedEve
ntHandler(MessageArrived) mq.BeginReceive
() .. / The Message Handler
for receiving Messages Asynchronously
/ public static void MessageArrived (Object
obj, ReceiveCompletedEventArgs asyncRcvd)
MessageQueue mq (MessageQueue) obj Message
msg mq.EndReceive(asyncRcvd.AsyncResult)
19Application using MSMQ (3)
Client Component (MSMQClient.cs) contd.
/ Display the Received Message
/ Console.WriteLine ("The Message Received
Reads " (string)msg.Body) mq.BeginReceive
()
20MMC snap-in after executing the Server Component
21COM Queued Components
Listener
Picks up message from queue and forwards it to
player
Invokes methods on server
MSMQ Queue
Player
Plays message to server component
Message is put in queue
Recorder
Call is recorded and the message is passed
22Application using COMQC
Server Component (QCserver.cs)
using System.EnterpriseServices .. //
Two types for application activation, library and
server assembly ApplicationActivation(Activatio
nOption.Server) // Strong name key
file assembly AssemblyKeyFile("d\\QCDemo.snk")
// Enables queuing support for the COM
component. assembly ApplicationQueuing(Enabledt
rue, QueueListenerEnabledtrue)
23Application using COMQC (1)
Server Component (QCserver.cs) contd.
/ This application only requires an MSMQ
workgroup installation and is not set up to work
with a domain controller. / assembly
ApplicationAccessControl(Valuefalse,
AuthenticationAuthenticationOption.None) names
pace QCDemo / InterfaceQueuing enables
queuing support for the IQComp interface. Calls
on the interface will be queued using MSMQ /
InterfaceQueuing public interface IQComponent
void DisplayMessage(string msg)
24Application using COM QC (2)
Server Component (QCserver.cs) contd.
public class QComp ServicedComponent,
IQComponent public void
DisplayMessage(string msg) MessageBox.Show(
msg, "Component Processing Message")
25Application using COM QC (3)
Client Component (QCclient.cs)
using System.Runtime.InteropServices . p
ublic static void Main(string args)
IQComponent iQC . /
invoke an instance of the queued component using
a queue moniker this provides a client reference
that is called in as if it were an actual
instance of the server component / iQC
(IQComponent) Marshal.BindToMoniker("queue/newQC
Demo.QComp") / If we're not connected to an
activated server object, this will place a
packaged message in the qcserver queue.
/ iQC.DisplayMessage (args0)
26Application using COM QC (4)
Client Component (QCclient.cs) contd.
/ appropriate method for releasing our queued
component / Marshal.ReleaseComObject(iQC)
27MMC snap-in displaying QCserver