0
0

RabbittaskController.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 common\models\RabbitLog;
  9. use yii\console\Controller;
  10. use components\PhpClient;
  11. use PhpAmqpLib\Message\AMQPMessage;
  12. use PhpAmqpLib\Connection\AMQPStreamConnection;
  13. use yii\helpers\ArrayHelper;
  14. /**
  15. * This command echoes the first argument that you have entered.
  16. *
  17. * This command is provided as an example for you to learn how to create console commands.
  18. *
  19. * @author Qiang Xue <qiang.xue@gmail.com>
  20. * @since 2.0
  21. */
  22. class RabbittaskController extends Controller
  23. {
  24. public function actionWorker()
  25. {
  26. $connection = new AMQPStreamConnection('172.30.118.225', 5673, 'guest', 'guest');
  27. $channel = $connection->channel();
  28. $channel->queue_declare('task_queue', false, true, false, false);
  29. //交互格式形式json php yii rabbittask/new-task "{"\"text\":\"广州\""}"
  30. // $city='{"text":"广州"}';
  31. // var_dump($city);
  32. // $city=json_decode($city,true);
  33. // var_dump($city);
  34. $callback = function ($msg) {
  35. //do sth
  36. //$msg->body
  37. //连接数据库
  38. //begin 持久化
  39. $model = new RabbitLog();
  40. $model->msg = json_encode($msg,TRUE);
  41. $model->msg_body =json_encode($msg->body,TRUE);
  42. $model->channel = json_encode($msg->delivery_info['channel'],TRUE);
  43. $model->queue = "task_queue";
  44. $model->msg_delivery_tag =json_encode($msg->delivery_info['delivery_tag'],TRUE);
  45. $model->addtime = date('y-m-d h:i:s',time());
  46. $model->describe = "this is rabbit consume message";
  47. $model->save(false);
  48. echo 'ok consume one '. "\n" ;
  49. $msg->delivery_info['channel']->basic_ack($msg->delivery_info['delivery_tag']);
  50. };
  51. $channel->basic_qos(null, 1, null);
  52. $channel->basic_consume('task_queue', '', false, false, false, false, $callback);
  53. while (count($channel->callbacks)) {
  54. $channel->wait();
  55. }
  56. $channel->close();
  57. $connection->close();
  58. }
  59. public function actionNewTask($n)
  60. {
  61. PhpClient::CallMq($n);
  62. }
  63. /*
  64. * demo db
  65. *
  66. * */
  67. public function actionInsert()
  68. {
  69. $model = new RabbitLog();
  70. $model->msg = 1;
  71. $model->msg_body = 1;
  72. $model->channel = 1;
  73. $model->queue = 1;
  74. $model->msg_delivery_tag = 1;
  75. $model->addtime =date('y-m-d h:i:s',time());
  76. $model->describe = "this is rabbit consume message";
  77. $model->save(false);
  78. echo 'ok consume one '. "\n" ;
  79. }
  80. public function actionSelect()
  81. {
  82. $model = new RabbitLog();
  83. $model->msg = 1;
  84. $model->msg_body = 1;
  85. $model->channel = 1;
  86. $model->queue = 1;
  87. $model->msg_delivery_tag = 1;
  88. $model->addtime =date('y-m-d h:i:s',time());
  89. $model->describe = "this is rabbit consume message";
  90. $model->save(false);
  91. echo 'ok consume one '. "\n" ;
  92. }
  93. }