Php amqp 效率低,怎么延迟? – php-amqplib

我想知道如何推迟使用Amqpphplib.

我用过这个很棒的咖啡脚本教程:

但它似乎不适用于PHP-amqplib.

消息按我的意愿过期,但似乎“x-dead-letter-exchange”不能完成这项工作.我使用了RabbitMQ管理控制台,我看到了所有队列的创建和删除.但是我的消息在到期后会立即进入队列.我使用RabbitMQ 3.2.3版本,PHP-amqplib 2.2.*版本.

这是我的代码:

连接类:

class Connection

{

/**

* @var $ch

*/

public $ch;

/**

* @var $consumer_tag

*/

public $consumer_tag;

/**

* @var $exchange

*/

public $exchange;

/**

* @var $conn

*/

public $conn;

public function __construct($host, $port, $user, $password, $vhost)

{

$this->exchange = 'immediate';

$this->queue = 'right.now.queue';

$this->consumer_tag = 'consumer';

$this->conn = new AMQPConnection($host, $port, $user, $password, $vhost);

$this->ch = $this->conn->channel();

$this->ch->exchange_declare($this->exchange, 'direct', false, true, false);

$this->ch->queue_declare($this->queue, false, true, false, false, false);

$this->ch->queue_bind($this->queue, $this->exchange);

}

public function createDelayedQueue ($name, $delay_seconds) {

$this->ch->queue_declare($name, false, false, false, true, true, array(

"x-dead-letter-exchange" => array("S", $this->exchange),

"x-message-ttl" => array("I", $delay_seconds*1000),

"x-expires" => array("I", $delay_seconds*1000+1000)

));

}

}

发布代码

$name = 'send.later.'.$ts;

$amqp->createDelayedQueue($name, 2);

$msg = new AMQPMessage($msg_body, array('content_type' => 'text/plain', 'delivery_mode' => 2));

$amqp->ch->basic_publish($msg);

消费者代码

$amqp = $this->getContainer()->get('amqp_connexion');

$amqp->ch->basic_consume($amqp->queue, $amqp->consumer_tag, false, false, false, false, function ($msg) {

echo $msg->body;

echo "\n--------\n";

});

$output->writeln('Listening '.$amqp->queue.'...');

// Loop as long as the channel has callbacks registered

while (count($amqp->ch->callbacks)) {

$amqp->ch->wait();

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值