我在pecl 1.0.3中使用amqp扩展,该扩展是用2.7.1 rabbitmq.编译的。
我正试图让一个基本的消费者/生产者的例子发挥作用,但我总是出错。关于这个扩展的php文档很少,而且很多文档似乎已经过时或错误。
我使用了一位用户发布的代码,但似乎无法让消费者部分工作
连接:
function amqp_connection() {
$amqpConnection = new AMQPConnection();
$amqpConnection->setLogin("guest");
$amqpConnection->setPassword("guest");
$amqpConnection->connect();
if(!$amqpConnection->isConnected()) {
die("Cannot connect to the broker, exiting !n");
}
return $amqpConnection;
}
发件人:
function amqp_send($text, $routingKey, $exchangeName){
$amqpConnection = amqp_connection();
$channel = new AMQPChannel($amqpConnection);
$exchange = new AMQPExchange($channel);
$exchange->setName($exchangeName);
$exchange->setType("fanout");
if($message = $exchange->publish($text, $routingKey)){
echo "sent";
}
if (!$amqpConnection->disconnect()) {
throw new Exception("Could not disconnect !");
}
}
接收器:
function amqp_receive($exchangeName, $routingKey, $queueName) {
$amqpConnection = amqp_connection();
$channel = new AMQPChannel($amqpConnection);
$queue = new AMQPQueue($channel);
$queue->setName($queueName);
$queue->bind($exchangeName, $routingKey);
//Grab the info
//...
}
然后发送:
amqp_send("Abcdefg", "action", "amq.fanout");
接收:
amqp_receive("amq.fanout","action","action");
我一直在运行脚本时遇到问题,并指向amqp receive:
PHP致命错误:未捕获异常"AMQPQueueException",消息为"服务器通道错误:404,消息:NOT_FOUND-在/home/jamescowhen/test.PHP中的vhost"/"中没有队列"操作":21
有人能给我指正确的方向吗?整个示例来自此处的用户注释:http://www.php.net/manual/en/amqp.examples.php#109024
异常似乎是由未声明队列引起的(正如错误消息所描述的404-未找到队列"action")。该示例适用于原始海报的原因可能是他之前已经声明了队列,但没有意识到在他的示例中缺少队列。
您可以通过对队列对象调用->declare()来声明队列。您还必须对exchange对象执行此操作,除非您在尝试将队列挂接到它时确定它已经存在。