首页
下载
文档
社区
视频
捐赠
源代码
赞助商
AOT 编译器
AI 助理
商业产品
PHP AOT 原生编译器
Swoole-Compiler 代码加密器
CRMEB 新零售社交电商系统
登录
注册
全部
提问
分享
讨论
建议
公告
开发框架
CodeGalaxy
发表新帖
php swoole+amqp+rabbitmq整合多进程启动channel被locked问题
运行环境:swoole 1.7.21 稳定版 ,mac os x 10.10 , php 5.5, rabbitmq3.5, amqp 1.6 {{{ <?php error_reporting(E_ALL^E_NOTICE); date_default_timezone_set('Asia/Shanghai'); class send { private $redis; private $serv; private $mq_conn=null; private $mq_channel=null; private $mq_exchange=null; private $mq_queue=null; private $mq_exchange_name = "swoole_exchanger_name"; private $mq_router_name="swoole_router_name"; private $mq_queue_name="swoole_queue_name"; private $mq=array(); public function __construct() { $this->serv = new swoole_http_server( "0.0.0.0", "9501" ); $this->serv->set(array( 'worker_num' => 8, //工作进程数量 'max_conn' => 1000, 'max_request' => 100000, 'debug_mode'=> 1, 'task_worker_num'=>8 //'daemonize' => true, //是否作为守护进程 )); $this->serv->on('Start', array($this, 'onStart')); $this->serv->on('WorkerStart', array($this, 'onWorkerStart')); $this->serv->on('connect', array($this, 'onConnect')); $this->serv->on('request' , array( $this , 'onRequest')); $this->serv->on('Receive', array($this, 'onReceive')); $this->serv->on('Close', array($this, 'onClose')); $this->serv->on('Task', array($this, 'onTask')); $this->serv->on('Finish', array($this, 'onFinish')); $this->serv->start(); } public function onStart( $serv ) { echo "Start\n"; } public function onRequest($request, $response){ $this->serv->task(""); $response->end("hello swoole rabbitmq"); } public function onConnect($serv, $fd){ echo "Connected\n"; } public function onWorkerStart( $serv , $worker_id) { $serv->mq['conn'] = new AMQPConnection(); $serv->mq['conn'] ->setHost('127.0.0.1'); $serv->mq['conn'] ->setLogin('guest'); $serv->mq['conn'] ->setPassword('guest'); $serv->mq['conn'] ->connect(); $serv->mq['channel'] = new AMQPChannel($serv->mq['conn']); $serv->mq['exchange'] = new AMQPExchange($serv->mq['channel']); $serv->mq['exchange']->setType(AMQP_EX_TYPE_DIRECT); $serv->mq['exchange']->setName($this->mq_exchange_name); $serv->mq['exchange']->declareExchange(); $serv->mq['queue'] = new AMQPQueue($serv->mq['channel']); $serv->mq['queue']->setFlags(AMQP_EXCLUSIVE); $serv->mq['queue']->setName($this->mq_queue_name); $serv->mq['queue']->declareQueue(); $serv->mq['queue']->bind($this->mq_exchange_name, $this->mq_queue_name); } public function onReceive(swoole_server $serv, $fd, $from_id, $data){ $serv->task(""); } public function onClose( $serv, $fd, $from_id ) { echo "Client {$fd} close connection\n"; } public function onTask($serv,$task_id,$from_id, $data) { //准备在这里把消息发送到rabbitmq队列里 } public function onFinish($serv,$task_id, $data) { } } new send(); 在php cli模式下运行 报以下错,上述问题需要解决多进程连接rabbitmq问题,rabbitmq通道被locked #[root@winfan] php send.php Fatal error: Uncaught exception 'AMQPQueueException' with message 'Server channel error: 405, message: RESOURCE_LOCKED - cannot obtain exclusive access to locked queue 'swoole_queue_name' in vhost '/'' in /Library/WebServer/Documents/xxxxx/rabbit-manager/tests/send.php:88 Stack trace: #0 /Library/WebServer/Documents/xxxxx/rabbit-manager/tests/send.php(88): AMQPQueue->declareQueue() #1 [internal function]: send->onWorkerStart(Object(swoole_http_server), 1) #2 /Library/WebServer/Documents/xxxxx/rabbit-manager/tests/send.php(48): swoole_http_server->start() #3 /Library/WebServer/Documents/xxxxx/rabbit-manager/tests/send.php(120): send->__construct() #4 {main} }}}
发布于9年前 · 12 次浏览 · 来自
提问
代
代号9527
运行环境:swoole 1.7.21 稳定版 ,mac os x 10.10 , php 5.5, rabbitmq3.5, amqp 1.6 {{{ <?php error_reporting(E_ALL^E_NOTICE); date_default_timezone_set('Asia/Shanghai'); class send { private $redis; private $serv; private $mq_conn=null; private $mq_channel=null; private $mq_exchange=null; private $mq_queue=null; private $mq_exchange_name = "swoole_exchanger_name"; private $mq_router_name="swoole_router_name"; private $mq_queue_name="swoole_queue_name"; private $mq=array(); public function __construct() { $this->serv = new swoole_http_server( "0.0.0.0", "9501" ); $this->serv->set(array( 'worker_num' => 8, //工作进程数量 'max_conn' => 1000, 'max_request' => 100000, 'debug_mode'=> 1, 'task_worker_num'=>8 //'daemonize' => true, //是否作为守护进程 )); $this->serv->on('Start', array($this, 'onStart')); $this->serv->on('WorkerStart', array($this, 'onWorkerStart')); $this->serv->on('connect', array($this, 'onConnect')); $this->serv->on('request' , array( $this , 'onRequest')); $this->serv->on('Receive', array($this, 'onReceive')); $this->serv->on('Close', array($this, 'onClose')); $this->serv->on('Task', array($this, 'onTask')); $this->serv->on('Finish', array($this, 'onFinish')); $this->serv->start(); } public function onStart( $serv ) { echo "Start\n"; } public function onRequest($request, $response){ $this->serv->task(""); $response->end("hello swoole rabbitmq"); } public function onConnect($serv, $fd){ echo "Connected\n"; } public function onWorkerStart( $serv , $worker_id) { $serv->mq['conn'] = new AMQPConnection(); $serv->mq['conn'] ->setHost('127.0.0.1'); $serv->mq['conn'] ->setLogin('guest'); $serv->mq['conn'] ->setPassword('guest'); $serv->mq['conn'] ->connect(); $serv->mq['channel'] = new AMQPChannel($serv->mq['conn']); $serv->mq['exchange'] = new AMQPExchange($serv->mq['channel']); $serv->mq['exchange']->setType(AMQP_EX_TYPE_DIRECT); $serv->mq['exchange']->setName($this->mq_exchange_name); $serv->mq['exchange']->declareExchange(); $serv->mq['queue'] = new AMQPQueue($serv->mq['channel']); $serv->mq['queue']->setFlags(AMQP_EXCLUSIVE); $serv->mq['queue']->setName($this->mq_queue_name); $serv->mq['queue']->declareQueue(); $serv->mq['queue']->bind($this->mq_exchange_name, $this->mq_queue_name); } public function onReceive(swoole_server $serv, $fd, $from_id, $data){ $serv->task(""); } public function onClose( $serv, $fd, $from_id ) { echo "Client {$fd} close connection\n"; } public function onTask($serv,$task_id,$from_id, $data) { //准备在这里把消息发送到rabbitmq队列里 } public function onFinish($serv,$task_id, $data) { } } new send(); 在php cli模式下运行 报以下错,上述问题需要解决多进程连接rabbitmq问题,rabbitmq通道被locked #[root@winfan] php send.php Fatal error: Uncaught exception 'AMQPQueueException' with message 'Server channel error: 405, message: RESOURCE_LOCKED - cannot obtain exclusive access to locked queue 'swoole_queue_name' in vhost '/'' in /Library/WebServer/Documents/xxxxx/rabbit-manager/tests/send.php:88 Stack trace: #0 /Library/WebServer/Documents/xxxxx/rabbit-manager/tests/send.php(88): AMQPQueue->declareQueue() #1 [internal function]: send->onWorkerStart(Object(swoole_http_server), 1) #2 /Library/WebServer/Documents/xxxxx/rabbit-manager/tests/send.php(48): swoole_http_server->start() #3 /Library/WebServer/Documents/xxxxx/rabbit-manager/tests/send.php(120): send->__construct() #4 {main} }}}
赞
0
收藏
提问
分享
讨论
建议
公告
开发框架
CodeGalaxy
登录
后参与评论
评论
2017-05-13
无
无为
我设置一个进程就可可以跑,不会报错, 问题还是在多进程共用一个channel问题吗?
赞
0
回复
2017-05-18
小
小Q画室
这个应该是 `AMQ` 扩展的限制,建议使用 addProcess单独开启一个进程执行 `AMQ` 队列操作。
赞
0
回复