Thursday, August 31, 2006

Programming MSMQ in .NET

Programming MSMQ in .NET

The System.Messaging namespace provides a set of classes which can be used to work with MSMQ. In this article we will be mainly focusing on Message and MessageQueue classes. The MessageQueue class provides all the necessary functionality to work with and manipulate MSMQ queues. It is like a wrapper around message queuing. The Message class provides everything required to define and use an MSMQ message.

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

MyQueue.Send("<<Message>>", "<<Message Label>>")


Dim msg As Message

msg = MyQueue.Receive()

MessageBox.Show(msg.Body)


MyQueue.Delete(".\Private$\MyNewQueue")


Message could be lost or duplicated. Thats why we need to create the transactional message queue.

queue = MessageQueue.Create(".\Private$\TranQueueA ", TRUE)

Dim mqTran As New MessageQueueTransaction()

Dim queueA As New MessageQueue()

queueA.Path = ".\Private$\TranQueueA"

Dim queueB As New MessageQueue()

queueB.Path = ".\Private$\TranQueueB"

mqTran.Begin()

Try

queueA.Send("Message A", "Label A", mqTran)

queueB.Send("Message B", "Label B", mqTran)

mqTran.Commit()

Catch ex As Exception

mqTran.Abort()

Finally

queueA.Close()

queueB.Close()

End Try


The Send and Receive methods of the MessageQueue class also expose overloads which takes a parameter of type MessageQueueTransactionType which is an enumeration of certain values. This basically specifies how you would like to interact with the queue (transactionally). We have not used it thus far, but is important to understand why and where this is used.

The enumeration contains three values:

Single

You might often come across situations where you want to have each queue operation in a separate internal transaction. That is, you may not want to use MessageQueueTransaction object to qualify each MSMQ operation. In such cases you can use the Single option. Here's an example:

Dim queueA As New MessageQueue()

queueA.Path = ".\Private$\TranQueueA"

queueA.Send("Single internal transactional message A", "Label A", _

MessageQueueTransactionType.Single)

queueA.Close()

Understand that, to send a message to a transactional queue, you have to be in a transaction (internal or external), else an error will be thrown.

None

Using this option enables us to receive a message from a transactional queue, but outside a transaction. We can also use this option to send a transactional message to a non transactional queue.

Automatic

This option is used in conjunction with external transactions. This directs the send or receive operation to use an already existing transaction context created by COM+, MTS etc. This is shown in the next section.

Tony

No comments:

Be A Developer That Uses AI

Developers will not be replaced by AI, they'll be replaced by developers that use AI. Generative AI tools are revolutionizing the way de...