3

I was planning to use RabbitMQ for a project in which I require to know position of my task after every few second in the queue.

Say I am using it for generating reports and if there is a Queue then I would want to show the user his position in the queue before the task actually starts.

Is this something I can achieve through RabbitMQ??

Tash Pemhiwa
  • 7,590
  • 4
  • 45
  • 49
HackerNews
  • 153
  • 1
  • 10

2 Answers2

3

It is not possible to get message position in queue in RabbitMQ. In fact, it is something not related to AMQP protocol at all.

As a workaround to achieve what you want, you may add message id to some storage on publish, say MySQL database and then remove it from there when consumed. Then you can query that data to get metrics you want.

pinepain
  • 12,453
  • 3
  • 60
  • 65
1

You can send a message in json encoded string with unique identifier, access list of unprocessed queue data trough RabbitMQ api, decode the message and do your magic things, such as counting particular task position.

Following is example retrieving queue list using PHP curl

        // 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);
Sofyan Hadi A.
  • 45
  • 1
  • 1
  • 9