質問

どのように優先順位付けの方法で処理される入力チャンネルができますか?同等のものはあります Scalaの「reactWithin(0) { ... case TIMEOUT }」構築へ?

役に立ちましたか?

解決

私は、設定された間隔に優先順位メッセージを配信するサブスクリプションクラスを書きました。これは、優先順位のメッセージを消費するのに理想的な、一般的な場合の方法ではないのですが、私は後世のためにそれを投稿します。私は、カスタムRequestReplyChannelは、特定の他の例のためのより良い選択肢だと思います。優先度つきキューの実装は、読者への課題として残しておきます。

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);
    }
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top