如何以优先方式处理频道输入?有没有相应的东西
Scala的“reactWithin(0) { ... case TIMEOUT }
”构造?
答案 0 :(得分: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);
}
}