0
0

RabbittaskController.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. /**
  3. * @link http://www.yiiframework.com/
  4. * @copyright Copyright (c) 2008 Yii Software LLC
  5. * @license http://www.yiiframework.com/license/
  6. */
  7. namespace console\controllers;
  8. use yii\console\Controller;
  9. use components\PhpClient;
  10. use PhpAmqpLib\Message\AMQPMessage;
  11. use PhpAmqpLib\Connection\AMQPStreamConnection;
  12. /**
  13. * This command echoes the first argument that you have entered.
  14. *
  15. * This command is provided as an example for you to learn how to create console commands.
  16. *
  17. * @author Qiang Xue <qiang.xue@gmail.com>
  18. * @since 2.0
  19. */
  20. class RabbittaskController extends Controller
  21. {
  22. public function actionWorker()
  23. {
  24. // require(\Yii::getAlias('@vendor') . '/vendor/autoload.php');
  25. $connection = new AMQPStreamConnection('172.30.118.225', 5673, 'guest', 'guest');
  26. $channel = $connection->channel();
  27. $channel->queue_declare('task_queue', false, true, false, false);
  28. //交互格式形式json php yii rabbittask/new-task "{"\"text\":\"广州\""}"
  29. // $city='{"text":"广州"}';
  30. // var_dump($city);
  31. // $city=json_decode($city,true);
  32. // var_dump($city);
  33. $callback = function ($msg) {
  34. //do sth
  35. //$msg->body
  36. //连接数据库
  37. $city=$msg->body;
  38. $city=json_decode($city,true);
  39. var_dump($city);
  40. $msg->delivery_info['channel']->basic_ack($msg->delivery_info['delivery_tag']);
  41. };
  42. $channel->basic_qos(null, 1, null);
  43. $channel->basic_consume('task_queue', '', false, false, false, false, $callback);
  44. while (count($channel->callbacks)) {
  45. $channel->wait();
  46. }
  47. $channel->close();
  48. $connection->close();
  49. }
  50. public function actionNewTask($n)
  51. {
  52. PhpClient::CallMq($n);
  53. }
  54. }