Skip to content

Commit 29e5bde

Browse files
committed
[Messenger] fix manual amqp setup when autosetup disabled
1 parent 69de4d2 commit 29e5bde

File tree

1 file changed

+16
-13
lines changed

1 file changed

+16
-13
lines changed

src/Symfony/Component/Messenger/Bridge/Amqp/Transport/Connection.php

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ class Connection
8080
private $queuesOptions;
8181
private $amqpFactory;
8282
private $autoSetupExchange;
83-
private $autoSetup;
83+
private $autoSetupDelayExchange;
8484

8585
/**
8686
* @var \AMQPChannel|null
@@ -114,7 +114,7 @@ public function __construct(array $connectionOptions, array $exchangeOptions, ar
114114
'queue_name_pattern' => 'delay_%exchange_name%_%routing_key%_%delay%',
115115
],
116116
], $connectionOptions);
117-
$this->autoSetupExchange = $this->autoSetup = $connectionOptions['auto_setup'] ?? true;
117+
$this->autoSetupExchange = $this->autoSetupDelayExchange = $connectionOptions['auto_setup'] ?? true;
118118
$this->exchangeOptions = $exchangeOptions;
119119
$this->queuesOptions = $queuesOptions;
120120
$this->amqpFactory = $amqpFactory ?? new AmqpFactory();
@@ -288,16 +288,16 @@ public function publish(string $body, array $headers = [], int $delayInMs = 0, A
288288
{
289289
$this->clearWhenDisconnected();
290290

291+
if ($this->autoSetupExchange) {
292+
$this->setupExchangeAndQueues(); // also setup normal exchange for delayed messages so delay queue can DLX messages to it
293+
}
294+
291295
if (0 !== $delayInMs) {
292296
$this->publishWithDelay($body, $headers, $delayInMs, $amqpStamp);
293297

294298
return;
295299
}
296300

297-
if ($this->autoSetupExchange) {
298-
$this->setupExchangeAndQueues();
299-
}
300-
301301
$this->publishOnExchange(
302302
$this->exchange(),
303303
$body,
@@ -356,8 +356,8 @@ private function publishOnExchange(\AMQPExchange $exchange, string $body, string
356356

357357
private function setupDelay(int $delay, ?string $routingKey)
358358
{
359-
if ($this->autoSetup) {
360-
$this->setup(); // setup delay exchange and normal exchange for delay queue to DLX messages to
359+
if ($this->autoSetupDelayExchange) {
360+
$this->setupDelayExchange();
361361
}
362362

363363
$queue = $this->createDelayQueue($delay, $routingKey);
@@ -450,11 +450,8 @@ public function nack(\AMQPEnvelope $message, string $queueName, int $flags = \AM
450450

451451
public function setup(): void
452452
{
453-
if ($this->autoSetupExchange) {
454-
$this->setupExchangeAndQueues();
455-
}
456-
$this->getDelayExchange()->declareExchange();
457-
$this->autoSetup = false;
453+
$this->setupExchangeAndQueues();
454+
$this->setupDelayExchange();
458455
}
459456

460457
private function setupExchangeAndQueues(): void
@@ -470,6 +467,12 @@ private function setupExchangeAndQueues(): void
470467
$this->autoSetupExchange = false;
471468
}
472469

470+
private function setupDelayExchange(): void
471+
{
472+
$this->getDelayExchange()->declareExchange();
473+
$this->autoSetupDelayExchange = false;
474+
}
475+
473476
/**
474477
* @return string[]
475478
*/

0 commit comments

Comments
 (0)