Using MSMQ in ASP

By using the MSMQ server-side components that we discussed in the previous section, we can write ASP code that works with MSMQ to send messages, receive messages, and even perform transactional messaging.

As an example of transactional messaging, we might want to make an update to SQL Server and also send an MSMQ message all as part of one transaction. As we shall see in this section, MSMQ messages can be sent to queues that support transactions and are managed by MTS. We'll also see how easy it is to send and receive messages from MSMQ using ASP code.

Sending a Message

To send an MSMQ message using ASP code, we need to create two MSMQ objects: MSMQQuery and MSMQMessage. The MSMQQuery object allows you to query MSMQ for existing public queues. The MSMQMessage object allows you to send your message. The code below shows how to search for an existing queue and send a message.

 <%@ Language=VBScript %> <HTML> <HEAD> <META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0"> <TITLE>Send MSMQ Message</TITLE> </HEAD> <BODY> <H2>Send MSMQ Message</H2> <HR> <% Set Query = Server.CreateObject("MSMQ.MSMQQuery.1") Set QInfos = Query.LookupQueue(,,"MyQueue") QInfos.Reset Set QInfo = QInfos.Next Set Queue = QInfo.Open(2,0) Set Message = Server.CreateObject("MSMQ.MSMQMessage.1") Message.Label = "MSMQ Message" Message.Body = "This message was sent by SendMessage.asp." Message.Send Queue Response.Write "Message Sent. Please check MSMQ Explorer." %> </BODY> </HTML> 

In the above code, we use the Server.CreateObject syntax to create both the MSMQQuery object and the MSMQMessage object. The LookupQueue method of the MSMQQuery object is used to locate the queue named MyQueue within MSMQ.

The message is sent by simply setting various properties of the MSMQMessage object. These properties include the message label and the message body. Finally, the message is sent by calling the Send method of the MSMQMessage object.

In this example, the ASP Web page displays a confirmation message to the browser indicating that the message was sent. In a real application, you would probably not notify the user that a message has been sent since this is typically back-end functionality that the end users do not need to be concerned with.

Figure 21-8 shows the resulting message within the MSMQ Explorer.

click to view at full size.

Figure 21-8. Message sent from SendMessage.ASP shown within the MSMQ Explorer.

In Figure 21-8, you can see some basic information about the message. You can see the message label, the priority, the message ID, the message position in the queue, and the size of the message in bytes.

For more information about the message, you can right-click the message and choose Properties from the context menu. By clicking the Body tab of the Message Properties dialog box, you can see the actual text within the body of the message. In this example, as shown in Figure 21-9 on the following page, the text reads "This message was sent by SendMessage.asp."

NOTE
Another way to view the text within the body of the message is to choose Columns from the View menu of the MSMQ Explorer. Next choose Body from the list of available columns, and choose Add. This will add this particular column to the list of columns that are displayed within the MSMQ Explorer. There are many other available columns to choose from that allow you to customize your view of messages within the MSMQ Explorer. The Columns dialog box also allows you to customize your view of queues, computers, and sites within the MSMQ Explorer.

Figure 21-9. The Body tab of the Message Properties dialog box showing the text within the body of the message.

Receiving Messages

In addition to sending messages, you can also write ASP code to read and/or receive messages. The following code shows how this is achieved.

 <%@ Language=VBScript %> <HTML> <HEAD> <META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0"> <TITLE>Read MSMQ Message</TITLE> </HEAD> <BODY> <H2>Read MSMQ Message</H2> <HR> <% Set Query = Server.CreateObject("MSMQ.MSMQQuery.1") Set QInfos = Query.LookupQueue(,,"MyQueue") QInfos.Reset Set QInfo = QInfos.Next Set Queue = QInfo.Open(1,0) Set Message = Queue.Receive Response.Write "Message Detail<P>" Response.Write "Message Label: " + Message.Label + "<BR>" Response.Write "Message Body:  " + Message.Body %> </BODY> </HTML> 

The code is similar to our earlier code in which we sent a message. The main difference is that we call the Receive method of the MSMQQueue object instead of calling the Send method of the MSMQMessage object. Calling the Receive method actually creates a message object for us that we can then use to inspect the message properties. The code above prints the message label and the message body properties on screen. Also note that the Open method has been called using a parameter of 1. This value means that the queue is opened for read access instead of write access. Figure 21-10 shows the resulting browser output when running this sample code.

click to view at full size.

Figure 21-10. Sample output from the ReadMessage.ASP file that opens up a queue in MSMQ and reads the first message.

NOTE
To read a message in a queue, you might need to alter the security permissions on the queue. You may well encounter a message such as "MSMQQueueInfo error 'c00e0025' - Access is denied". This is because by default only the queue owner has read access to messages in the queue. A simple way to open up the security during development of your code is to choose the Security tab from the Queue Properties dialog box and go into the Permissions section. Next change the permissions for Everyone from Send (SqGpPg) to Special Access (All). This setting is obviously not advised for production usage, but it is a simple way to be able to read the messages during your development and experimentation with MSMQ.

Transactional Messaging

To send a transactional message using ASP code, you simply follow the same basic procedure as in "Sending a Message" but add a few lines of code to declare the Web page as transactional and your message as transactional. The code listing below shows how to search for an existing transactional queue and send a message.

There are a number of differences here when compared to the code in "Sending a Message." First, the page is declared as transactional by using this syntax:

 <%@TRANSACTION=Required LANGUAGE=VBScript%> 

Next, the event handlers at the bottom of the code listing are used to trap either the OnTransactionCommit event or the OnTransactionAbort event and print out an appropriate message on screen.

The one change to the main code body in the code is that the Send method is called with an additional parameter. The parameter value of 1 after the Send method indicates that the call is part of the current MTS transaction. The value of 1 is equivalent to the MQ_MTS_TRANSACTION value.

 <%@TRANSACTION=Required LANGUAGE=VBScript%> <HTML> <HEAD> <META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0"> <TITLE>Send Transactional MSMQ Message</TITLE> </HEAD> <BODY> <H2>Send Transactional MSMQ Message</H2> <HR> <% Set Query = Server.CreateObject("MSMQ.MSMQQuery.1") Set QInfos = Query.LookupQueue(,,"mytransqueue") QInfos.Reset Set QInfo = QInfos.Next Set Queue = QInfo.Open(2,0) Set Message = Server.CreateObject("MSMQ.MSMQMessage.1") Message.Label = "Transactional MSMQ Message" Message.Body = _     "This message was sent by SendTransactionalMessage.asp." Message.Send Queue, 1 Queue.Close %> </BODY> </HTML> <%  Sub OnTransactionCommit     Response.Write _         "Transactional MSMQ Message Sent and Committed." End Sub  %> <% Sub OnTransactionAbort     Response.Write "Transactional MSMQ Message Aborted." End Sub  %> 

NOTE
To send a transactional message, you must be sure to locate and use a transactional queue. If you have not yet set up a transactional queue within the MSMQ Explorer, you can do so by creating a new queue and marking the Transactional check box within the Queue Name dialog box at the time you create the queue. Once you have created a queue, you cannot change its transactional property from nontransactional to transactional or vice versa. You need to mark the queue as transactional during queue creation.



Programming Microsoft Visual InterDev 6. 0
Programming Microsoft Visual InterDev 6.0
ISBN: 1572318147
EAN: 2147483647
Year: 2005
Pages: 143
Authors: Nicholas D Evans, Ken Miller, Ken Spencer
BUY ON AMAZON

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net