Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

MSMQ for .NET Developers (Part 1) : Page 5

MSMQ provides robust and easy-to-use messaging capabilities that let you send and receive messages between applications running on one machine, different machines, and even across the Internet, both synchronously and asynchronously. Unfortunately, MSMQ is notoriously underused. Find out how to get started with MSMQ and what you can do with it.


advertisement
MSMQ Administration in .NET
MSMQ provides surprising simple management of MSMQ queues. You can can create, delete, and purge queues with little effort. Further, you can set many queue properties programmatically, including permissions assignment.

Creating, Deleting, and Purging Queues
To create a queue, simply call the shared CreateQueue method, passing the pathname identifying the queue. Similarly, you can delete and purge (remove all messages) queues . See the code samples below.

' create a private queue on the local machine named DevXTestQueue MessageQueue.Create(".\private$\DevXTestQueue") ' delete the private queue on the local machine ' named DevXTestQueue MessageQueue.Delete(".\private$\DevXTestQueue") ' purge the private queue on the local machine ' named DevXTestQueue MessageQueue.Purge(".\private$\DevXTestQueue")

Testing for the Presence of a Queue
MSMQ provides another shared method named Exists, which you can use to test for the presence of a queue. The only method parameter is the pathname of the queue. This method is quite expensive, so it is not a method you should use in your code during the normal course of message processing. The expense is incurred because, except for local private queues, the Exists method queries AD for information about the specified queue. Remember that because the method uses AD to find queues, you can't use Exists to test for the presence of remote private queues—you can run queries for private queues only on the machine where the queue resides. The code sample below demonstrates using Exists before attempting to create a queue.

' Using Exists to Test for the Presence of a Queue Private Function CreateQueue(ByVal PathName As String) _ As MessageQueue If MessageQueue.Exists(PathName) = True Then Return New MessageQueue(PathName) Else Return MessageQueue.Create(PathName) End If End Function

Setting Permissions on Queues
Until .NET made the System.Messaging functionality of MSMQ available, VB developers found it difficult to set various properties of queues. One common change made to a queue after it is created is the assignment of permissions to the queue. While there are many ways to programmatically assign permissions to a queue, the most straightforward way to do this is to call the SetPermissions method on the appropriate queue object. By passing a string representing the user to whom permission should be granted (or revoked), the permission that is being affected, and an action (Set, Revoke, Allow, or Deny), you can set the permissions as intended. Unlike the Exists method, you can set permissions on both local and remote queues and on both public and private queues.

The code below demonstrates granting full control permissions (and subsequently revoking the same permission) to the user michael.jones@passporthealth.com on a queue named DevXTestQueue.



' Using SetPermissions to Grant and Revoke Permissions on a Queue myQueue = New MessageQueue(".\private$\DevXTestQueue) myQueue.SetPermissions("michael.jones@passporthealth.com", _ MessageQueueAccessRights.FullControl, AccessControlEntryType.Set) myQueue = New MessageQueue(".\private$\DevXTestQueue) myQueue.SetPermissions("michael.jones@passporthealth.com", _ MessageQueueAccessRights.FullControl, _ AccessControlEntryType.Revoke)

Enumerating Queues on a Machine
Tools available in System.Messaging also have several methods that can enumerate the public and private queues on a specified machine or all queues across an entire domain. Two of the most useful of these methods are GetPublicQueuesByMachine and GetPrivateQueuesByMachine. As their names suggest, you provide the name of a machine, and they both return an array of MessageQueue objects present on the specified machine. The code below demonstrates this functionality.

' Using GetPublicQueuesByMachine and GetPrivateQueuesByMachine to ' Enumerate Queues myQueues = MessageQueue.GetPublicQueuesByMachine(".") Debug.WriteLine("---- " & myQueues.GetLength(0).ToString & _ " Queues ----") For Each myQueue As MessageQueue In myQueues Debug.WriteLine(myQueue.QueueName) Next myQueues = MessageQueue. GetPrivateQueuesByMachine("SERVER1") Debug.WriteLine("---- " & myQueues.GetLength(0).ToString & _ " Queues ----") For Each myQueue As MessageQueue In myQueues Debug.WriteLine(myQueue.QueueName) Next



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date