A Wrapper Around Messaging Queue

by sanket 27. April 2009 12:30
Came across a need to write a utility using Messaging Queue. It is kind of a Publisher Subscriber, but it not durable messaging at all. Just a basic beginning for any body who wants to use MSMQ, can use the following code.
   1:  using System;
   2:  using System.Collections.Generic;
   3:  using System.Messaging;
   4:   
   5:  namespace Coderslog.Utilities.MsmqUtilities
   6:  {
   7:      public class Queue
   8:      {
   9:          private MessageQueue                _internalQueue;
  10:          private readonly List<GetMessage>   _messageSubScribers = new List<GetMessage>();
  11:   
  12:          public static Queue CreateQueue(string queueUri)
  13:          {
  14:              var queueInst = new Queue
  15:                                  {
  16:                                      _internalQueue =
  17:                                          !MessageQueue.Exists(queueUri)
  18:                                              ? MessageQueue.Create(queueUri)
  19:                                              : new MessageQueue(queueUri)
  20:                                  };
  21:              return queueInst;
  22:          }
  23:   
  24:          private void _internalQueue_ReceiveCompleted(object sender, ReceiveCompletedEventArgs e)
  25:          {
  26:              if (e.Message == null || !(e.Message.Body is QueueMessage)) return;
  27:              foreach (var subscriber in _messageSubScribers)
  28:              {
  29:                  if (subscriber != null && subscriber.Target != null)
  30:                      subscriber((QueueMessage) e.Message.Body);
  31:              }
  32:          }
  33:   
  34:          /*To Force Users to create a queue using the above method*/
  35:          private Queue()
  36:          {
  37:              _internalQueue.ReceiveCompleted += _internalQueue_ReceiveCompleted;
  38:          }
  39:   
  40:          public void PutMessage(QueueMessage message)
  41:          {
  42:              _internalQueue.Send(message);
  43:          }
  44:   
  45:          public void Subscribe(GetMessage messageReciver)
  46:          {
  47:              _messageSubScribers.Add(messageReciver);
  48:          }
  49:   
  50:          public void InitRecieving()
  51:          {
  52:              _internalQueue.BeginReceive();
  53:          }
  54:      }
  55:   
  56:   
  57:      public delegate void GetMessage(QueueMessage message);
  58:   
  59:      [Serializable]
  60:      public class QueueMessage
  61:      {
  62:          public string Header { get; set; }
  63:          public string Body { get; set; }
  64:      }
  65:  }

Currently rated 5.0 by 1 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

General

Comments are closed

Inida

About Coderslog

One fine day we thought, we need to be contributing to our community.

So good sense prevailed, and that's how coderslog got started. That's about us.

Calendar

<<  March 2010  >>
MoTuWeThFrSaSu
22232425262728
1234567
891011121314
15161718192021
22232425262728
2930311234

View posts in large calendar

RecentPosts

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2010 CodersLog