0
0

Queue.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. namespace common\logic;
  3. /**
  4. * Created by PhpStorm.
  5. * User: ZhangFeng
  6. * Date: 2018/1/30
  7. * Time: 11:39
  8. * Description:
  9. */
  10. use components\CUrl;
  11. use yii\db\ActiveRecord;
  12. class Queue extends ActiveRecord
  13. {
  14. public $queue;
  15. public $passive;
  16. public $durable;
  17. public $exclusive;
  18. public $auto_delete;
  19. public $if_unused;
  20. public $if_empty;
  21. public $nowait;
  22. public $ticket;
  23. public $user;
  24. public $pass;
  25. public function rules()
  26. {
  27. return [
  28. ['queue','required','on' => ['delete','create'],'message' => '队列名称为空'],
  29. [['passive','exclusive','auto_delete'],'default','value' => false,'on' => 'create'],
  30. ['durable','default','value' => true,'on' => 'create'],
  31. [['if_unused','if_empty','nowait'],'default','on' => 'delete'],
  32. ['ticket','default','value' => null,'on' => 'delete'],
  33. [['user','pass'],'required','on' => 'list','message' => '用户名和密码不能为空']
  34. ];
  35. }
  36. /**
  37. * @return mixed
  38. */
  39. public function createQueue()
  40. {
  41. $connection = Connection::getInstance();
  42. $channel = $connection->channel();
  43. $queue = $channel->queue_declare($this->queue, $this->passive, $this->durable, $this->exclusive, $this->auto_delete);
  44. return isset($queue[0]) ? : null;
  45. }
  46. /**
  47. * @return mixed
  48. */
  49. public function deleteQueue()
  50. {
  51. $connection = Connection::getInstance();
  52. $channel = $connection->channel();
  53. $res = $channel->queue_delete($this->queue,$this->if_unused,$this->if_empty,$this->nowait,$this->ticket);
  54. return $res;
  55. }
  56. /**
  57. * @return array
  58. */
  59. public function listQueue()
  60. {
  61. $url = 'http://192.168.1.183:15672/api/queues';
  62. $curl = new CUrl();
  63. $curl->authorize = $this->user . ':' . $this->pass;
  64. $res = $curl->get($url);
  65. $res = json_decode($res,true);
  66. return array_column($res,'name');
  67. }
  68. }