2009-06-26 19 views

答えて

0

設定した間隔で優先順位付きメッセージを配信するSubscriptionクラスを作成しました。優先順位付けされたメッセージを消費するのは理想的な一般的な方法ではありませんが、私はそれを後世向けに投稿します。カスタムRequestReplyChannelは、他の特定のケースではより良いオプションになると思います。 PriorityQueueの実装は、読者の練習として残されています。

class PrioritySubscriber<T> : BaseSubscription<T> 
{ 
    private readonly PriorityQueue<T> queue; 
    private readonly IScheduler scheduler; 
    private readonly Action<T> receive; 
    private readonly int interval; 

    private readonly object sync = new object(); 
    private ITimerControl next = null; 

    public PrioritySubscriber(IComparer<T> comparer, IScheduler scheduler, 
     Action<T> receive, int interval) 
    { 
     this.queue = new PriorityQueue<T>(comparer); 
     this.scheduler = scheduler; 
     this.receive = receive; 
     this.interval = interval; 
    } 

    protected override void OnMessageOnProducerThread(T msg) 
    { 
     lock (this.sync) 
     { 
      this.queue.Enqueue(msg); 

      if (this.next == null) 
      { 
       this.next = 
        this.scheduler.Schedule(this.Receive, this.interval); 
      } 
     } 
    } 

    private void Receive() 
    { 
     T msg; 

     lock (this.sync) 
     { 
      msg = this.queue.Dequeue(); 

      if (this.queue.Count > 0) 
      { 
       this.next = 
        this.scheduler.Schedule(this.Receive, this.interval); 
      } 
     } 

     this.receive(msg); 
    } 
} 
+0

の代わりには、.NET(OrderedBag )についてWintellectのPowerコレクションを使用して検討することもでき、あなた自身を書いて、あなたはCodePlexの経由でこれらを見つけることができます。 – Rick

関連する問題