我想使用与问题here和代码here类似的持续运行的BufferBlock实现使用者/生产者模式。
我尝试使用类似OP的ActionBlock,但是如果缓冲区块已满并且新消息在其队列中,那么新消息永远不会被添加到ConcurrentDictionary _queue。
在下面的代码中,当使用此调用将新消息添加到缓冲区块时,永远不会调用ConsumeAsync方法:_messageBufferBlock.SendAsync(message)
如何更正下面的代码,以便每次使用_messageBufferBlock.SendAsync(message)
添加新邮件时调用ConsumeAsync方法?
public class PriorityMessageQueue
{
private volatile ConcurrentDictionary<int,MyMessage> _queue = new ConcurrentDictionary<int,MyMessage>();
private volatile BufferBlock<MyMessage> _messageBufferBlock;
private readonly Task<bool> _initializingTask; // not used but allows for calling async method from constructor
private int _dictionaryKey;
public PriorityMessageQueue()
{
_initializingTask = Init();
}
public async Task<bool> EnqueueAsync(MyMessage message)
{
return await _messageBufferBlock.SendAsync(message);
}
private async Task<bool> ConsumeAsync()
{
try
{
// This code does not fire when a new message is added to the buffereblock
while (await _messageBufferBlock.OutputAvailableAsync())
{
// A message object is never received from the bufferblock
var message = await _messageBufferBlock.ReceiveAsync();
}
return true;
}
catch (Exception ex)
{
return false;
}
}
private async Task<bool> Init()
{
var executionDataflowBlockOptions = new ExecutionDataflowBlockOptions
{
MaxDegreeOfParallelism = Environment.ProcessorCount,
BoundedCapacity = 50
};
var prioritizeMessageBlock = new ActionBlock<MyMessage>(msg =>
{
SetMessagePriority(msg);
}, executionDataflowBlockOptions);
_messageBufferBlock = new BufferBlock<MyMessage>();
_messageBufferBlock.LinkTo(prioritizeMessageBlock, new DataflowLinkOptions { PropagateCompletion = true, MaxMessages = 50});
return await ConsumeAsync();
}
}
修改 我删除了所有额外的代码并添加了评论。
答案 0 :(得分:2)
我仍然不完全确定你想要完成什么,但我会试着指出你正确的方向。该示例中的大多数代码都不是必需的。
我需要知道新消息何时到达
如果这是您唯一的要求,那么我假设您只需要在传入新消息时运行一些任意代码。在数据流中执行此操作的最简单方法是使用TransformBlock
和将该块设置为管道中的初始接收器。每个块都有它自己的缓冲区,所以除非你需要另一个缓冲区,否则你可以把它留下来。
public class PriorityMessageQueue {
private TransformBlock<MyMessage, MyMessage> _messageReciever;
public PriorityMessageQueue() {
var executionDataflowBlockOptions = new ExecutionDataflowBlockOptions {
MaxDegreeOfParallelism = Environment.ProcessorCount,
BoundedCapacity = 50
};
var prioritizeMessageBlock = new ActionBlock<MyMessage>(msg => {
SetMessagePriority(msg);
}, executionDataflowBlockOptions);
_messageReciever = new TransformBlock<MyMessage, MyMessage>(msg => NewMessageRecieved(msg), executionDataflowBlockOptions);
_messageReciever.LinkTo(prioritizeMessageBlock, new DataflowLinkOptions { PropagateCompletion = true });
}
public async Task<bool> EnqueueAsync(MyMessage message) {
return await _messageReciever.SendAsync(message);
}
private MyMessage NewMessageRecieved(MyMessage message) {
//do something when a new message arrives
//pass the message along in the pipeline
return message;
}
private void SetMessagePriority(MyMessage message) {
//Handle a message
}
}
当然,您有另一个选择是在EnqueAsync
返回任务之前立即执行SendAsync
所需的任何操作,但TransformBlock
会为您提供额外的灵活性。< / p>