Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/282.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
一条消息的C#RabbitMQ EventBasicConsumer?_C#_Rabbitmq - Fatal编程技术网

一条消息的C#RabbitMQ EventBasicConsumer?

一条消息的C#RabbitMQ EventBasicConsumer?,c#,rabbitmq,C#,Rabbitmq,我想创建一个方法: bool TryPullMessage(string queue, out T message, TimeSpan? timeout = null); 它将尝试从队列中提取一条消息(我一次为一条消息配置了Qos),并在超时后失败。对于实施,我有两个选择: 在循环中使用手动请求:model.BasicGet(队列,…) 使用消费者:新EvenBasicConsumer(型号) 然而,我不想使用第一种方法,因为它会产生无限的流量和资源消耗。对于第二种方法,我尝试了很多类似的方法

我想创建一个方法:

bool TryPullMessage(string queue, out T message, TimeSpan? timeout = null);
它将尝试从队列中提取一条消息(我一次为一条消息配置了Qos),并在超时后失败。对于实施,我有两个选择:

  • 在循环中使用手动请求:model.BasicGet(队列,…)
  • 使用消费者:新EvenBasicConsumer(型号)
  • 然而,我不想使用第一种方法,因为它会产生无限的流量和资源消耗。对于第二种方法,我尝试了很多类似的方法:

                var parent = new EventWaitHandle(false, EventResetMode.AutoReset);
                var child = new EventWaitHandle(false, EventResetMode.AutoReset);
    
                var consumer = new EventingBasicConsumer(_model);
                consumer.Received += (sender, args) =>
                {
                    //save message localy
                        parent.Set();
                        child.WaitOne();
                };
                _model.BasicConsume(queue, true, consumer);
                try
                {
                    parent.WaitOne(timeout.Value);
                }
                finally
                {
                    _model.BasicCancel(consumer.ConsumerTag);
                    child.Set();
                }
    
    但如果没有成功,它只会继续消耗它们。是否可以对一条消息使用RabbitMQ推送