Skip to content

Commit 2ec6e26

Browse files
committed
[amqp] add support of ttl and priority on producer.
1 parent adf2a07 commit 2ec6e26

21 files changed

Lines changed: 286 additions & 32 deletions

‎composer.json‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@
2424
"enqueue/test": "*@dev",
2525
"enqueue/async-event-dispatcher": "*@dev",
2626
"queue-interop/queue-interop": "^0.6@dev",
27-
"queue-interop/queue-spec": "^0.6@dev",
28-
"queue-interop/amqp-interop": "^0.5@dev",
27+
"queue-interop/amqp-interop": "^0.6@dev",
28+
"queue-interop/queue-spec": "^0.5@dev",
2929

3030
"phpunit/phpunit": "^5",
3131
"doctrine/doctrine-bundle": "~1.2",

‎pkg/amqp-ext/AmqpProducer.php‎

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Interop\Amqp\AmqpProducer as InteropAmqpProducer;
77
use Interop\Amqp\AmqpQueue;
88
use Interop\Amqp\AmqpTopic;
9+
use Interop\Queue\DeliveryDelayNotSupportedException;
910
use Interop\Queue\InvalidDestinationException;
1011
use Interop\Queue\InvalidMessageException;
1112
use Interop\Queue\PsrDestination;
@@ -14,6 +15,16 @@
1415

1516
class AmqpProducer implements InteropAmqpProducer
1617
{
18+
/**
19+
* @var int|null
20+
*/
21+
private $priority;
22+
23+
/**
24+
* @var int|float|null
25+
*/
26+
private $timeToLive;
27+
1728
/**
1829
* @var \AMQPChannel
1930
*/
@@ -42,6 +53,14 @@ public function send(PsrDestination $destination, PsrMessage $message)
4253

4354
InvalidMessageException::assertMessageInstanceOf($message, AmqpMessage::class);
4455

56+
if (null !== $this->priority && null === $message->getPriority()) {
57+
$message->setPriority($this->priority);
58+
}
59+
60+
if (null !== $this->timeToLive && null === $message->getExpiration()) {
61+
$message->setExpiration($this->timeToLive);
62+
}
63+
4564
$amqpAttributes = $message->getHeaders();
4665

4766
if ($message->getProperties()) {
@@ -80,7 +99,7 @@ public function send(PsrDestination $destination, PsrMessage $message)
8099
*/
81100
public function setDeliveryDelay($deliveryDelay)
82101
{
83-
throw new \LogicException('Not implemented');
102+
throw new DeliveryDelayNotSupportedException('The provider does not support delivery delay feature');
84103
}
85104

86105
/**
@@ -96,30 +115,30 @@ public function getDeliveryDelay()
96115
*/
97116
public function setPriority($priority)
98117
{
99-
throw new \LogicException('Not implemented');
118+
$this->priority = $priority;
100119
}
101120

102121
/**
103122
* {@inheritdoc}
104123
*/
105124
public function getPriority()
106125
{
107-
return null;
126+
return $this->priority;
108127
}
109128

110129
/**
111130
* {@inheritdoc}
112131
*/
113132
public function setTimeToLive($timeToLive)
114133
{
115-
throw new \LogicException('Not implemented');
134+
$this->timeToLive = $timeToLive;
116135
}
117136

118137
/**
119138
* {@inheritdoc}
120139
*/
121140
public function getTimeToLive()
122141
{
123-
return null;
142+
return $this->timeToLive;
124143
}
125144
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
namespace Enqueue\AmqpExt\Tests\Spec;
4+
5+
use Enqueue\AmqpExt\AmqpConnectionFactory;
6+
use Enqueue\AmqpExt\AmqpContext;
7+
use Interop\Queue\PsrContext;
8+
use Interop\Queue\Spec\SendAndReceivePriorityMessagesFromQueueSpec;
9+
10+
/**
11+
* @group functional
12+
*/
13+
class AmqpSendAndReceivePriorityMessagesFromQueueTest extends SendAndReceivePriorityMessagesFromQueueSpec
14+
{
15+
/**
16+
* {@inheritdoc}
17+
*/
18+
protected function createContext()
19+
{
20+
$factory = new AmqpConnectionFactory(getenv('AMQP_DSN'));
21+
22+
return $factory->createContext();
23+
}
24+
25+
/**
26+
* {@inheritdoc}
27+
*
28+
* @param AmqpContext $context
29+
*/
30+
protected function createQueue(PsrContext $context, $queueName)
31+
{
32+
$queue = $context->createQueue($queueName);
33+
$queue->setArguments(['x-max-priority' => 10]);
34+
35+
$context->declareQueue($queue);
36+
$context->purgeQueue($queue);
37+
38+
return $queue;
39+
}
40+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
namespace Enqueue\AmqpExt\Tests\Spec;
4+
5+
use Enqueue\AmqpExt\AmqpConnectionFactory;
6+
use Enqueue\AmqpExt\AmqpContext;
7+
use Interop\Queue\PsrContext;
8+
use Interop\Queue\Spec\SendAndReceiveTimeToLiveMessagesFromQueueSpec;
9+
10+
/**
11+
* @group functional
12+
*/
13+
class AmqpSendAndReceiveTimeToLiveMessagesFromQueueTest extends SendAndReceiveTimeToLiveMessagesFromQueueSpec
14+
{
15+
/**
16+
* {@inheritdoc}
17+
*/
18+
protected function createContext()
19+
{
20+
$factory = new AmqpConnectionFactory(getenv('AMQP_DSN'));
21+
22+
return $factory->createContext();
23+
}
24+
25+
/**
26+
* {@inheritdoc}
27+
*
28+
* @param AmqpContext $context
29+
*/
30+
protected function createQueue(PsrContext $context, $queueName)
31+
{
32+
$queue = $context->createQueue($queueName);
33+
$context->declareQueue($queue);
34+
$context->purgeQueue($queue);
35+
36+
return $queue;
37+
}
38+
}

‎pkg/amqp-ext/composer.json‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,16 @@
1313
"require": {
1414
"php": ">=5.6",
1515
"ext-amqp": "^1.6",
16-
"queue-interop/queue-interop": "^0.6@dev",
17-
"queue-interop/amqp-interop": "^0.5@dev",
16+
17+
"queue-interop/amqp-interop": "^0.6@dev",
1818
"psr/log": "^1"
1919
},
2020
"require-dev": {
2121
"phpunit/phpunit": "~5.4.0",
2222
"enqueue/test": "^0.7@dev",
2323
"enqueue/enqueue": "^0.7@dev",
2424
"enqueue/null": "^0.7@dev",
25-
"queue-interop/queue-spec": "^0.6@dev",
25+
"queue-interop/queue-spec": "^0.5@dev",
2626
"empi89/php-amqp-stubs": "*@dev",
2727
"symfony/dependency-injection": "^2.8|^3",
2828
"symfony/config": "^2.8|^3"

‎pkg/amqp-lib/AmqpContext.php‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Interop\Queue\PsrTopic;
1818
use PhpAmqpLib\Channel\AMQPChannel;
1919
use PhpAmqpLib\Connection\AbstractConnection;
20+
use PhpAmqpLib\Wire\AMQPTable;
2021

2122
class AmqpContext implements InteropAmqpContext
2223
{
@@ -173,7 +174,7 @@ public function declareQueue(InteropAmqpQueue $queue)
173174
(bool) ($queue->getFlags() & InteropAmqpQueue::FLAG_EXCLUSIVE),
174175
(bool) ($queue->getFlags() & InteropAmqpQueue::FLAG_AUTODELETE),
175176
(bool) ($queue->getFlags() & InteropAmqpQueue::FLAG_NOWAIT),
176-
$queue->getArguments()
177+
$queue->getArguments() ? new AMQPTable($queue->getArguments()) : null
177178
);
178179
}
179180

‎pkg/amqp-lib/AmqpProducer.php‎

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Interop\Amqp\AmqpProducer as InteropAmqpProducer;
77
use Interop\Amqp\AmqpQueue as InteropAmqpQueue;
88
use Interop\Amqp\AmqpTopic as InteropAmqpTopic;
9+
use Interop\Queue\DeliveryDelayNotSupportedException;
910
use Interop\Queue\InvalidDestinationException;
1011
use Interop\Queue\InvalidMessageException;
1112
use Interop\Queue\PsrDestination;
@@ -17,6 +18,16 @@
1718

1819
class AmqpProducer implements InteropAmqpProducer
1920
{
21+
/**
22+
* @var int|null
23+
*/
24+
private $priority;
25+
26+
/**
27+
* @var int|float|null
28+
*/
29+
private $timeToLive;
30+
2031
/**
2132
* @var AMQPChannel
2233
*/
@@ -43,6 +54,14 @@ public function send(PsrDestination $destination, PsrMessage $message)
4354

4455
InvalidMessageException::assertMessageInstanceOf($message, InteropAmqpMessage::class);
4556

57+
if (null !== $this->priority && null === $message->getPriority()) {
58+
$message->setPriority($this->priority);
59+
}
60+
61+
if (null !== $this->timeToLive && null === $message->getExpiration()) {
62+
$message->setExpiration($this->timeToLive);
63+
}
64+
4665
$amqpProperties = $message->getHeaders();
4766

4867
if ($appProperties = $message->getProperties()) {
@@ -75,7 +94,7 @@ public function send(PsrDestination $destination, PsrMessage $message)
7594
*/
7695
public function setDeliveryDelay($deliveryDelay)
7796
{
78-
throw new \LogicException('Not implemented');
97+
throw new DeliveryDelayNotSupportedException('The provider does not support delivery delay feature');
7998
}
8099

81100
/**
@@ -91,30 +110,30 @@ public function getDeliveryDelay()
91110
*/
92111
public function setPriority($priority)
93112
{
94-
throw new \LogicException('Not implemented');
113+
$this->priority = $priority;
95114
}
96115

97116
/**
98117
* {@inheritdoc}
99118
*/
100119
public function getPriority()
101120
{
102-
return null;
121+
return $this->priority;
103122
}
104123

105124
/**
106125
* {@inheritdoc}
107126
*/
108127
public function setTimeToLive($timeToLive)
109128
{
110-
throw new \LogicException('Not implemented');
129+
$this->timeToLive = $timeToLive;
111130
}
112131

113132
/**
114133
* {@inheritdoc}
115134
*/
116135
public function getTimeToLive()
117136
{
118-
return null;
137+
return $this->timeToLive;
119138
}
120139
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
namespace Enqueue\AmqpLib\Tests\Spec;
4+
5+
use Enqueue\AmqpLib\AmqpConnectionFactory;
6+
use Enqueue\AmqpLib\AmqpContext;
7+
use Interop\Queue\PsrContext;
8+
use Interop\Queue\Spec\SendAndReceivePriorityMessagesFromQueueSpec;
9+
10+
/**
11+
* @group functional
12+
*/
13+
class AmqpSendAndReceivePriorityMessagesFromQueueTest extends SendAndReceivePriorityMessagesFromQueueSpec
14+
{
15+
/**
16+
* {@inheritdoc}
17+
*/
18+
protected function createContext()
19+
{
20+
$factory = new AmqpConnectionFactory(getenv('AMQP_DSN'));
21+
22+
return $factory->createContext();
23+
}
24+
25+
/**
26+
* {@inheritdoc}
27+
*
28+
* @param AmqpContext $context
29+
*/
30+
protected function createQueue(PsrContext $context, $queueName)
31+
{
32+
$queue = $context->createQueue($queueName);
33+
$queue->setArguments(['x-max-priority' => 10]);
34+
35+
$context->declareQueue($queue);
36+
$context->purgeQueue($queue);
37+
38+
return $queue;
39+
}
40+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
namespace Enqueue\AmqpLib\Tests\Spec;
4+
5+
use Enqueue\AmqpLib\AmqpConnectionFactory;
6+
use Enqueue\AmqpLib\AmqpContext;
7+
use Interop\Queue\PsrContext;
8+
use Interop\Queue\Spec\SendAndReceiveTimeToLiveMessagesFromQueueSpec;
9+
10+
/**
11+
* @group functional
12+
*/
13+
class AmqpSendAndReceiveTimeToLiveMessagesFromQueueTest extends SendAndReceiveTimeToLiveMessagesFromQueueSpec
14+
{
15+
/**
16+
* {@inheritdoc}
17+
*/
18+
protected function createContext()
19+
{
20+
$factory = new AmqpConnectionFactory(getenv('AMQP_DSN'));
21+
22+
return $factory->createContext();
23+
}
24+
25+
/**
26+
* {@inheritdoc}
27+
*
28+
* @param AmqpContext $context
29+
*/
30+
protected function createQueue(PsrContext $context, $queueName)
31+
{
32+
$queue = $context->createQueue($queueName);
33+
$context->declareQueue($queue);
34+
$context->purgeQueue($queue);
35+
36+
return $queue;
37+
}
38+
}

‎pkg/amqp-lib/composer.json‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,15 @@
1414
"php": ">=5.6",
1515
"php-amqplib/php-amqplib": "^2.7@dev",
1616
"queue-interop/queue-interop": "^0.6@dev",
17-
"queue-interop/amqp-interop": "^0.5@dev",
17+
"queue-interop/amqp-interop": "^0.6@dev",
1818
"psr/log": "^1"
1919
},
2020
"require-dev": {
2121
"phpunit/phpunit": "~5.4.0",
2222
"enqueue/test": "^0.7@dev",
2323
"enqueue/enqueue": "^0.7@dev",
2424
"enqueue/null": "^0.7@dev",
25-
"queue-interop/queue-spec": "^0.6@dev",
25+
"queue-interop/queue-spec": "^0.5@dev",
2626
"symfony/dependency-injection": "^2.8|^3",
2727
"symfony/config": "^2.8|^3"
2828
},

0 commit comments

Comments
 (0)