Skip to content

Commit 20c030d

Browse files
committed
delay strategy
1 parent 8c0385a commit 20c030d

4 files changed

Lines changed: 50 additions & 11 deletions

File tree

‎pkg/amqp-ext/AmqpConnectionFactory.php‎

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,14 @@
22

33
namespace Enqueue\AmqpExt;
44

5+
use Enqueue\AmqpTools\DelayStrategyAware;
6+
use Enqueue\AmqpTools\DelayStrategyAwareTrait;
57
use Interop\Amqp\AmqpConnectionFactory as InteropAmqpConnectionFactory;
68

7-
class AmqpConnectionFactory implements InteropAmqpConnectionFactory
9+
class AmqpConnectionFactory implements InteropAmqpConnectionFactory, DelayStrategyAware
810
{
11+
use DelayStrategyAwareTrait;
12+
913
/**
1014
* @var array
1115
*/
@@ -79,12 +83,18 @@ public function __construct($config = 'amqp://')
7983
public function createContext()
8084
{
8185
if ($this->config['lazy']) {
82-
return new AmqpContext(function () {
86+
$context = new AmqpContext(function () {
8387
return $this->createExtContext($this->establishConnection());
8488
}, $this->config['receive_method']);
89+
$context->setDelayStrategy($this->delayStrategy);
90+
91+
return $context;
8592
}
8693

87-
return new AmqpContext($this->createExtContext($this->establishConnection()), $this->config['receive_method']);
94+
$context = new AmqpContext($this->createExtContext($this->establishConnection()), $this->config['receive_method']);
95+
$context->setDelayStrategy($this->delayStrategy);
96+
97+
return $context;
8898
}
8999

90100
/**

‎pkg/amqp-ext/AmqpContext.php‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Enqueue\AmqpExt;
44

5+
use Enqueue\AmqpTools\DelayStrategyAware;
6+
use Enqueue\AmqpTools\DelayStrategyAwareTrait;
57
use Interop\Amqp\AmqpBind as InteropAmqpBind;
68
use Interop\Amqp\AmqpContext as InteropAmqpContext;
79
use Interop\Amqp\AmqpQueue as InteropAmqpQueue;
@@ -15,8 +17,10 @@
1517
use Interop\Queue\PsrDestination;
1618
use Interop\Queue\PsrTopic;
1719

18-
class AmqpContext implements InteropAmqpContext
20+
class AmqpContext implements InteropAmqpContext, DelayStrategyAware
1921
{
22+
use DelayStrategyAwareTrait;
23+
2024
/**
2125
* @var \AMQPChannel
2226
*/
@@ -217,7 +221,10 @@ public function createTemporaryQueue()
217221
*/
218222
public function createProducer()
219223
{
220-
return new AmqpProducer($this->getExtChannel());
224+
$producer = new AmqpProducer($this->getExtChannel(), $this);
225+
$producer->setDelayStrategy($this->delayStrategy);
226+
227+
return $producer;
221228
}
222229

223230
/**

‎pkg/amqp-ext/AmqpProducer.php‎

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Enqueue\AmqpExt;
44

5+
use Enqueue\AmqpTools\DelayStrategyAware;
6+
use Enqueue\AmqpTools\DelayStrategyAwareTrait;
57
use Interop\Amqp\AmqpMessage;
68
use Interop\Amqp\AmqpProducer as InteropAmqpProducer;
79
use Interop\Amqp\AmqpQueue;
@@ -13,8 +15,10 @@
1315
use Interop\Queue\PsrMessage;
1416
use Interop\Queue\PsrTopic;
1517

16-
class AmqpProducer implements InteropAmqpProducer
18+
class AmqpProducer implements InteropAmqpProducer, DelayStrategyAware
1719
{
20+
use DelayStrategyAwareTrait;
21+
1822
/**
1923
* @var int|null
2024
*/
@@ -30,12 +34,24 @@ class AmqpProducer implements InteropAmqpProducer
3034
*/
3135
private $amqpChannel;
3236

37+
/**
38+
* @var AmqpContext
39+
*/
40+
private $context;
41+
42+
/**
43+
* @var int
44+
*/
45+
private $deliveryDelay;
46+
3347
/**
3448
* @param \AMQPChannel $ampqChannel
49+
* @param AmqpContext $context
3550
*/
36-
public function __construct(\AMQPChannel $ampqChannel)
51+
public function __construct(\AMQPChannel $ampqChannel, AmqpContext $context)
3752
{
3853
$this->amqpChannel = $ampqChannel;
54+
$this->context = $context;
3955
}
4056

4157
/**
@@ -67,7 +83,9 @@ public function send(PsrDestination $destination, PsrMessage $message)
6783
$amqpAttributes['headers'] = $message->getProperties();
6884
}
6985

70-
if ($destination instanceof AmqpTopic) {
86+
if ($this->deliveryDelay) {
87+
$this->delayStrategy->delayMessage($this->context, $destination, $message, $this->deliveryDelay);
88+
} elseif ($destination instanceof AmqpTopic) {
7189
$amqpExchange = new \AMQPExchange($this->amqpChannel);
7290
$amqpExchange->setType($destination->getType());
7391
$amqpExchange->setName($destination->getTopicName());
@@ -99,15 +117,19 @@ public function send(PsrDestination $destination, PsrMessage $message)
99117
*/
100118
public function setDeliveryDelay($deliveryDelay)
101119
{
102-
throw DeliveryDelayNotSupportedException::providerDoestNotSupportIt();
120+
if (null === $this->delayStrategy) {
121+
throw DeliveryDelayNotSupportedException::providerDoestNotSupportIt();
122+
}
123+
124+
$this->deliveryDelay = $deliveryDelay;
103125
}
104126

105127
/**
106128
* {@inheritdoc}
107129
*/
108130
public function getDeliveryDelay()
109131
{
110-
return null;
132+
return $this->deliveryDelay;
111133
}
112134

113135
/**

‎pkg/amqp-tools/DelayStrategyAwareTrait.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ trait DelayStrategyAwareTrait
1010
protected $delayStrategy;
1111

1212
/**
13-
* {@inheritdoc}
13+
* @param DelayStrategy|null $delayStrategy
1414
*/
1515
public function setDelayStrategy(DelayStrategy $delayStrategy = null)
1616
{

0 commit comments

Comments
 (0)