Php amqp 效率低,执行PHP AMQP的延迟队列

最近我做了一个生产者/消费者队列系统的快速实施.

namespace Queue;

use PhpAmqpLib\Connection\AMQPStreamConnection;

use PhpAmqpLib\Message\AMQPMessage;

use PhpAmqpLib\Wire\AMQPTable;

class Amqp

{

private $connection;

private $queueName;

private $delayedQueueName;

private $channel;

private $callback;

public function __construct($host, $port, $login, $password, $queueName)

{

$this->connection = new AMQPStreamConnection($host, $port, $login, $password);

$this->queueName = $queueName;

$this->delayedQueueName = null;

$this->channel = $this->connection->channel();

// First, we need to make sure that RabbitMQ will never lose our queue.

// In order to do so, we need to declare it as durable. To do so we pass

// the third parameter to queue_declare as true.

$this->channel->queue_declare($queueName, false, true, false, false);

}

public function __destruct()

{

$this->close();

}

// Just in case : https://stackoverflow.com/questions/151660/can-i-trust-php-destruct-method-to-be-called

// We should call close explicitly if possible.

public function close()

{

if (!is_null($this->channel)) {

$this->channel->close();

$this->channel = null;

}

if (!is_null($this->connection)) {

$this->connection->close();

$this->connection = null;

}

}

public function produceWithDelay($data, $delay)

{

if (is_null($this->delayedQueueName))

{

$delayedQueueName = $this->queueName . '.delayed';

// First, we need to make sure that RabbitMQ will never lose our queue.

// In order to do so, we need to declare it as durable. To do so we pass

// the third parameter to queue_declare as true.

$this->channel->queue_declare($this->delayedQueueName, false, true, false, false, false,

new AMQPTable(array(

'x-dead-letter-exchange' => '',

'x-dead-letter-routing-key' => $this->queueName

))

);

$this->delayedQueueName = $delayedQueueName;

}

$msg = new AMQPMessage(

$data,

array(

'delivery_mode' => AMQPMessage::DELIVERY_MODE_PERSISTENT,

'expiration' => $delay

)

);

$this->channel->basic_publish($msg, '', $this->delayedQueueName);

}

public function produce($data)

{

$msg = new AMQPMessage(

$data,

array('delivery_mode' => AMQPMessage::DELIVERY_MODE_PERSISTENT)

);

$this->channel->basic_publish($msg, '', $this->queueName);

}

public function consume($callback)

{

$this->callback = $callback;

// This tells RabbitMQ not to give more than one message to a worker at

// a time.

$this->channel->basic_qos(null, 1, null);

// Requires ack.

$this->channel->basic_consume($this->queueName, '', false, false, false, false, array($this, 'consumeCallback'));

while(count($this->channel->callbacks)) {

$this->channel->wait();

}

}

public function consumeCallback($msg)

{

call_user_func_array(

$this->callback,

array($msg)

);

// Very important to ack, in order to remove msg from queue. Ack after

// callback, as exception might happen in callback.

$msg->delivery_info['channel']->basic_ack($msg->delivery_info['delivery_tag']);

}

public function getQueueSize()

{

// three tuple containing (, , )

$tuple = $this->channel->queue_declare($this->queueName, false, true, false, false);

if ($tuple != null && isset($tuple[1])) {

return $tuple[1];

}

return -1;

}

}

公共功能产生和公共功能消费对按预期工作.

但是,它带有延迟队列系统

public function generateWithDelay和public function consume pair不能正常工作.消费者呼叫消费,不能接收任何项目,甚至等待一段时间.

我认为我的产品在实施中是不正确的.我可以知道这是什么问题吗?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值