Message.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. <?php
  2. namespace common\logic\MQMessage;
  3. use PhpAmqpLib\Message\AMQPMessage;
  4. use PhpAmqpLib\Connection\AMQPStreamConnection;
  5. /**
  6. * 处理消息体逻辑
  7. * Class Message
  8. * @package common\logic\MQMessage
  9. */
  10. class Message extends Config
  11. {
  12. private $connection;
  13. private $channel;
  14. private $callback_queue;
  15. private $response = null;
  16. private $corr_id;
  17. private $result = '';
  18. public function __construct()
  19. {
  20. $this->connection = new AMQPStreamConnection(
  21. self::HOST,
  22. self::PORT,
  23. self::USER,
  24. self::PASS
  25. );
  26. $this->channel = $this->connection->channel();
  27. list($this->callback_queue, ,) = $this->channel->queue_declare(
  28. '', false, false, true, false
  29. );
  30. $this->channel->basic_consume(
  31. $this->callback_queue, '', false, false, false, false,
  32. array($this, 'on_response')
  33. );
  34. }
  35. /**
  36. * [发送信息]
  37. * @author: libingke
  38. * @param string $body 消息主体
  39. * @param string $routing_key
  40. * @return array 返回数据
  41. */
  42. public function send($body, $routing_key = 'task_queue')
  43. {
  44. $properties = [
  45. 'correlation_id' => uniqid(),
  46. 'reply_to' => $this->callback_queue
  47. ];
  48. if (is_string($body)) {
  49. $msg = new AMQPMessage( (string) $body,$properties);
  50. $this->result = $this->channel->basic_publish($msg, '', $routing_key);
  51. }
  52. $this->channel->close();
  53. $this->connection->close();
  54. return $this->result;
  55. }
  56. /**
  57. * [批量发送信息]
  58. * @author: libingke
  59. * @param string $body 消息主体
  60. * @param string $routing_key
  61. * @return array 返回数据
  62. */
  63. public function batchSend($body, $routing_key = '')
  64. {
  65. $properties = [
  66. 'correlation_id' => uniqid(),
  67. 'reply_to' => $this->callback_queue
  68. ];
  69. if (is_array($body)) {
  70. $msg = new AMQPMessage((string) $body, $properties);
  71. $this->result = $this->channel->batch_basic_publish($msg, '', $routing_key);
  72. }
  73. $this->channel->close();
  74. $this->connection->close();
  75. return $this->result;
  76. }
  77. /**
  78. * [消费信息]
  79. * @author: libingke
  80. * @param string $queue 队列名称
  81. * @param string $str
  82. * @return array 返回数据
  83. */
  84. public function receive($queue, $str)
  85. {
  86. $callback = function($msg) {
  87. echo " [x] Received ", $msg->body, "\n";
  88. };
  89. $this->result = $this->channel->basic_consume($queue, '', false, true, false, false, $callback);
  90. while(count($this->channel->callbacks)) {
  91. $this->channel->wait();
  92. }
  93. return $this->result;
  94. }
  95. /**
  96. * [批量消费信息]
  97. * @author: libingke
  98. * @param string $queue 队列名称
  99. * @param string $arr
  100. * @return array 返回数据
  101. */
  102. public function batchReceive($queue, $arr)
  103. {
  104. $callback = function($msg) {
  105. echo " [x] Received ", $msg->body, "\n";
  106. };
  107. $this->result = $this->channel->basic_consume($queue, '', false, true, false, false, $callback);
  108. while(count($this->channel->callbacks)) {
  109. $this->channel->wait();
  110. }
  111. return $this->result;
  112. }
  113. /**
  114. * [删除信息]
  115. * @author: libingke
  116. */
  117. public function delete()
  118. {
  119. }
  120. public function on_response($rep)
  121. {
  122. if($rep->get('correlation_id') == $this->corr_id) {
  123. $this->result .= $rep->body;
  124. $this->response = $rep->body;
  125. }
  126. }
  127. public static function CallMq($n)
  128. {
  129. $connection = new AMQPStreamConnection(
  130. self::HOST, self::PORT, self::USER, self::PASS
  131. );
  132. $channel = $connection->channel();
  133. $channel->queue_declare('task_queue', false, true, false, false);
  134. $data=empty($n)?"Hello World!":$n;
  135. $msg = new AMQPMessage($data,
  136. array('delivery_mode' => AMQPMessage::DELIVERY_MODE_PERSISTENT)
  137. );
  138. $channel->basic_publish($msg, '', 'task_queue');
  139. $channel->close();
  140. $connection->close();
  141. return true;
  142. }
  143. public static function CallUserMq($n)
  144. {
  145. $connection = new AMQPStreamConnection(
  146. self::HOST, self::PORT, self::USER, self::PASS
  147. );
  148. $channel = $connection->channel();
  149. $channel->queue_declare('login', false, true, false, false);
  150. $data=empty($n)?"Hello World!":$n;
  151. $msg = new AMQPMessage($data,
  152. array('delivery_mode' => AMQPMessage::DELIVERY_MODE_PERSISTENT)
  153. );
  154. $channel->basic_publish($msg, '', 'login');
  155. $channel->close();
  156. $connection->close();
  157. return true;
  158. }
  159. }