Skip to content
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

Fixed using Symfony Serializer with RabbitMQ. #87

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
63 changes: 50 additions & 13 deletions QueueInteropTransport.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,16 @@
use Enqueue\AmqpTools\RabbitMqDelayPluginDelayStrategy;
use Enqueue\AmqpTools\RabbitMqDlxDelayStrategy;
use Enqueue\MessengerAdapter\EnvelopeItem\InteropMessageStamp;
use Interop\Amqp\AmqpContext;
use Interop\Amqp\AmqpMessage;
use Interop\Queue\Consumer;
use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\Exception\LogicException;
use Symfony\Component\Messenger\Transport\Serialization\PhpSerializer;
use Symfony\Component\Messenger\Transport\Serialization\SerializerInterface;
use Symfony\Component\Messenger\Transport\TransportInterface;
use Interop\Queue\Exception as InteropQueueException;
use Interop\Queue\Message;
use Interop\Queue\Message as InteropMessage;
use Enqueue\MessengerAdapter\Exception\MissingMessageMetadataSetterException;
use Enqueue\MessengerAdapter\Exception\SendingMessageFailedException;
use Enqueue\MessengerAdapter\EnvelopeItem\TransportConfiguration;
Expand Down Expand Up @@ -79,13 +82,7 @@ public function get(): iterable
throw $e;
}

$envelope = $this->serializer->decode(array(
'body' => $interopMessage->getBody(),
'headers' => $interopMessage->getHeaders(),
'properties' => $interopMessage->getProperties(),
));

$envelope = $envelope->with(new InteropMessageStamp($interopMessage));
$envelope = $this->decodeMessage($interopMessage);

return array($envelope);
}
Expand Down Expand Up @@ -201,7 +198,7 @@ private function getDestination(?Envelope $envelope): array
);
}

private function setMessageMetadata(Message $interopMessage, Envelope $envelope): void
private function setMessageMetadata(InteropMessage $interopMessage, Envelope $envelope): void
{
$configuration = $envelope->last(TransportConfiguration::class);

Expand All @@ -221,21 +218,61 @@ private function setMessageMetadata(Message $interopMessage, Envelope $envelope)
}
}

private function encodeMessage(Envelope $envelope): Message
private function encodeMessage(Envelope $envelope): InteropMessage
{
$context = $this->contextManager->context();
$encodedMessage = $this->serializer->encode($envelope);

if ($context instanceof AmqpContext
&& !$this->serializer instanceof PhpSerializer
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if someone is using .a different serializer?

I dont really understand why we should special treat AMQP context and PHPSerializer. Could you please explain that?

) {
// populates rabbit message's headers property
$properties = $encodedMessage['headers'] ?? array();

// populates rabbit message's properties (only those that match valid key)
$headers = $properties;

if (isset($headers['Content-Type'])) {
$headers['content_type'] = $headers['Content-Type'];
}
} else {
$properties = $encodedMessage['properties'] ?? array();
$headers = $encodedMessage['headers'] ?? array();
}

$interopMessage = $context->createMessage(
$encodedMessage['body'],
$encodedMessage['properties'] ?? array(),
$encodedMessage['headers'] ?? array()
$properties,
$headers
);

return $interopMessage;
}

private function findMessage(Envelope $envelope): Message
private function decodeMessage(InteropMessage $interopMessage): Envelope
{
if ($interopMessage instanceof AmqpMessage
&& !$this->serializer instanceof PhpSerializer
) {
$envelope = $this->serializer->decode(array(
'body' => $interopMessage->getBody(),
'headers' => $interopMessage->getProperties(),
'properties' => $interopMessage->getHeaders(),
));
} else {
$envelope = $this->serializer->decode(array(
'body' => $interopMessage->getBody(),
'headers' => $interopMessage->getHeaders(),
'properties' => $interopMessage->getProperties(),
));
}

$envelope = $envelope->with(new InteropMessageStamp($interopMessage));

return $envelope;
}

private function findMessage(Envelope $envelope): InteropMessage
{
/** @var InteropMessageStamp $interopStamp */
$interopStamp = $envelope->last(InteropMessageStamp::class);
Expand Down