- 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
{
public function setArguments($arguments) : Builder
{
$this->arguments = array_merge($this->arguments, $arguments);
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 = 1);
namespace App\Components\Amqp;
use Hyperf\Amqp\Annotation\Producer;
use Hyperf\Amqp\Builder;
use Hyperf\Amqp\Message\ProducerMessageInterface;
use Hyperf\Di\Annotation\AnnotationCollector;
use PhpAmqpLib\Message\AMQPMessage;
use Throwable;
class DelayProducer extends Builder
{
public function produce(ProducerMessageInterface $producerMessage, AmqpBuilder $queueBuilder, bool $confirm = false, int $timeout = 5) : bool
{
return retry(1, function () use ($producerMessage, $queueBuilder, $confirm, $timeout)
{
return $this->produceMessage($producerMessage, $queueBuilder, $confirm, $timeout);
});
}
private function produceMessage(ProducerMessageInterface $producerMessage, AmqpBuilder $queueBuilder, bool $confirm = false, int $timeout = 5) : bool
{
$result = false;
$this->injectMessageProperty($producerMessage);
$message = new AMQPMessage($producerMessage->payload(), $producerMessage->getProperties());
$pool = $this->getConnectionPool($producerMessage->getPoolName());
$connection = $pool->get();
if ($confirm) {
$channel = $connection->getConfirmChannel();
} else {
$channel = $connection->getChannel();
}
$channel->set_ack_handler(function () use (&$result)
{
$result = true;
});
try {
$exchangeBuilder = $producerMessage->getExchangeBuilder();
$channel->queue_declare($queueBuilder->getQueue(), $queueBuilder->isPassive(), $queueBuilder->isDurable(), $queueBuilder->isExclusive(), $queueBuilder->isAutoDelete(), $queueBuilder->isNowait(), $queueBuilder->getArguments(), $queueBuilder->getTicket());
$channel->exchange_declare($exchangeBuilder->getExchange(), $exchangeBuilder->getType(), $exchangeBuilder->isPassive(), $exchangeBuilder->isDurable(), $exchangeBuilder->isAutoDelete(), $exchangeBuilder->isInternal(), $exchangeBuilder->isNowait(), $exchangeBuilder->getArguments(), $exchangeBuilder->getTicket());
$channel->queue_bind($queueBuilder->getQueue(), $producerMessage->getExchange(), $producerMessage->getRoutingKey());
$channel->basic_publish($message, $producerMessage->getExchange(), $producerMessage->getRoutingKey());
$channel->wait_for_pending_acks_returns($timeout);
} catch (Throwable $exception) {
$connection->reconnect();
throw $exception;
}
finally {
$connection->release();
}
return $confirm ? $result : true;
}
private function injectMessageProperty(ProducerMessageInterface $producerMessage) : void
{
if (class_exists(AnnotationCollector::class)) {
$annotation = AnnotationCollector::getClassAnnotation(get_class($producerMessage), Producer::class);
if ($annotation) {
$annotation->routingKey && $producerMessage->setRoutingKey($annotation->routingKey);
$annotation->exchange && $producerMessage->setExchange($annotation->exchange);
}
}
}
}
處理超時訂單
- Orderqueueconsumer.Php
- Orderqueueproducer.Php
Orderqueueproducer.php
<?php
declare(strict_types = 1);
namespace App\Amqp\Producer;
use Hyperf\Amqp\Annotation\Producer;
use Hyperf\Amqp\Builder\ExchangeBuilder;
use Hyperf\Amqp\Message\ProducerMessage;
class OrderQueueProducer extends ProducerMessage
{
public function __construct($data)
{
$this->payload = $data;
}
public function getExchangeBuilder() : ExchangeBuilder
{
return parent::getExchangeBuilder();
}
}
Orderqueueconsumer.php
<?php
declare(strict_types = 1);
namespace App\Amqp\Consumer;
use App\Service\CityTransport\OrderService;
use Hyperf\Amqp\Result;
use Hyperf\Amqp\Annotation\Consumer;
use Hyperf\Amqp\Message\ConsumerMessage;
class OrderQueueConsumer extends ConsumerMessage
{
public function consume($data) : string
{
##業務處理
}
public function isEnable() : bool
{
return true;
}
}
demo
$builder = new AmqpBuilder();
$builder->setDelayedQueue('order_exchange', 1, 'delay_exchange', 'delay_route');
$que = ApplicationContext::getContainer()->get(DelayProducer::class);
var_dump($que->produce(new OrderQueueProducer(['order_sn' => (string)mt_rand(10000, 90000)]), $builder))
本作品採用《CC 協議》,轉載必須註明作者和本文連結