• 12
name

A PHP Error was encountered

Severity: Notice

Message: Undefined index: userid

Filename: views/question.php

Line Number: 191

Backtrace:

File: /home/prodcxja/public_html/questions/application/views/question.php
Line: 191
Function: _error_handler

File: /home/prodcxja/public_html/questions/application/controllers/Questions.php
Line: 433
Function: view

File: /home/prodcxja/public_html/questions/index.php
Line: 315
Function: require_once

I'm required to create a simple queue manager to pass a number from a sender to a consumer. Hello World tutorial provided by RabbitMQ covers almost 70% of it.

But I need to change the queue to not to forever waiting for incoming messages. Or stop waiting after certain amount of messages. I read and tried few solutions from other post, but it doesn't work.

rabbitmq AMQP::consume() - undefined method. there's another method, wait_frame but it is protected.

and other post is in python which I dont understand.

<?php

require_once __DIR__ . '/vendor/autoload.php';
require 'config.php';
use PhpAmqpLib\Connection\AMQPStreamConnection;

function recieveQueue($queueName){
    $connection = new AMQPStreamConnection('localhost', 5672, 'guest', 'guest');

    // try{
    //  $connection->wait_frame(10);
    // }catch(AMQPConnectionException $e){
    //  echo "asdasd";
    // }

    $channel = $connection->channel();

    $channel->queue_declare($queueName, false, false, false, false);

    echo ' [*] Waiting for messages. To exit press CTRL+C', "\n";

    $callback = function($msg) {
        echo " [x] Received ", $msg->body, "\n";

    };

    // $tag = uniqid() . microtime(true);
    // $queue->consume($callback, $flags, $tag);

    $channel->basic_consume($queueName, '', false, true, false, false, $callback);

    // $channel->cancel($tag);

    while(count($channel->callbacks)) {
        $channel->wait();
    }

    echo "\nfinish";
}

recieveQueue('vtiger');

?>

Modify wait() in while loop:

$timeout = 55;
while(count($channel->callbacks)) {
    $channel->wait(null, false, $timeout);
}
  • 10
Reply Report

wait function works only with sockets, we have to catch the exception:

 $timeout = 5;
    while (count($channel->callbacks)) {
        try{
            $channel->wait(null, false , $timeout);
        }catch(\PhpAmqpLib\Exception\AMQPTimeoutException $e){
            $channel->close();
            $connection->close();
            exit;
        }
    }
  • 6
Reply Report

This is how I did to give signal to the queue to stop consuming incoming messages.

However this may not be a proper way of doing it since it gives an error instead of comes out properly.

Please suggest a better answer if there's one.

    $callback = function($msg) {
        echo " [x] Received ", $msg->body, "\n";

        // if queue recieve 'stop', stop consume anymore messages
        if ($msg->body == 'stop'){
            $channel->basic_cancel($queueName);
        }
    };

    $channel->basic_consume($queueName, '', false, true, false, false, $callback);

    $timeout = 10;
    while(count($channel->callbacks)) {
        // $channel->wait(null, false, $timeout);
        $channel->wait();
    }
  • 0
Reply Report

Warm tip !!!

This article is reproduced from Stack Exchange / Stack Overflow, please click

Trending Tags

Related Questions