Workers.php 724 B

123456789101112131415161718192021222324252627282930
  1. <?php
  2. namespace App\Console\Workers;
  3. class Workers{
  4. protected $queue = 'hello';
  5. /**
  6. * Create a new command instance.
  7. *
  8. * @return void
  9. */
  10. public function __construct()
  11. {
  12. $connection = new AMQPStreamConnection(MQ_HOST, MQ_PORT, MQ_USERNAME, MQ_PASSWORD);
  13. $channel = $connection->channel();
  14. $channel->queue_declare($this->queue, false, true, false, false);
  15. echo " [*] Waiting for messages. To exit press CTRL+C\n";
  16. $channel->basic_consume($this->queue, '', false, true, false, false, $this->job);
  17. while ($channel->is_open()) {
  18. $channel->wait();
  19. }
  20. return 0;
  21. }
  22. public function job($msg){
  23. echo ' [x] Received ', $msg->body, "\n";
  24. }
  25. }