RabbitMqController.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. <?php
  2. /**
  3. * rabbitMq 消费者
  4. */
  5. namespace console\controllers;
  6. use common\enums\RabbitMqEnum;
  7. use Yii;
  8. use yii\helpers\Console;
  9. use yii\console\Controller;
  10. use ReflectionMethod;
  11. use Exception;
  12. use PhpAmqpLib\Message\AMQPMessage;
  13. use services\common\RabbitMqService;
  14. use services\common\UserWalletService;
  15. class RabbitMqController extends Controller
  16. {
  17. /**
  18. * 普通任务队列消费
  19. * @throws \Exception
  20. */
  21. public function actionTaskConsume()
  22. {
  23. try {
  24. Yii::$app->services->rabbitMq->listen(RabbitMqEnum::EXCHANGE_TASK, RabbitMqEnum::TASK_QUEUE, function ($message) {
  25. $res = $this->handleMessage($message);
  26. Yii::getLogger()->flush(true);
  27. return $res;
  28. });
  29. } catch (Exception $e) {
  30. Yii::getLogger()->flush(true);
  31. echo 'exception:' . $e->getMessage();
  32. }
  33. }
  34. /**
  35. * 重试队列消费
  36. * @throws Exception
  37. */
  38. public function actionRetryConsume()
  39. {
  40. try {
  41. Yii::$app->services->rabbitMq->listen(RabbitMqEnum::EXCHANGE_DELAY_RETRY, RabbitMqEnum::DELAY_RETRY_QUEUE, function ($message) {
  42. $res = $this->handleMessage($message);
  43. Yii::getLogger()->flush(true);
  44. return $res;
  45. });
  46. } catch (Exception $e) {
  47. Yii::getLogger()->flush(true);
  48. echo 'exception:' . $e->getMessage();
  49. }
  50. }
  51. /**
  52. * 订单任务队列消费
  53. * @throws \Exception
  54. */
  55. public function actionOrderConsume()
  56. {
  57. try {
  58. Yii::$app->services->rabbitMq->listen(RabbitMqEnum::EXCHANGE_ORDER, RabbitMqEnum::ORDER_QUEUE, function ($message) {
  59. $res = $this->handleMessage($message);
  60. Yii::getLogger()->flush(true);
  61. return $res;
  62. });
  63. } catch (Exception $e) {
  64. Yii::getLogger()->flush(true);
  65. echo 'exception:' . $e->getMessage();
  66. }
  67. }
  68. /**
  69. * 延时队列消费
  70. * @throws Exception
  71. */
  72. public function actionDelayConsume()
  73. {
  74. try {
  75. Yii::$app->services->rabbitMq->listen(RabbitMqEnum::EXCHANGE_DELAY, RabbitMqEnum::DELAY_QUEUE, function ($message) {
  76. $res = $this->handleMessage($message);
  77. Yii::getLogger()->flush(true);
  78. return $res;
  79. });
  80. } catch (Exception $e) {
  81. Yii::getLogger()->flush(true);
  82. echo 'exception:' . $e->getMessage();
  83. }
  84. }
  85. /**
  86. * 消息处理
  87. * @param AMQPMessage $message
  88. * @throws
  89. * @return bool
  90. */
  91. private function handleMessage($message)
  92. {
  93. $routing_key = $message->getRoutingKey();
  94. echo '队列(' . $routing_key . ')的消息,其内容为:' . $message->getBody() . PHP_EOL . PHP_EOL;
  95. $message_body = json_decode($message->getBody(), true);
  96. $class = $message_body['handler_class'];
  97. $method = $message_body['method'];
  98. $data = $message_body['data'];
  99. if (!method_exists($class, $method)) {
  100. //方法不存在,忽略
  101. Yii::error('actionTaskConsume: class' . $class . ' ,method:' . $method . ' ,not exist');
  102. return false;
  103. }
  104. $reflect_method = new ReflectionMethod($class, $method);
  105. Yii::$app->db->close();
  106. Yii::$app->redis->close();
  107. if ($reflect_method->isStatic()) {
  108. $res = $class::$method($data);
  109. if ($res === false) Yii::error($class::$static_error ?? '未定义错误');
  110. //if($res === false) throw new Exception($class::$static_error ?? '未定义错误');
  111. } else {
  112. $obj = new $class();
  113. $res = $obj->$method($data);
  114. if ($res === false) Yii::error($obj->error ?? '未定义错误');
  115. //if($res === false) throw new Exception($obj->error ?? '未定义错误');
  116. }
  117. return $res;
  118. }
  119. /**
  120. * 修改关系链队列消费
  121. * @throws \Exception
  122. */
  123. public function actionChangeRelationshipConsume()
  124. {
  125. try {
  126. Yii::$app->services->rabbitMq->listen(RabbitMqEnum::EXCHANGE_TASK_CHANGE_RELATIONSHIP, RabbitMqEnum::CHANGE_RELATIONSHIP_QUEUE, function ($message) {
  127. $res = $this->handleMessage($message);
  128. Yii::getLogger()->flush(true);
  129. return $res;
  130. });
  131. } catch (Exception $e) {
  132. Yii::getLogger()->flush(true);
  133. echo 'exception:' . $e->getMessage();
  134. }
  135. }
  136. /**
  137. * 编辑商品队列消费
  138. * @throws \Exception
  139. */
  140. public function actionEditGoods()
  141. {
  142. try {
  143. Yii::$app->services->rabbitMq->listen(RabbitMqEnum::EXCHANGE_TASK_GOODS, RabbitMqEnum::GOODS_QUEUE, function ($message) {
  144. $res = $this->handleMessage($message);
  145. Yii::getLogger()->flush(true);
  146. return $res;
  147. });
  148. } catch (Exception $e) {
  149. Yii::getLogger()->flush(true);
  150. echo 'exception:' . $e->getMessage();
  151. }
  152. }
  153. /**
  154. * 编辑用户资金队列消费
  155. * @throws \Exception
  156. */
  157. public function actionUserWalletConsume()
  158. {
  159. try {
  160. Yii::$app->services->rabbitMq->listen(RabbitMqEnum::EXCHANGE_TASK_USER_WALLET, RabbitMqEnum::USER_WALLET_QUEUE, function ($message) {
  161. $message_id = $message->get('message_id');
  162. $data['message_id'] = $message->get('message_id');
  163. $routing_key = $message->getRoutingKey();
  164. echo $message->getConsumerTag() . '接收到发给队列(' . $routing_key . ')的消息,其内容为:' . $message->getBody() . PHP_EOL;
  165. $message_body = json_decode($message->getBody(), true);
  166. $class = $message_body['handler_class'];
  167. $method = $message_body['method'];
  168. $data = $message_body['data'];
  169. $res = $class::$method($data);
  170. //$res = UserWalletService::handle($message_id,$data);
  171. Yii::getLogger()->flush(true);
  172. return $res;
  173. });
  174. } catch (Exception $e) {
  175. Yii::getLogger()->flush(true);
  176. echo 'actionUserWalletConsume exception:' . $e->getMessage();
  177. }
  178. }
  179. /**
  180. * 拼团任务队列消费
  181. * @throws \Exception
  182. */
  183. public function actionGroupBuy()
  184. {
  185. try {
  186. Yii::$app->services->rabbitMq->listen(RabbitMqEnum::EXCHANGE_TASK_GROUP_BUY, RabbitMqEnum::GROUP_BUY_QUEUE, function ($message) {
  187. $res = $this->handleMessage($message);
  188. Yii::getLogger()->flush(true);
  189. return $res;
  190. });
  191. } catch (Exception $e) {
  192. Yii::getLogger()->flush(true);
  193. echo 'exception:' . $e->getMessage();
  194. }
  195. }
  196. /**
  197. * 幸运拼团任务队列消费
  198. * @throws \Exception
  199. */
  200. public function actionLuckyGroup()
  201. {
  202. try {
  203. Yii::$app->services->rabbitMq->listen(RabbitMqEnum::EXCHANGE_TASK_LUCKY_GROUP, RabbitMqEnum::LUCKY_GROUP_QUEUE, function ($message) {
  204. $res = $this->handleMessage($message);
  205. Yii::getLogger()->flush(true);
  206. return $res;
  207. });
  208. } catch (Exception $e) {
  209. Yii::getLogger()->flush(true);
  210. echo 'exception:' . $e->getMessage();
  211. }
  212. }
  213. /**
  214. * 幸运拼团任务队列消费
  215. * @throws \Exception
  216. */
  217. public function actionCensus()
  218. {
  219. try {
  220. Yii::$app->services->rabbitMq->listen(RabbitMqEnum::EXCHANGE_TASK_CENSUS, RabbitMqEnum::CENSUS_QUEUE, function ($message) {
  221. $res = $this->handleMessage($message);
  222. Yii::getLogger()->flush(true);
  223. return $res;
  224. });
  225. } catch (Exception $e) {
  226. Yii::getLogger()->flush(true);
  227. echo 'exception:' . $e->getMessage();
  228. }
  229. }
  230. public function actionImportUser()
  231. {
  232. try {
  233. Yii::$app->services->rabbitMq->listen(RabbitMqEnum::EXCHANGE_TASK_IMPORT_USER, RabbitMqEnum::IMPORT_USER, function ($message) {
  234. $res = $this->handleMessage($message);
  235. Yii::getLogger()->flush(true);
  236. return $res;
  237. });
  238. } catch (Exception $e) {
  239. Yii::getLogger()->flush(true);
  240. echo 'exception:' . $e->getMessage();
  241. }
  242. }
  243. /**
  244. * 长耗时队列消费
  245. * @throws \Exception
  246. */
  247. public function actionLongConsume()
  248. {
  249. try {
  250. Yii::$app->services->rabbitMq->listen(RabbitMqEnum::EXCHANGE_LONG_DURATION, RabbitMqEnum::LONG_DURATION_QUEUE, function ($message) {
  251. $res = $this->handleMessage($message);
  252. Yii::getLogger()->flush(true);
  253. return $res;
  254. });
  255. } catch (Exception $e) {
  256. Yii::getLogger()->flush(true);
  257. echo 'exception:' . $e->getMessage();
  258. }
  259. }
  260. }