شركة التطبيقات المتكاملة لتصميم النظم البرمجية الخاصة

Integrated Applications Programming Company

Skip Navigation LinksHome » Code Library » Msmq

Public general use code classes and xml files that we've compiled and used over the years:

MSMQ (Microsoft Message Queuing) Support class.

   1:  using System;
   2:  using System.Collections.Generic;
   3:  using System.Messaging;
   4:   
   5:  namespace Ia.Cl.Model
   6:  {
   7:      ////////////////////////////////////////////////////////////////////////////
   8:   
   9:      /// <summary publish="true">
  10:      /// MSMQ (Microsoft Message Queuing) Support class.
  11:      /// </summary>
  12:      /// <remarks> 
  13:      /// Copyright © 2015-2019 Jasem Y. Al-Shamlan (info@ia.com.kw), Integrated Applications - Kuwait. All Rights Reserved.
  14:      ///
  15:      /// This library is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by
  16:      /// the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
  17:      ///
  18:      /// This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
  19:      /// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  20:      /// 
  21:      /// You should have received a copy of the GNU General Public License along with this library. If not, see http://www.gnu.org/licenses.
  22:      /// 
  23:      /// Copyright notice: This notice may not be removed or altered from any source distribution.
  24:      /// </remarks> 
  25:   
  26:      ////////////////////////////////////////////////////////////////////////////
  27:   
  28:      /// <summary>
  29:      ///
  30:      /// </summary>
  31:      public static class Msmq
  32:      {
  33:          ////////////////////////////////////////////////////////////////////////////
  34:   
  35:          /// <summary>
  36:          /// References public queues.
  37:          /// </summary>
  38:          [Obsolete("Code does not work property and/or is not tested. ")]
  39:          public static void SendPublic(string queueName, string label, string text)
  40:          {
  41:              string path;
  42:   
  43:              path = @"FormatName:DIRECT=OS:Ia\" + queueName;
  44:   
  45:              using (MessageQueue mq = new MessageQueue())
  46:              {
  47:                  try
  48:                  {
  49:                      if (MessageQueue.Exists(path)) mq.Path = path;
  50:                      else MessageQueue.Create(path);
  51:   
  52:                      mq.Send(text, label);
  53:                  }
  54:                  catch (MessageQueueException mqe)
  55:                  {
  56:                  }
  57:                  finally
  58:                  {
  59:                      mq.Dispose();
  60:                  }
  61:              }
  62:          }
  63:   
  64:          ////////////////////////////////////////////////////////////////////////////
  65:   
  66:          /// <summary>
  67:          ///
  68:          /// </summary>
  69:          [Obsolete("Code does not work property and/or is not tested. ")]
  70:          public static void RecievePublic(string queueName, out string label, out string body)
  71:          {
  72:              System.Messaging.Message m;
  73:   
  74:              m = RecievePublic(queueName);
  75:   
  76:              if (m != null)
  77:              {
  78:                  label = m.Label;
  79:                  body = (string)m.Body;
  80:              }
  81:              else
  82:              {
  83:                  label = body = string.Empty;
  84:              }
  85:          }
  86:   
  87:          ////////////////////////////////////////////////////////////////////////////
  88:   
  89:          /// <summary>
  90:          ///
  91:          /// </summary>
  92:          [Obsolete("Code does not work property and/or is not tested. ")]
  93:          public static System.Messaging.Message RecievePublic(string queueName)
  94:          {
  95:              string path;
  96:              System.Messaging.Message m;
  97:   
  98:              path = @".\" + queueName;
  99:   
 100:              using (MessageQueue mq = new MessageQueue())
 101:              {
 102:                  try
 103:                  {
 104:                      if (MessageQueue.Exists(path))
 105:                      {
 106:                          mq.Path = path;
 107:   
 108:                          mq.Formatter = new XmlMessageFormatter(new Type[] { typeof(string) });
 109:   
 110:                          m = mq.Receive();
 111:                      }
 112:                      else
 113:                      {
 114:                          m = null;
 115:                      }
 116:                  }
 117:                  catch (MessageQueueException mqe)
 118:                  {
 119:                      m = null;
 120:                  }
 121:                  finally
 122:                  {
 123:                      mq.Dispose();
 124:                  }
 125:              }
 126:   
 127:              return m;
 128:          }
 129:   
 130:          ////////////////////////////////////////////////////////////////////////////
 131:          ////////////////////////////////////////////////////////////////////////////
 132:   
 133:          /// <summary>
 134:          ///
 135:          /// </summary>
 136:          public static void SendOutgoing(string queueName, string label, string text)
 137:          {
 138:              string path;
 139:   
 140:              path = @"FormatName:DIRECT=OS:ia\private$\test";
 141:   
 142:              path = @"FormatName:Direct=http://ia/msmq/private$/test";
 143:   
 144:              using (MessageQueue mq = new MessageQueue(path))
 145:              {
 146:                  try
 147:                  {
 148:                      //if (MessageQueue.Exists(path)) mq.Path = path;
 149:                      //else MessageQueue.Create(path);
 150:   
 151:                      mq.Send(text, label);
 152:                  }
 153:                  catch (MessageQueueException mqe)
 154:                  {
 155:                  }
 156:                  finally
 157:                  {
 158:                      mq.Dispose();
 159:                  }
 160:              }
 161:          }
 162:   
 163:          ////////////////////////////////////////////////////////////////////////////
 164:          ////////////////////////////////////////////////////////////////////////////
 165:   
 166:          /// <summary>
 167:          ///
 168:          /// </summary>
 169:          public static void SendPrivate(string queueName, string label, string text)
 170:          {
 171:              string path;
 172:   
 173:              path = @".\private$\" + queueName;
 174:   
 175:              using (MessageQueue mq = new MessageQueue())
 176:              {
 177:                  try
 178:                  {
 179:                      if (MessageQueue.Exists(path)) mq.Path = path;
 180:                      else MessageQueue.Create(path);
 181:   
 182:                      mq.Send(text, label);
 183:                  }
 184:                  catch (MessageQueueException mqe)
 185:                  {
 186:                  }
 187:                  finally
 188:                  {
 189:                      mq.Dispose();
 190:                  }
 191:              }
 192:          }
 193:   
 194:          ////////////////////////////////////////////////////////////////////////////
 195:   
 196:          /// <summary>
 197:          ///
 198:          /// </summary>
 199:          public static void RecievePrivate(string queueName, out string label, out string body)
 200:          {
 201:              System.Messaging.Message m;
 202:   
 203:              m = RecievePrivateMessage(queueName);
 204:   
 205:              if (m != null)
 206:              {
 207:                  label = m.Label;
 208:                  body = (string)m.Body;
 209:              }
 210:              else
 211:              {
 212:                  label = body = string.Empty;
 213:              }
 214:          }
 215:   
 216:          ////////////////////////////////////////////////////////////////////////////
 217:   
 218:          /// <summary>
 219:          ///
 220:          /// </summary>
 221:          public static System.Messaging.Message RecievePrivateMessage(string queueName)
 222:          {
 223:              // Receive will hang if there were not elements in quere, therefore I have to check if count is > 0 first
 224:              string path;
 225:              System.Messaging.Message m;
 226:   
 227:              path = @".\private$\" + queueName;
 228:   
 229:              using (MessageQueue mq = new MessageQueue())
 230:              {
 231:                  try
 232:                  {
 233:                      if (MessageQueue.Exists(path))
 234:                      {
 235:                          if (Count(queueName) > 0)
 236:                          {
 237:                              mq.Path = path;
 238:   
 239:                              mq.Formatter = new XmlMessageFormatter(new Type[] { typeof(string) });
 240:   
 241:                              m = mq.Receive();
 242:                          }
 243:                          else m = null;
 244:                      }
 245:                      else
 246:                      {
 247:                          m = null;
 248:                      }
 249:                  }
 250:                  catch (MessageQueueException mqe)
 251:                  {
 252:                      m = null;
 253:                  }
 254:                  finally
 255:                  {
 256:                      mq.Dispose();
 257:                  }
 258:              }
 259:   
 260:              return m;
 261:          }
 262:   
 263:          ////////////////////////////////////////////////////////////////////////////
 264:   
 265:          /// <summary>
 266:          ///
 267:          /// </summary>
 268:          public static System.Messaging.Message[] ReceivePrivateMessageList(string queueName)
 269:          {
 270:              return ReceiveOrPeekPrivateMessageList(queueName, true);
 271:          }
 272:   
 273:          ////////////////////////////////////////////////////////////////////////////
 274:   
 275:          /// <summary>
 276:          ///
 277:          /// </summary>
 278:          public static List<string> RecievePrivateList(string queueName)
 279:          {
 280:              System.Messaging.Message[] messageList;
 281:              List<string> list;
 282:   
 283:              messageList = ReceiveOrPeekPrivateMessageList(queueName, true);
 284:              list = new List<string>();
 285:   
 286:              if (messageList != null && messageList.Length > 0)
 287:              {
 288:                  foreach (var m in messageList)
 289:                  {
 290:                      list.Add(m.Body.ToString());
 291:                  }
 292:              }
 293:              else
 294:              {
 295:   
 296:              }
 297:   
 298:              return list;
 299:          }
 300:   
 301:          ////////////////////////////////////////////////////////////////////////////
 302:   
 303:          /// <summary>
 304:          ///
 305:          /// </summary>
 306:          private static System.Messaging.Message[] ReceiveOrPeekPrivateMessageList(string queueName, bool purgeList)
 307:          {
 308:              string path;
 309:              System.Messaging.Message[] list;
 310:   
 311:              path = @".\private$\" + queueName;
 312:   
 313:              using (MessageQueue mq = new MessageQueue())
 314:              {
 315:                  try
 316:                  {
 317:                      if (MessageQueue.Exists(path))
 318:                      {
 319:                          mq.Path = path;
 320:   
 321:                          mq.Formatter = new XmlMessageFormatter(new Type[] { typeof(string) });
 322:   
 323:                          list = mq.GetAllMessages();
 324:   
 325:                          if (purgeList) mq.Purge();
 326:                      }
 327:                      else
 328:                      {
 329:                          list = null;
 330:                      }
 331:                  }
 332:                  catch (MessageQueueException mqe)
 333:                  {
 334:                      list = null;
 335:                  }
 336:                  finally
 337:                  {
 338:                      mq.Dispose();
 339:                  }
 340:              }
 341:   
 342:              return list;
 343:          }
 344:   
 345:          ////////////////////////////////////////////////////////////////////////////
 346:   
 347:          /// <summary>
 348:          ///
 349:          /// </summary>
 350:          public static void Purge(string queueName)
 351:          {
 352:              string path;
 353:   
 354:              path = @".\private$\" + queueName;
 355:   
 356:              using (MessageQueue mq = new MessageQueue())
 357:              {
 358:                  try
 359:                  {
 360:                      if (MessageQueue.Exists(path))
 361:                      {
 362:                          mq.Path = path;
 363:   
 364:                          mq.Formatter = new XmlMessageFormatter(new Type[] { typeof(string) });
 365:   
 366:                          mq.Purge();
 367:                      }
 368:                      else
 369:                      {
 370:                      }
 371:                  }
 372:                  catch (MessageQueueException mqe)
 373:                  {
 374:                  }
 375:                  finally
 376:                  {
 377:                      mq.Dispose();
 378:                  }
 379:              }
 380:          }
 381:   
 382:          ////////////////////////////////////////////////////////////////////////////
 383:   
 384:          /// <summary>
 385:          ///
 386:          /// </summary>
 387:          public static void PeekPrivate(string queueName, out string label, out string body)
 388:          {
 389:              System.Messaging.Message m;
 390:   
 391:              m = PeekPrivateMessage(queueName);
 392:   
 393:              if (m != null)
 394:              {
 395:                  label = m.Label;
 396:                  body = (string)m.Body;
 397:              }
 398:              else
 399:              {
 400:                  label = body = string.Empty;
 401:              }
 402:          }
 403:   
 404:          ////////////////////////////////////////////////////////////////////////////
 405:   
 406:          /// <summary>
 407:          ///
 408:          /// </summary>
 409:          public static System.Messaging.Message PeekPrivateMessage(string queueName)
 410:          {
 411:              // Peek will hang if there were not elements in quere, therefore I have to check if count is > 0 first
 412:              string path;
 413:              System.Messaging.Message m;
 414:   
 415:              path = @".\private$\" + queueName;
 416:   
 417:              using (MessageQueue mq = new MessageQueue())
 418:              {
 419:                  try
 420:                  {
 421:                      if (MessageQueue.Exists(path))
 422:                      {
 423:                          if (Count(queueName) > 0)
 424:                          {
 425:                              mq.Path = path;
 426:   
 427:                              mq.Formatter = new XmlMessageFormatter(new Type[] { typeof(string) });
 428:   
 429:                              m = mq.Peek();
 430:                          }
 431:                          else m = null;
 432:                      }
 433:                      else
 434:                      {
 435:                          m = null;
 436:                      }
 437:                  }
 438:                  catch (MessageQueueException mqe)
 439:                  {
 440:                      m = null;
 441:                  }
 442:                  finally
 443:                  {
 444:                      mq.Dispose();
 445:                  }
 446:              }
 447:   
 448:              return m;
 449:          }
 450:   
 451:          ////////////////////////////////////////////////////////////////////////////
 452:   
 453:          /// <summary>
 454:          ///
 455:          /// </summary>
 456:          public static System.Messaging.Message[] PeekPrivateMessageList(string queueName)
 457:          {
 458:              return ReceiveOrPeekPrivateMessageList(queueName, false);
 459:          }
 460:   
 461:          ////////////////////////////////////////////////////////////////////////////
 462:   
 463:          /// <summary>
 464:          ///
 465:          /// </summary>
 466:          public static List<string> PeekPrivateList(string queueName)
 467:          {
 468:              System.Messaging.Message[] messageList;
 469:              List<string> list;
 470:   
 471:              messageList = ReceiveOrPeekPrivateMessageList(queueName, false);
 472:              list = new List<string>();
 473:   
 474:              if (messageList != null && messageList.Length > 0)
 475:              {
 476:                  foreach (var m in messageList)
 477:                  {
 478:                      list.Add(m.Body.ToString());
 479:                  }
 480:              }
 481:              else
 482:              {
 483:   
 484:              }
 485:   
 486:              return list;
 487:          }
 488:   
 489:          ////////////////////////////////////////////////////////////////////////////
 490:   
 491:          /// <summary>
 492:          ///
 493:          /// </summary>
 494:          public static long Count(string queueName)
 495:          {
 496:              long count;
 497:              string path;
 498:              //System.Messaging.Message[] list;
 499:   
 500:              path = @".\private$\" + queueName;
 501:   
 502:              using (MessageQueue mq = new MessageQueue())
 503:              {
 504:                  try
 505:                  {
 506:                      count = 0;
 507:   
 508:                      if (MessageQueue.Exists(path))
 509:                      {
 510:                          mq.Path = path;
 511:   
 512:                          var me = mq.GetMessageEnumerator2();
 513:   
 514:                          while (me.MoveNext()) count++;
 515:                      }
 516:                      else
 517:                      {
 518:                      }
 519:                  }
 520:                  catch (MessageQueueException mqe)
 521:                  {
 522:                      count = 0;
 523:                  }
 524:                  finally
 525:                  {
 526:                      mq.Dispose();
 527:                  }
 528:              }
 529:   
 530:              return count;
 531:          }
 532:   
 533:          ////////////////////////////////////////////////////////////////////////////
 534:   
 535:          /// <summary>
 536:          /// References queues by label.
 537:          /// </summary>
 538:          [Obsolete("Code does not work property and/or is not tested. ")]
 539:          public static void SendByLabel(string queueName)
 540:          {
 541:              using (MessageQueue mq = new MessageQueue("Label:TheLabel"))
 542:              {
 543:                  mq.Send("Queue by label.");
 544:              }
 545:   
 546:              return;
 547:          }
 548:   
 549:          ////////////////////////////////////////////////////////////////////////////
 550:   
 551:          /// <summary>
 552:          /// References queues by format name.
 553:          /// </summary>
 554:          [Obsolete("Code does not work property and/or is not tested. ")]
 555:          public static void SendByFormatName(string queueName)
 556:          {
 557:              using (MessageQueue mq = new MessageQueue("FormatName:Public=5A5F7535-AE9A-41d4-935C-845C2AFF7112"))
 558:              {
 559:                  mq.Send("Queue by format name.");
 560:              }
 561:   
 562:              return;
 563:          }
 564:   
 565:          ////////////////////////////////////////////////////////////////////////////
 566:   
 567:          /// <summary>
 568:          /// References computer journal queues.
 569:          /// </summary>
 570:          [Obsolete("Code does not work property and/or is not tested. ")]
 571:          public static void MonitorComputerJournal()
 572:          {
 573:              using (MessageQueue mq = new MessageQueue(".\\Journal$"))
 574:              {
 575:                  while (true)
 576:                  {
 577:                      Message journalMessage = mq.Receive();
 578:                      // Process the journal message.
 579:                  }
 580:              }
 581:          }
 582:   
 583:          ////////////////////////////////////////////////////////////////////////////
 584:   
 585:          /// <summary>
 586:          /// References queue journal queues.
 587:          /// </summary>
 588:          [Obsolete("Code does not work property and/or is not tested. ")]
 589:          public static void MonitorQueueJournal()
 590:          {
 591:              using (MessageQueue mq = new MessageQueue(".\\myQueue\\Journal$"))
 592:              {
 593:                  while (true)
 594:                  {
 595:                      Message journalMessage = mq.Receive();
 596:                      // Process the journal message.
 597:                  }
 598:              }
 599:          }
 600:   
 601:          ////////////////////////////////////////////////////////////////////////////
 602:   
 603:          /// <summary>
 604:          /// References dead-letter queues.
 605:          /// </summary>
 606:          [Obsolete("Code does not work property and/or is not tested. ")]
 607:          public static void MonitorDeadLetter()
 608:          {
 609:              using (MessageQueue mq = new MessageQueue(".\\DeadLetter$"))
 610:              {
 611:                  while (true)
 612:                  {
 613:                      Message deadMessage = mq.Receive();
 614:                      // Process the dead-letter message.
 615:                  }
 616:              }
 617:          }
 618:   
 619:          ////////////////////////////////////////////////////////////////////////////
 620:   
 621:          /// <summary>
 622:          /// References transactional dead-letter queues.
 623:          /// </summary>
 624:          [Obsolete("Code does not work property and/or is not tested. ")]
 625:          public static void MonitorTransactionalDeadLetter()
 626:          {
 627:              using (MessageQueue mq = new MessageQueue(".\\XactDeadLetter$"))
 628:              {
 629:                  while (true)
 630:                  {
 631:                      Message txDeadLetter = mq.Receive();
 632:                      // Process the transactional dead-letter message.
 633:                  }
 634:              }
 635:          }
 636:   
 637:          ////////////////////////////////////////////////////////////////////////////
 638:          ////////////////////////////////////////////////////////////////////////////
 639:      }
 640:   
 641:      ////////////////////////////////////////////////////////////////////////////
 642:      ////////////////////////////////////////////////////////////////////////////
 643:  }
 644: