MSMQ Processing of multiple pieces of data

You want to process a bunch of  data associated with a single MSMQ message.  First, create a class with the Serializable attribute.

[Serializable()]

public class cSearchUrl

{

      public string Url = String.Empty;

      public string ServerName = String.Empty;

      public long Id = 0;

}

Post the message

MessageQueue mq = new MessageQueue(gstrQueueForSearchResults);

cSearchResults objRes = new cSearchResults();

try

{

      objRes.UrlText = pText;

      objRes.Url = pstrUrl;

      objRes.Id = plngUrl;

      //Don't need this for the specific entry, but it is here.

      objRes.ServerName = cCommon.CalculateServerName(pstrUrl);

      mq.Send(objRes);

}

finally

{

      mq.Dispose();

      mq = null;

}

Then, I need to get the data on the other end.  Here is my setup code.

gMQSearchResults = new MessageQueue();

gMQSearchResults.Path = gstrQueueForSearchResults;

gMQSearchResults.Formatter = new XmlMessageFormatter(new Type[] {typeof(WebSearch.cSearchResults)});

gMQSearchResults.ReceiveCompleted += new ReceiveCompletedEventHandler(this.MQ_ReceiveCompleted_ForSearchResults );

gMQSearchResults.BeginReceive();

Here is my processing code.

private void MQ_ReceiveCompleted_ForSearchResults(object sender, ReceiveCompletedEventArgs e)

{

// Add code here to respond to message.

System.Messaging.Message msg = gMQSearchResults.EndReceive(e.AsyncResult);

WebSearch.cSearchResults cSearchRes;

try

{

      msg.Formatter = new XmlMessageFormatter(new Type[] {typeof(WebSearch.cSearchResults)}); //= new XmlMessageFormatter(new Type() {GetType(WebSearchSupport.cSearchResults)}); //new XmlMessageFormatter(new String(){"System.String, mscorlib"});

      cSearchRes = (WebSearch.cSearchResults)msg.Body;

      StoreUrlInSearchResults(cSearchRes);

}

      //Exception handling code

finally

{

      msg.Dispose();

      msg = null;

      if ( this.gblRunStatus == true )

      {

            gMQSearchResults.BeginReceive();

      }

}

 

}

I hope that this helps.  If you have any suggestions, let me know.

Wally

No Comments