test_queue.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. use PhpAmqpLib\Connection\AMQPStreamConnection;
  5. use PhpAmqpLib\Message\AMQPMessage;
  6. define('HOST', '120.55.25.252');
  7. define('PORT', 5672);
  8. define('USER', 'test');
  9. define('PASS', '6acbQWE13');
  10. define('VHOST', '/');
  11. //If this is enabled you can see AMQP output on the CLI
  12. define('AMQP_DEBUG', true);
  13. class test_queue extends Command
  14. {
  15. /**
  16. * The name and signature of the console command.
  17. *
  18. * @var string
  19. */
  20. protected $signature = 'command:tqueue';
  21. /**
  22. * The console command description.
  23. *
  24. * @var string
  25. */
  26. protected $description = 'tqueue';
  27. /**
  28. * Create a new command instance.
  29. *
  30. * @return void
  31. */
  32. public function __construct()
  33. {
  34. parent::__construct();
  35. }
  36. /**
  37. * Execute the console command.
  38. *
  39. * @return mixed
  40. */
  41. public function handle()
  42. {
  43. echo 'publish';
  44. $this->exchange = 'router';
  45. $this->queue = 'msg';
  46. $connection = new AMQPStreamConnection(HOST, PORT, USER, PASS, VHOST);
  47. $channel = $connection->channel();
  48. echo $channel;
  49. /*
  50. The following code is the same both in the consumer and the producer.
  51. In this way we are sure we always have a queue to consume from and an
  52. exchange where to publish messages.
  53. */
  54. /*
  55. name: $queue
  56. passive: false
  57. durable: true // the queue will survive server restarts
  58. exclusive: false // the queue can be accessed in other channels
  59. auto_delete: false //the queue won't be deleted once the channel is closed.
  60. */
  61. $channel->queue_declare($this->queue, false, true, false, false);
  62. /*
  63. name: $exchange
  64. type: direct
  65. passive: false
  66. durable: true // the exchange will survive server restarts
  67. auto_delete: false //the exchange won't be deleted once the channel is closed.
  68. */
  69. $channel->exchange_declare($exchange, 'direct', false, true, false);
  70. $channel->queue_bind($queue, $exchange);
  71. // $messageBody = implode(' ', array_slice($argv, 1));
  72. $messageBody = 'msg1';
  73. $options = getopt("f:hp:");
  74. var_dump('$options');
  75. var_dump($options);
  76. $message = new AMQPMessage($messageBody, array('content_type' => 'text/plain', 'delivery_mode' => AMQPMessage::DELIVERY_MODE_PERSISTENT));
  77. $channel->basic_publish($message, $exchange);
  78. $channel->close();
  79. $connection->close();
  80. }
  81. }