Skip to content

Commit 74503b0

Browse files
authored
FEATURE: Add support for DLX
2 parents 8f2b3f3 + fec03a9 commit 74503b0

File tree

3 files changed

+33
-9
lines changed

3 files changed

+33
-9
lines changed

Classes/Queue/RabbitQueue.php

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Flowpack\JobQueue\Common\Exception as JobQueueException;
88
use Flowpack\JobQueue\Common\Queue\Message;
99
use Flowpack\JobQueue\Common\Queue\QueueInterface;
10+
use Neos\Utility\ObjectAccess;
1011
use PhpAmqpLib\Channel\AMQPChannel;
1112
use PhpAmqpLib\Connection\AMQPStreamConnection;
1213
use PhpAmqpLib\Message\AMQPMessage;
@@ -44,6 +45,11 @@ class RabbitQueue implements QueueInterface
4445
*/
4546
protected $routingKey = '';
4647

48+
/**
49+
* @var bool
50+
*/
51+
protected $useDLX = false;
52+
4753
/**
4854
* @param mixed[] $options
4955
*/
@@ -106,6 +112,8 @@ public function __construct(string $name, array $options = [])
106112
$this->channel->queue_bind($this->name, $this->exchangeName, $this->routingKey);
107113
}
108114
}
115+
116+
$this->useDLX = $options['useDLX'] ?? false;
109117
}
110118

111119
protected function connect(): void
@@ -161,19 +169,29 @@ public function release(string $messageId, array $options = []): void
161169
*/
162170
public function reQueueMessage(Message $message, array $releaseOptions): void
163171
{
164-
// Ack the current message
165-
$this->channel->basic_ack($message->getIdentifier());
166-
167-
// requeue the message
168-
$this->queue($message->getPayload(), $releaseOptions, $message->getNumberOfReleases() + 1);
172+
if ($this->useDLX) {
173+
// Use nack to move message to DLX
174+
$this->channel->basic_nack($message->getIdentifier());
175+
} else {
176+
// Ack the current message
177+
$this->channel->basic_ack($message->getIdentifier());
178+
179+
// requeue the message
180+
$this->queue($message->getPayload(), $releaseOptions, $message->getNumberOfReleases() + 1);
181+
}
169182
}
170183

171184
/**
172185
* @inheritdoc
173186
*/
174187
public function abort(string $messageId): void
175188
{
176-
$this->channel->basic_nack($messageId);
189+
if ($this->useDLX) {
190+
// basic_nack would move message to DLX, not actually removing it
191+
$this->channel->basic_ack($messageId);
192+
} else {
193+
$this->channel->basic_nack($messageId);
194+
}
177195
}
178196

179197
/**
@@ -258,7 +276,7 @@ protected function queue(string $payload, array $options = [], int $numberOfRele
258276

259277
$headers = new AMQPTable($headerOptions);
260278
$message->set('application_headers', $headers);
261-
$this->channel->basic_publish($message, $this->exchangeName, $this->routingKey !== '' ? $this->routingKey : $this->name);
279+
$this->channel->basic_publish($message, $this->exchangeName, $this->routingKey);
262280
return $correlationIdentifier;
263281
}
264282

@@ -272,7 +290,11 @@ protected function dequeue(bool $ack = true, ?int $timeout = null): ?Message
272290

273291
/** @var AMQPTable $applicationHeader */
274292
$applicationHeader = $message->get('application_headers')->getNativeData();
275-
$numberOfReleases = $applicationHeader['x-numberOfReleases'] ?? 0;
293+
if ($this->useDLX) {
294+
$numberOfReleases = ObjectAccess::getPropertyPath($applicationHeader, 'x-death.0.count') ?? 0;
295+
} else {
296+
$numberOfReleases = $applicationHeader['x-numberOfReleases'] ?? 0;
297+
}
276298

277299
if ($ack) {
278300
$this->channel->basic_ack($deliveryTag);

Configuration/Settings.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ Neos:
2222
# delay: 5
2323
#
2424
# options:
25-
# routingKey: '' # for advanced exchange configuration
25+
# routingKey: '' # for advanced exchange configuration
26+
# useDLX: false # support for dead letter exchange
2627
#
2728
# # Options that are used to configure a que
2829
# queueOptions:

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ Flowpack:
8686
8787
options:
8888
routingKey: ''
89+
useDLX: true # enable support for dead letter exchange, requires configuration in RabbitMQ
8990
9091
queueOptions:
9192
# don't declare a queue by default

0 commit comments

Comments
 (0)