-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathRabbitMqDelayPluginDelayStrategy.php
53 lines (42 loc) · 2.1 KB
/
RabbitMqDelayPluginDelayStrategy.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<?php
declare(strict_types=1);
namespace Enqueue\AmqpTools;
use Interop\Amqp\AmqpContext;
use Interop\Amqp\AmqpDestination;
use Interop\Amqp\AmqpMessage;
use Interop\Amqp\AmqpQueue;
use Interop\Amqp\AmqpTopic;
use Interop\Amqp\Impl\AmqpBind;
use Interop\Queue\Exception\InvalidDestinationException;
class RabbitMqDelayPluginDelayStrategy implements DelayStrategy
{
public function delayMessage(AmqpContext $context, AmqpDestination $dest, AmqpMessage $message, int $delay): void
{
$delayMessage = $context->createMessage($message->getBody(), $message->getProperties(), $message->getHeaders());
$delayMessage->setProperty('x-delay', (int) $delay);
$delayMessage->setRoutingKey($message->getRoutingKey());
if ($dest instanceof AmqpTopic) {
$delayTopic = $context->createTopic('enqueue.'.$dest->getTopicName().'.delayed');
$delayTopic->setType('x-delayed-message');
$delayTopic->addFlag($dest->getFlags());
$delayTopic->setArgument('x-delayed-type', $dest->getType());
$context->declareTopic($delayTopic);
$context->bind(new AmqpBind($dest, $delayTopic, $delayMessage->getRoutingKey()));
} elseif ($dest instanceof AmqpQueue) {
$delayTopic = $context->createTopic('enqueue.queue.delayed');
$delayTopic->setType('x-delayed-message');
$delayTopic->addFlag(AmqpTopic::FLAG_DURABLE);
$delayTopic->setArgument('x-delayed-type', AmqpTopic::TYPE_DIRECT);
$delayMessage->setRoutingKey($dest->getQueueName());
$context->declareTopic($delayTopic);
$context->bind(new AmqpBind($dest, $delayTopic, $delayMessage->getRoutingKey()));
} else {
throw new InvalidDestinationException(sprintf('The destination must be an instance of %s but got %s.', AmqpTopic::class.'|'.AmqpQueue::class, $dest::class));
}
$producer = $context->createProducer();
if ($producer instanceof DelayStrategyAware) {
$producer->setDelayStrategy(null);
}
$producer->send($delayTopic, $delayMessage);
}
}