Mq.php 5.2 KB

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