This is a mirror of official site: http://jasper-net.blogspot.com/

Microsoft Message Queuing – A simple multithreaded client and server

| Sunday, March 13, 2011
Introduction

This article shows how to create a very simple client-server solution using Microsoft Message Queuing – seems like there is a need for something very simple, just the basic stuff to get you up and running with a moderately performing solution.

This solution can easily handle 500 000 moderately complex messages in three minutes.

Screenshot of the Microsoft Message Queuing client.

Background

Over in the Q&A section, a question was posted requesting help with a solution that needed to be capable of handling 1500 messages each minute. 1500 messages shouldn’t be a problem – but a simple answer on how to achieve this is still somewhat more complex then what I’d like to post as a reply to a question. I searched a bit around CP, and to my surprise I didn’t find an existing article illustrating the approach I had in mind.

Screenshot of the Microsoft Message Queuing server.

Usually the server part would be implemented as a Windows service.

A brief look at some of the code

Objects of the Payload class is what we are going to send from the client to the server. The class is simple, but not overly so.

[Serializable]
public class Payload
{
 private Guid id;
 private string text;
 private DateTime timeStamp;
 private byte[] buffer;

 public Payload()
 {
 }
 public Payload(string text, byte bufferFillValue, int bufferSize )
 {
  id = Guid.NewGuid();
  this.text = text;
  timeStamp = DateTime.UtcNow;
  buffer = new byte[bufferSize];
  for (int i = 0; i < bufferSize; i++)
  {
   buffer[i] = bufferFillValue;
  }
 }
 // The rest is just properties exposing the fields
}
Calling the UI thread

Most of the interesting stuff happens in worker threads, to interact with components in the UI thread we use the familiar InvokeRequired/Invoke pattern.

private delegate void LogMessageDelegate(string text);
private void LogMessage(string text)
{
 if (InvokeRequired)
 {
  Invoke(new LogMessageDelegate(LogMessage), text);
 }
 else
 {
  // Don't do this in a deployment scenario - use something like
  // Log4Net. This is here just to illustrate interaction with 
  // the UI thread.
  messageTextBox.AppendText(text + Environment.NewLine);
 }
}

Read more: Codeproject

Posted via email from Jasper-net

0 comments: