| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298 |
- <?php
- /**
- * rabbitMq 消费者
- */
- namespace console\controllers;
- use common\enums\RabbitMqEnum;
- use Yii;
- use yii\helpers\Console;
- use yii\console\Controller;
- use ReflectionMethod;
- use Exception;
- use PhpAmqpLib\Message\AMQPMessage;
- use services\common\RabbitMqService;
- use services\common\UserWalletService;
- class RabbitMqController extends Controller
- {
- /**
- * 普通任务队列消费
- * @throws \Exception
- */
- public function actionTaskConsume()
- {
- try {
- Yii::$app->services->rabbitMq->listen(RabbitMqEnum::EXCHANGE_TASK, RabbitMqEnum::TASK_QUEUE, function ($message) {
- $res = $this->handleMessage($message);
- Yii::getLogger()->flush(true);
- return $res;
- });
- } catch (Exception $e) {
- Yii::getLogger()->flush(true);
- echo 'exception:' . $e->getMessage();
- }
- }
- /**
- * 重试队列消费
- * @throws Exception
- */
- public function actionRetryConsume()
- {
- try {
- Yii::$app->services->rabbitMq->listen(RabbitMqEnum::EXCHANGE_DELAY_RETRY, RabbitMqEnum::DELAY_RETRY_QUEUE, function ($message) {
- $res = $this->handleMessage($message);
- Yii::getLogger()->flush(true);
- return $res;
- });
- } catch (Exception $e) {
- Yii::getLogger()->flush(true);
- echo 'exception:' . $e->getMessage();
- }
- }
- /**
- * 订单任务队列消费
- * @throws \Exception
- */
- public function actionOrderConsume()
- {
- try {
- Yii::$app->services->rabbitMq->listen(RabbitMqEnum::EXCHANGE_ORDER, RabbitMqEnum::ORDER_QUEUE, function ($message) {
- $res = $this->handleMessage($message);
- Yii::getLogger()->flush(true);
- return $res;
- });
- } catch (Exception $e) {
- Yii::getLogger()->flush(true);
- echo 'exception:' . $e->getMessage();
- }
- }
- /**
- * 延时队列消费
- * @throws Exception
- */
- public function actionDelayConsume()
- {
- try {
- Yii::$app->services->rabbitMq->listen(RabbitMqEnum::EXCHANGE_DELAY, RabbitMqEnum::DELAY_QUEUE, function ($message) {
- $res = $this->handleMessage($message);
- Yii::getLogger()->flush(true);
- return $res;
- });
- } catch (Exception $e) {
- Yii::getLogger()->flush(true);
- echo 'exception:' . $e->getMessage();
- }
- }
- /**
- * 消息处理
- * @param AMQPMessage $message
- * @throws
- * @return bool
- */
- private function handleMessage($message)
- {
- $routing_key = $message->getRoutingKey();
- echo '队列(' . $routing_key . ')的消息,其内容为:' . $message->getBody() . PHP_EOL . PHP_EOL;
- $message_body = json_decode($message->getBody(), true);
- $class = $message_body['handler_class'];
- $method = $message_body['method'];
- $data = $message_body['data'];
- if (!method_exists($class, $method)) {
- //方法不存在,忽略
- Yii::error('actionTaskConsume: class' . $class . ' ,method:' . $method . ' ,not exist');
- return false;
- }
- $reflect_method = new ReflectionMethod($class, $method);
- Yii::$app->db->close();
- Yii::$app->redis->close();
- if ($reflect_method->isStatic()) {
- $res = $class::$method($data);
- if ($res === false) Yii::error($class::$static_error ?? '未定义错误');
- //if($res === false) throw new Exception($class::$static_error ?? '未定义错误');
- } else {
- $obj = new $class();
- $res = $obj->$method($data);
- if ($res === false) Yii::error($obj->error ?? '未定义错误');
- //if($res === false) throw new Exception($obj->error ?? '未定义错误');
- }
- return $res;
- }
- /**
- * 修改关系链队列消费
- * @throws \Exception
- */
- public function actionChangeRelationshipConsume()
- {
- try {
- Yii::$app->services->rabbitMq->listen(RabbitMqEnum::EXCHANGE_TASK_CHANGE_RELATIONSHIP, RabbitMqEnum::CHANGE_RELATIONSHIP_QUEUE, function ($message) {
- $res = $this->handleMessage($message);
- Yii::getLogger()->flush(true);
- return $res;
- });
- } catch (Exception $e) {
- Yii::getLogger()->flush(true);
- echo 'exception:' . $e->getMessage();
- }
- }
- /**
- * 编辑商品队列消费
- * @throws \Exception
- */
- public function actionEditGoods()
- {
- try {
- Yii::$app->services->rabbitMq->listen(RabbitMqEnum::EXCHANGE_TASK_GOODS, RabbitMqEnum::GOODS_QUEUE, function ($message) {
- $res = $this->handleMessage($message);
- Yii::getLogger()->flush(true);
- return $res;
- });
- } catch (Exception $e) {
- Yii::getLogger()->flush(true);
- echo 'exception:' . $e->getMessage();
- }
- }
- /**
- * 编辑用户资金队列消费
- * @throws \Exception
- */
- public function actionUserWalletConsume()
- {
- try {
- Yii::$app->services->rabbitMq->listen(RabbitMqEnum::EXCHANGE_TASK_USER_WALLET, RabbitMqEnum::USER_WALLET_QUEUE, function ($message) {
- $message_id = $message->get('message_id');
- $data['message_id'] = $message->get('message_id');
- $routing_key = $message->getRoutingKey();
- echo $message->getConsumerTag() . '接收到发给队列(' . $routing_key . ')的消息,其内容为:' . $message->getBody() . PHP_EOL;
- $message_body = json_decode($message->getBody(), true);
- $class = $message_body['handler_class'];
- $method = $message_body['method'];
- $data = $message_body['data'];
- $res = $class::$method($data);
- //$res = UserWalletService::handle($message_id,$data);
- Yii::getLogger()->flush(true);
- return $res;
- });
- } catch (Exception $e) {
- Yii::getLogger()->flush(true);
- echo 'actionUserWalletConsume exception:' . $e->getMessage();
- }
- }
- /**
- * 拼团任务队列消费
- * @throws \Exception
- */
- public function actionGroupBuy()
- {
- try {
- Yii::$app->services->rabbitMq->listen(RabbitMqEnum::EXCHANGE_TASK_GROUP_BUY, RabbitMqEnum::GROUP_BUY_QUEUE, function ($message) {
- $res = $this->handleMessage($message);
- Yii::getLogger()->flush(true);
- return $res;
- });
- } catch (Exception $e) {
- Yii::getLogger()->flush(true);
- echo 'exception:' . $e->getMessage();
- }
- }
- /**
- * 幸运拼团任务队列消费
- * @throws \Exception
- */
- public function actionLuckyGroup()
- {
- try {
- Yii::$app->services->rabbitMq->listen(RabbitMqEnum::EXCHANGE_TASK_LUCKY_GROUP, RabbitMqEnum::LUCKY_GROUP_QUEUE, function ($message) {
- $res = $this->handleMessage($message);
- Yii::getLogger()->flush(true);
- return $res;
- });
- } catch (Exception $e) {
- Yii::getLogger()->flush(true);
- echo 'exception:' . $e->getMessage();
- }
- }
- /**
- * 幸运拼团任务队列消费
- * @throws \Exception
- */
- public function actionCensus()
- {
- try {
- Yii::$app->services->rabbitMq->listen(RabbitMqEnum::EXCHANGE_TASK_CENSUS, RabbitMqEnum::CENSUS_QUEUE, function ($message) {
- $res = $this->handleMessage($message);
- Yii::getLogger()->flush(true);
- return $res;
- });
- } catch (Exception $e) {
- Yii::getLogger()->flush(true);
- echo 'exception:' . $e->getMessage();
- }
- }
- public function actionImportUser()
- {
- try {
- Yii::$app->services->rabbitMq->listen(RabbitMqEnum::EXCHANGE_TASK_IMPORT_USER, RabbitMqEnum::IMPORT_USER, function ($message) {
- $res = $this->handleMessage($message);
- Yii::getLogger()->flush(true);
- return $res;
- });
- } catch (Exception $e) {
- Yii::getLogger()->flush(true);
- echo 'exception:' . $e->getMessage();
- }
- }
- /**
- * 长耗时队列消费
- * @throws \Exception
- */
- public function actionLongConsume()
- {
- try {
- Yii::$app->services->rabbitMq->listen(RabbitMqEnum::EXCHANGE_LONG_DURATION, RabbitMqEnum::LONG_DURATION_QUEUE, function ($message) {
- $res = $this->handleMessage($message);
- Yii::getLogger()->flush(true);
- return $res;
- });
- } catch (Exception $e) {
- Yii::getLogger()->flush(true);
- echo 'exception:' . $e->getMessage();
- }
- }
- }
|