2014-11-01 70 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); 
檢索隊列列表