12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576 |
- <?php
- /**
- * @link http://www.yiiframework.com/
- * @copyright Copyright (c) 2008 Yii Software LLC
- * @license http://www.yiiframework.com/license/
- */
- namespace console\controllers;
- use yii\console\Controller;
- use components\PhpClient;
- use PhpAmqpLib\Message\AMQPMessage;
- use PhpAmqpLib\Connection\AMQPStreamConnection;
- /**
- * This command echoes the first argument that you have entered.
- *
- * This command is provided as an example for you to learn how to create console commands.
- *
- * @author Qiang Xue <qiang.xue@gmail.com>
- * @since 2.0
- */
- class RabbittaskController extends Controller
- {
- public function actionWorker()
- {
- // require(\Yii::getAlias('@vendor') . '/vendor/autoload.php');
- $connection = new AMQPStreamConnection('172.30.118.225', 5673, 'guest', 'guest');
- $channel = $connection->channel();
- $channel->queue_declare('task_queue', false, true, false, false);
- //交互格式形式json php yii rabbittask/new-task "{"\"text\":\"广州\""}"
- // $city='{"text":"广州"}';
- // var_dump($city);
- // $city=json_decode($city,true);
- // var_dump($city);
- $callback = function ($msg) {
- //do sth
- //$msg->body
- //连接数据库
- $city=$msg->body;
- $city=json_decode($city,true);
- var_dump($city);
- $msg->delivery_info['channel']->basic_ack($msg->delivery_info['delivery_tag']);
- };
- $channel->basic_qos(null, 1, null);
- $channel->basic_consume('task_queue', '', false, false, false, false, $callback);
- while (count($channel->callbacks)) {
- $channel->wait();
- }
- $channel->close();
- $connection->close();
- }
- public function actionNewTask($n)
- {
- PhpClient::CallMq($n);
- }
- }
|