Quantcast
Channel: Active questions tagged consumer - Stack Overflow
Viewing all articles
Browse latest Browse all 91

How to stop or timeout a consumer from consuming after specific time in RabbitMQ using php

$
0
0

Here is a hello world example on rabbitmq docs about consumer using php and php-amqplib package:

<?phprequire_once __DIR__ . '/vendor/autoload.php';use PhpAmqpLib\Connection\AMQPStreamConnection;$connection = new AMQPStreamConnection('localhost', 5672, 'guest', 'guest');$channel = $connection->channel();$channel->queue_declare('hello', false, false, false, false);echo " [*] Waiting for messages. To exit press CTRL+C\n";$callback = function ($msg) {    echo ' [x] Received ', $msg->getBody(), "\n";};$channel->basic_consume('hello', '', false, true, false, false, $callback);try {    $channel->consume();} catch (\Throwable $exception) {    echo $exception->getMessage();}$channel->close();$connection->close();

Are there any ways to stop or timeout the consumer after a specific time?And I wonder how the process can reach the close() method of connection and channel in the code above if there are not any exception occur because the consume() method is blocking it?

I found out that there is a basic_cancel method but I don know where and when to use it because like I said, the consume() method is blocking the code.


Viewing all articles
Browse latest Browse all 91

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>