|
|||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
IntroductionTalk of enterprise applications, things that come to your mind are transactions ,messaging etc. Talk of messaging, what strikes immediately is MSMQ which stands for Microsoft Message Queuing. MSMQ provides a great framework for applications that use messaging infrastructure. In this article I will discuss very elementary concepts of MSMQ programming like creating a queue, writing a message to the queue and receiving from it etc. Background
Let me take a couple of minutes here to explain what messaging is. Here, you have two applications: a sending application and a receiving application. A sending application prepares a message and puts it into a queue. A message can be any piece of information that the receiving application understands. The receiving application receives the message from the queue and processes it. One thing to note here is that the sender and the receiver are not tightly coupled and they can work without the other application being online. Enter System.MessagingTo build a messaging application we need to create a queue first, have one application send messages to it and have another application receive those messages from it. So, in this article I shall focus mainly on these operations. Before doing that I would like to mention about the basic types of MSMQ queues: Private and Public. Public queues are those that are published in the active directory. So, applications running on different servers throughout the network can find and use public queues through the active directory. Private queues on the other hand are available locally on a particular machine and are not published in the active directory. The Creating a QueueQueues can be created either programmatically or through the computer management snap-in. I presume MSMQ is installed on your system. ProgrammaticallyYou can use the Try
Dim queueAsMessageQueue
queue = MessageQueue.Create(".\Private$\MyNewQueue")
' If there is an error creating a queue you get a MessageQueueException exception
Catch ex As MessageQueueException
End Try
Note the parameter I have passed to the the Create method. It is called the path of the queue. The single dot (.) in the path indicates the queue is created on the local machine. Since we are creating a private queue here, we need to include a Private$ in the path. If we are creating or accessing a public queue we can just use the MachineName\QueueName format in the path. Through the computer management snap-in
Sending a simple messageWe use the MyQueue.Send("<<Message>>", "<<Message Label>>") One thing I would like to mention here is that the Send method takes an object as the first parameter to denote the message body. This can be a Reading a message from the queueThere are two types of operations with respect to reading a message fom the Queue: Peeking and Receiving. When we receive a message from the queue, the message is removed from the queue. Peeking is similar to Receiving but here the message is not removed from the queue. Dim msg As Message
msg = MyQueue.Receive()
MessageBox.Show(msg.Body)
There are many overloads of Receive and the one used above indefinitely blocks the caller till a message is available on the queue. On the other hand if you do not want to get blocked indefinitely then you can use the overload of Receive which takes a msg = MyQueue.Receive(New TimeSpan(1000))
We have seen that Peek and Receive operations are synchronous in nature. However there exists a method where we can do these operations asynchronously. For this, we can use the Deleting a QueueWe can use the MyQueue.Delete(".\Private$\MyNewQueue") You can also delete a queue from the computer management console. Right click on the queue you wish to delete and select Delete from the context menu.
Other simple queue operationsEnlisted below are some other simple operations on a queue which you might be interested in.
A VS.NET TipYou might have noticed that we have used a lot of the
About the SourceIncluded with this article is a VB.NET windows application project which has all the simple operations mentioned above implemented. Here's how it looks.
Towards the left of the screen in a treeview control enlisting all the private queues in the system. Messages are also listed under the respective queue nodes in the tree. The operations possible with this app are:
Please excuse me if you find bugs in the project :) ConclusionIn this article and in the VB.NET project that I have submitted, I have used only Private Queues. Also, I have not used transactional messaging. Please note the working with Public queues is almost identical but for some small changes in Path semantics. So, everything I have explained with respect to private queues will definitely hold good for public queues as well. I will be doing an injustice to MSMQ if I finish off everything in a single article. In this article we saw the very basics of MSMQ. In the forthcoming article we shall see some more advanced concepts like Transactional messaging, serializing and deserializing messages, asynchronous operations etc.
|
||||||||||||||||||||||||||||||||||