Mq.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. namespace App\Http\Api;
  3. use PhpAmqpLib\Connection\AMQPStreamConnection;
  4. use PhpAmqpLib\Message\AMQPMessage;
  5. use PhpAmqpLib\Exchange\AMQPExchangeType;
  6. class Mq{
  7. public static function publish(string $channelName, $message){
  8. //一对一
  9. $connection = new AMQPStreamConnection(env("RABBITMQ_HOST"),
  10. env("RABBITMQ_PORT"),
  11. env("RABBITMQ_USER"),
  12. env("RABBITMQ_PASSWORD"),
  13. env("RABBITMQ_VIRTUAL_HOST")
  14. );
  15. $channel = $connection->channel();
  16. $channel->queue_declare($channelName, false, true, false, false);
  17. $msg = new AMQPMessage(json_encode($message,JSON_UNESCAPED_UNICODE));
  18. $channel->basic_publish($msg, '', $channelName);
  19. $channel->close();
  20. $connection->close();
  21. }
  22. /**
  23. * @param string $exchange
  24. * @param string $queue
  25. * @param callable|null $callback
  26. */
  27. public static function worker($exchange,$queue,$callback=null){
  28. $consumerTag = 'consumer';
  29. $connection = new AMQPStreamConnection(env("RABBITMQ_HOST"),
  30. env("RABBITMQ_PORT"),
  31. env("RABBITMQ_USER"),
  32. env("RABBITMQ_PASSWORD"),
  33. env("RABBITMQ_VIRTUAL_HOST"));
  34. $channel = $connection->channel();
  35. /*
  36. The following code is the same both in the consumer and the producer.
  37. In this way we are sure we always have a queue to consume from and an
  38. exchange where to publish messages.
  39. */
  40. /*
  41. name: $queue
  42. passive: false
  43. durable: true // the queue will survive server restarts
  44. exclusive: false // the queue can be accessed in other channels
  45. auto_delete: false //the queue won't be deleted once the channel is closed.
  46. */
  47. $channel->queue_declare($queue, false, true, false, false);
  48. /*
  49. name: $exchange
  50. type: direct
  51. passive: false
  52. durable: true // the exchange will survive server restarts
  53. auto_delete: false //the exchange won't be deleted once the channel is closed.
  54. */
  55. $channel->exchange_declare($exchange, AMQPExchangeType::DIRECT, false, true, false);
  56. $channel->queue_bind($queue, $exchange);
  57. /**
  58. * @param \PhpAmqpLib\Message\AMQPMessage $message
  59. */
  60. $process_message = function ($message) use($callback,$connection,$exchange,$queue)
  61. {
  62. if($callback !== null){
  63. try{
  64. $result = $callback(json_decode($message->body));
  65. if($result !== 0){
  66. throw new \Exception('error');
  67. }
  68. }catch(\Exception $e){
  69. // push to issues
  70. $channelName = 'issues';
  71. $channelIssues = $connection->channel();
  72. $channelIssues->queue_declare($channelName, false, true, false, false);
  73. $msg = new AMQPMessage(json_encode(['exchange'=>$exchange,
  74. 'channel'=>$queue,
  75. 'message'=>json_decode($message->body),
  76. 'result'=>$result,
  77. 'error'=>$e,
  78. ],JSON_UNESCAPED_UNICODE));
  79. $channelIssues->basic_publish($msg, '', $channelName);
  80. $channelIssues->close();
  81. }
  82. }
  83. $message->ack();
  84. // Send a message with the string "quit" to cancel the consumer.
  85. /*
  86. if ($message->body === 'quit') {
  87. $message->getChannel()->basic_cancel($message->getConsumerTag());
  88. }
  89. */
  90. };
  91. /*
  92. queue: Queue from where to get the messages
  93. consumer_tag: Consumer identifier
  94. no_local: Don't receive messages published by this consumer.
  95. no_ack: If set to true, automatic acknowledgement mode will be used by this consumer. See https://www.rabbitmq.com/confirms.html for details.
  96. exclusive: Request exclusive consumer access, meaning only this consumer can access the queue
  97. nowait:
  98. callback: A PHP Callback
  99. */
  100. $channel->basic_consume($queue, $consumerTag, false, false, false, false, $process_message);
  101. /**
  102. * @param \PhpAmqpLib\Channel\AMQPChannel $channel
  103. * @param \PhpAmqpLib\Connection\AbstractConnection $connection
  104. */
  105. $shutdown = function ($channel, $connection)
  106. {
  107. $channel->close();
  108. $connection->close();
  109. };
  110. register_shutdown_function($shutdown, $channel, $connection);
  111. // Loop as long as the channel has callbacks registered
  112. while ($channel->is_consuming()) {
  113. $channel->wait(null, true);
  114. // do something else
  115. usleep(300000);
  116. }
  117. }
  118. }