延时队列
- Delayproducer.Php
- Amqpbuilder.Php
AmqpBuilder.php
<?php
declare(strict_types = 1);
namespace App\Components\Amqp;
use Hyperf\Amqp\Builder\Builder;
use Hyperf\Amqp\Builder\QueueBuilder;
class AmqpBuilder extends QueueBuilder
{
/**
* @param array|\PhpAmqpLib\Wire\AMQPTable $arguments
*
* @return \Hyperf\Amqp\Builder\Builder
*/
public function setArguments($arguments) : Builder
{
$this->arguments = array_merge($this->arguments, $arguments);
return $this;
}
/**
* 设置延时队列相关参数
*
* @param string $queueName
* @param int $xMessageTtl
* @param string $xDeadLetterExchange
* @param string $xDeadLetterRoutingKey
*
* @return $this
*/
public function setDelayedQueue(string $queueName, int $xMessageTtl, string $xDeadLetterExchange, string $xDeadLetterRoutingKey) : self
{
$this->setArguments([
'x-message-ttl' => ['I', $xMessageTtl * 1000], // 毫秒
'x-dead-letter-exchange' => ['S', $xDeadLetterExchange],
'x-dead-letter-routing-key' => ['S', $xDeadLetterRoutingKey],
]);
$this->setQueue($queueName);
return $this;
}
}
DelayProducer.php
<?php
declare(strict_types =