2014-11-01 71 views
0

我打算将RabbitMQ用于一个项目,在该项目中,我需要在队列中每隔几秒就知道我的任务的位置。知道在RabbitMQ任务的位置

说我正在使用它来生成报告,如果有一个队列,那么我想在任务实际开始之前向用户显示他在队列中的位置。

这是我能通过RabbitMQ实现的吗?

回答

1

在RabbitMQ中无法获取队列中的消息位置。实际上,它完全与AMQP协议无关。

作为一种解决方法,您可以在发布时将消息ID添加到某些存储上,比如说MySQL数据库,然后在消耗时将其从消息中删除。然后,您可以查询该数据以获取所需的指标。

0

你可以用json编码的字符串发送一条消息,带有唯一标识符,访问未处理的队列数据列表通过RabbitMQ api,对消息进行解码并完成你的魔术事物,比如计算特定的任务位置。

以下为示例使用PHP卷曲

 // create php curl 
     $ch = curl_init(); 

     $queue_titile = "hallo"; 

     $url = "http://localhost::15672/#/api/queues/%2F/$queue_titile/get";          

     // define post data 
     $post_data = '{"vhost":"/","name":"'.$queue_titile.'","truncate":"'.$limit.'","requeue":"true","encoding":"auto","count":"'.$limit.'"}'; 

     // set curl configuration 
     curl_setopt($ch, CURLOPT_URL, $url); 
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
     curl_setopt($ch, CURLOPT_USERPWD, 'guest:guest'); 
     curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); 
     curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data); 

     // send curl request            
     $response = curl_exec($ch); 

     // close curl connection 
     curl_close($ch);          

     // decode json response 
     $response = json_decode($response); 
检索队列列表