MSMQ Part 1/2: Architecture and Simple Implementation Using VB
MSMQ is a messaging solution that uses the store-forward style of distributed computing to share data across networks in terms of messages. In part one of this two part series, Liviu explains the architecture of an enterprise system in relation to MSMQ. He also shows us how to implement a simple message queuing application using Visual Basic 6 and the MSMQ COM objects.
MSMQ Part 1/2: Architecture and Simple Implementation Using VB - Creating, looking up and deleting queues (Page 4 of 5 )
In our example we've seen a quick way of locating, opening and creating a queue. On this page we will take a closer look at how to locate a queue using more than just PathName and an MSMQQueue object.
FormatName Property
If you look in MSDN under the MSMQQueueInfo object property, you will notice a property called FormatName. The "name" bit at the end of this property suggests that it might be somehow related to the PathName, and that's not far from the truth.
Using the FormatName property, we can access a certain queue more or less in the same way that we did with the PathName property. The difference is (apart from the format) that by using this approach, a client can provide disconnected messaging (i.e. not reliant on a SC).
Where the first part denotes the way the communication is achieved: PUBLIC (locating the queue via a SC), PRIVATE (locating a private queue), or DIRECT (bypass the SC – this is normally used for disconnected messaging, when we don't have access to the SC).
Protocol determines the protocol to be used for locating the machine holding the queue. Possible values are SPX, TCP or whatever other protocols are supported by MSMQ. In most of the cases the value will be TCP, because this is the standard protocol in an enterprise network. Queue is the queue name (if we considered our previous example, Queue would be "Greeting").
To get an idea of how queues are opened, we can use the following code to inspect the FormatName property:
Private Sub TestFormatName()
Dim qinfo As MSMQQueueInfo
Dim q As MSMQQueue
Set qinfo = New MSMQQueueInfo
qinfo.PathName = ".\PRIVATE$\Greeting2"
qinfo.Label = "Second Greeting Queue"
qinfo.Create
MsgBox "Queue format name is: " + qinfo.FormatName
Set q = qinfo.Open(MQ_SEND_ACCESS, MQ_DENY_NONE)
End Sub
Of course, you will have to replace the PathName with whatever path you're using to store your message in (in this case we are creating a private queue on the local machine, but the queue could be public and created on any server in your enterprise).
Deleting a queue
Deleting a queue is just as simple as creating one, and I won't go into to much detail about it. The only thing we have to do once we have located our queue is call the delete method of an MSMQQueueInfo object.
Here's a quick example on how to delete all queues called "Greeting".
Dim query As New MSMQQuery
Dim qinfos As MSMQQueueInfos
Dim qinfo As MSMQQueueInfo
Private Sub DelQ()
Set qinfos = query.LookupQueue(Label:="Greeting")
qinfos.Reset
Set qinfo = qinfos.Next
While Not qinfo Is Nothing
qinfo.Delete
Set qinfo = qinfos.Next
Wend
End Sub
As you may have noticed, we are using two new object types here: MSMQQuery and MSMQQueueInfos. Briefly, MSMQQuery allows us to query for queues. It has one single method called LookupQueue, which allows us to query the active directory for existing public queues. This method returns an MSMQQueueInfos object, which is basically a list of MSMQQueueInfo objects. If we reconsider the relationship diagram presented earlier on, here's what it looks like:
Locating a queue by name
As we’ve just seen on the last page, we have already discovered another way of locating a queue – by using the MSMQQuery object and calling its LookupQueue method. This method returns an MSMQQueueInfos object, which is a collection of MSMQQueueInfo objects. The syntax of the function includes quite a few parameters, however the most interesting (and most used) one is label, which allows us to specify the name of the queue we're after.
Furthermore, another very interesting parameter is the one called RelLabel, which allows us to define a relationship between the value we pass as the parameter Label and the actual label of the queue (i.e. equal, not equal, greater than etc.) The possible values for this parameter are:
REL_EQ Equal
REL_NEQ Not equal
REL_LT Less than
REL_GT Greater than
REL_GE Greater than or equal
REL_LE Less than or equal
For example, to retrieve all of the queues not labelled "Greeting", we could use the following piece of code:
Dim Qs As MSMQQueueInfos
Dim MQ As New MSMQQuery
Set Qs = MQ.LookupQueue( Label:="Greeting", RelLabel:=REL_NEQ)
Once we get hold of the MSMQQueueInfos object, we can iterate through its items using the Next method, as seen on the previous page. However, remember before starting to iterate through an MSMQQueueInfos object, we must call its reset method to reset the position of the pointer right back to the beginning of the list.
So, getting back to our receiver example, here's another way to locate a queue called "Greeting":
Dim MQ As New MSMQQuery
Dim QS As MSMQQueryInfos
Dim QI As MSMQQueryInfo
Dim Q As MSMQQueue
Set QS = MQ.LookupQueue( Label:="Greeting") 'default relation is REL_EQ
QS.Reset
Set QI = QS.Next
Set Q = QI.Open( MQ_RECEIVE_ACCESS, MQ_DENY_NONE )