I
I
Ivan Palamarchuk2017-02-14 15:42:05
PHP
Ivan Palamarchuk, 2017-02-14 15:42:05

How to implement multiple RabbitMQ consumers?

Let's say I have a worker (consumer):

$connection = new AMQPConnection($config['rabbitmq']);
$connection->connect();
$channel = new AMQPChannel($connection);

$exchange = new AMQPExchange($channel);
$exchange->setName('exchange_name');
$exchange->setType(AMQP_EX_TYPE_DIRECT);
$exchange->setFlags(AMQP_AUTODELETE);
$exchange->declareExchange();

$queue = new AMQPQueue($channel);
$queue->setName('queue_name');
$queue->setFlags(AMQP_DURABLE);
$queue->declareQueue();
$queue->bind($exchange->getName(), $queue->getName());

while (true) {
    $envelope = $queue->get();

    if ($envelope) {
        var_dump($envelope->getBody());
        $queue->nack($envelope->getDeliveryTag());
    }
}

$connection->disconnect();

Which way is better to support 15 consumers? Those. if I run this worker, it will eat the process, I would not like to run 15 php-cli processes in parallel..

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Fedorov, 2017-02-14
@qonand

to manage processes, you can use for example supervisord

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question