MsmqWorkerAvailabilityManager.cs :  » Workflows » NServiceBus » MsmqWorkerAvailabilityManager » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Workflows » NServiceBus 
NServiceBus » MsmqWorkerAvailabilityManager » MsmqWorkerAvailabilityManager.cs
using System;
using NServiceBus.Unicast.Distributor;
using System.Messaging;
using NServiceBus.Unicast.Transport.Msmq;

namespace MsmqWorkerAvailabilityManager{
  /// <summary>
  /// An implementation of <see cref="IWorkerAvailabilityManager"/> for MSMQ to be used
  /// with the <see cref="Distributor"/> class.
  /// </summary>
    public class MsmqWorkerAvailabilityManager : IWorkerAvailabilityManager
    {
        #region config info

        private MessageQueue storageQueue;

    /// <summary>
    /// Sets the path to the queue that will be used for storing
    /// worker availability.
    /// </summary>
    /// <remarks>The queue provided must be transactional.</remarks>
        public virtual string StorageQueue
        {
            set 
            {
                string path = MsmqTransport.GetFullPath(value);
                MessageQueue q = new MessageQueue(path);

                if (!q.Transactional)
                    throw new Exception("Queue must be transactional.");

                this.storageQueue = q;
            }
        }

        #endregion

        #region IWorkerAvailabilityManager Members

    /// <summary>
    /// Removes all entries from the worker availability queue
    /// with the specified address.
    /// </summary>
    /// <param name="address">
    /// The address of the worker to remove from the availability list.
    /// </param>
        public void ClearAvailabilityForWorker(string address)
        {
            lock (locker)
            {
                Message[] existing = this.storageQueue.GetAllMessages();

                foreach (Message m in existing)
                    if (MsmqTransport.GetIndependentAddressForQueue(m.ResponseQueue) == address)
                        this.storageQueue.ReceiveById(m.Id, MessageQueueTransactionType.Automatic);
            }
        }

    /// <summary>
    /// Pops the next available worker from the available worker queue
    /// and returns its address.
    /// </summary>
    /// <returns>The address of the next available worker.</returns>
        public string PopAvailableWorker()
        {
            lock (locker)
            {
                Message[] existing = this.storageQueue.GetAllMessages();

                if (existing.Length == 0)
                    return null;
                else
                {
                    this.storageQueue.ReceiveById(existing[0].Id, MessageQueueTransactionType.Automatic);

                    return MsmqTransport.GetIndependentAddressForQueue(existing[0].ResponseQueue);
                }
            }
        }

    /// <summary>
    /// Signal that a worker is available to receive a dispatched message.
    /// </summary>
    /// <param name="address">
    /// The address of the worker that will accept the dispatched message.
    /// </param>
        public void WorkerAvailable(string address)
        {
            lock (locker)
            {
                Message msg = new Message();
                msg.ResponseQueue = new MessageQueue(MsmqTransport.GetFullPath(address));

                this.storageQueue.Send(msg, MessageQueueTransactionType.Automatic);
            }
        }

        #endregion

        #region members

        private object locker = new object();

        #endregion
    }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.