PHP delay for simple task

46 views
Skip to first unread message

Sebastien Hotte

unread,
Dec 20, 2019, 4:09:31 PM12/20/19
to rabbitmq-users
Hi,

I need to create a delay from each message sent to my queue. From my code, everything is done in a second. Don't know what is wrong.

This is the code:
My producer:
$connection = new AMQPStreamConnection('localhost', 5672, 'guest', 'guest');
       $channel = $connection->channel();

        $channel->exchange_declare('send', 'x-delayed-message', false, false, true, false, false,new AMQPTable([
           'x-delayed-type' => 'x-delayed-message',
       ]));
       $data = [1,2,3,4,5,6,7];
       for ($i=0; $i < count($data); $i++) {
            $msg = new AMQPMessage($data[$i],array(
               'delivery_mode' => 2,
               'application_headers' => new AMQPTable([
                   'x-delay' => 10000,
               ])
           ));
           $channel->basic_publish($msg, '', 'hello');

            echo " [x] Sent 'Hello World!'\n";
       }
       $channel->close();
       $connection->close();
My consumer:
$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->body, "\n";
       };
       
       $channel->basic_consume('hello', '', false, true, false, false, $callback);
       
       while ($channel->is_consuming()) {
           $channel->wait();
       }

        $channel->close();
       $connection->close();

Thank you.

Luke Bakken

unread,
Dec 23, 2019, 3:40:01 PM12/23/19
to rabbitmq-users
Hello,

Please provide the following information:
  • RabbitMQ version
  • Erlang version
  • Operating system and version
  • PHP version
  • php-amqplib version
  • The output of rabbitmq-plugins list
Reply all
Reply to author
Forward
0 new messages