TestMq.php 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. use PhpAmqpLib\Connection\AMQPStreamConnection;
  5. use PhpAmqpLib\Message\AMQPMessage;
  6. class TestMq extends Command
  7. {
  8. /**
  9. * The name and signature of the console command.
  10. *
  11. * @var string
  12. */
  13. protected $signature = 'test:mq ';
  14. /**
  15. * The console command description.
  16. *
  17. * @var string
  18. */
  19. protected $description = 'Command description';
  20. /**
  21. * Create a new command instance.
  22. *
  23. * @return void
  24. */
  25. public function __construct()
  26. {
  27. parent::__construct();
  28. }
  29. /**
  30. * Execute the console command.
  31. *
  32. * @return int
  33. */
  34. public function handle()
  35. {
  36. $connection = new AMQPStreamConnection('localhost', 5672, 'guest', 'guest');
  37. $channel = $connection->channel();
  38. $channel->queue_declare('hello', false, false, false, false);
  39. $msg = new AMQPMessage('Hello World!');
  40. $channel->basic_publish($msg, '', 'hello');
  41. echo " [x] Sent 'Hello World!'\n";
  42. $channel->close();
  43. $connection->close();
  44. return 0;
  45. }
  46. }