8000 [Messenger][Amqp] Fix wrong routing key when use failure queue by fabcdl · Pull Request #52083 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Messenger][Amqp] Fix wrong routing key when use failure queue #52083

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: 6.3
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -804,6 +804,23 @@ private function createDelayOrRetryConnection(\AMQPExchange $delayExchange, stri

return Connection::fromDsn('amqp://localhost', [], $factory);
}

public function testForcePublishWithDefaultRoutingKey()
{
$factory = new TestAmqpFactory(
$amqpConnection = $this->createMock(\AMQPConnection::class),
$amqpChannel = $this->createMock(\AMQPChannel::class),
$amqpQueue = $this->createMock(\AMQPQueue::class),
$amqpExchange = $this->createMock(\AMQPExchange::class)
);

$amqpExchange->expects($this->once())->method('publish')->with('body', 'default_routing_key');
$amqpEnvelope = $this->createMock(\AMQPEnvelope::class);
$amqpStamp = AmqpStamp::createFromAmqpEnvelope($amqpEnvelope, null, '', true);

$connection = Connection::fromDsn('amqp://localhost?exchange[default_publish_routing_key]=default_routing_key', [], $factory);
$connection->publish('body', [], 0, $amqpStamp);
}
}

class TestAmqpFactory extends AmqpFactory
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Symfony\Component\Messenger\Exception\TransportException;
use Symfony\Component\Messenger\Stamp\DelayStamp;
use Symfony\Component\Messenger\Stamp\RedeliveryStamp;
use Symfony\Component\Messenger\Stamp\SentToFailureTransportStamp;
use Symfony\Component\Messenger\Transport\Sender\SenderInterface;
use Symfony\Component\Messenger\Transport\Serialization\PhpSerializer;
use Symfony\Component\Messenger\Transport\Serialization\SerializerInterface;
Expand Down Expand Up @@ -59,7 +60,8 @@ public function send(Envelope $envelope): Envelope
$amqpStamp = AmqpStamp::createFromAmqpEnvelope(
$amqpReceivedStamp->getAmqpEnvelope(),
$amqpStamp,
$envelope->last(RedeliveryStamp::class) ? $amqpReceivedStamp->getQueueName() : null
$envelope->last(RedeliveryStamp::class) ? $amqpReceivedStamp->getQueueName() : null,
$envelope->last(SentToFailureTransportStamp::class) ? true : false
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ final class AmqpStamp implements NonSendableStampInterface
private int $flags;
private array $attributes;
private bool $isRetryAttempt = false;
private bool $forceDefaultRoutingKey = false;

public function __construct(string $routingKey = null, int $flags = \AMQP_NOPARAM, array $attributes = [])
{
Expand All @@ -46,7 +47,7 @@ public function getAttributes(): array
return $this->attributes;
}

public static function createFromAmqpEnvelope(\AMQPEnvelope $amqpEnvelope, self $previousStamp = null, string $retryRoutingKey = null): self
public static function createFromAmqpEnvelope(\AMQPEnvelope $amqpEnvelope, self $previousStamp = null, string $retryRoutingKey = null, bool $forceDefaultRoutingKey = false): self
{
$attr = $previousStamp->attributes ?? [];

Expand All @@ -71,6 +72,8 @@ public static function createFromAmqpEnvelope(\AMQPEnvelope $amqpEnvelope, self
$stamp->isRetryAttempt = true;
}

$stamp->forceDefaultRoutingKey = $forceDefaultRoutingKey;

return $stamp;
}

Expand All @@ -87,4 +90,9 @@ public static function createWithAttributes(array $attributes, self $previousSta
array_merge($previousStamp->attributes ?? [], $attributes)
);
}

public function isForceDefaultRoutingKey(): bool
{
return $this->forceDefaultRoutingKey;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -563,6 +563,10 @@ public function purgeQueues(): void

private function getRoutingKeyForMessage(?AmqpStamp $amqpStamp): ?string
{
if ($amqpStamp?->isForceDefaultRoutingKey()) {
return $this->getDefaultPublishRoutingKey();
}

return $amqpStamp?->getRoutingKey() ?? $this->getDefaultPublishRoutingKey();
}
}
0