Closed angelfs closed 4 years ago
The headers
field is very important because it specifies, which class the message belongs to and also the body content type.
Please just implement your own Decoder like so:
<?php
namespace App\Message;
use Koco\Kafka\Messenger\KafkaMessageDecoderInterface;
use RdKafka\Message;
class MyDecoder implements KafkaMessageDecoderInterface
{
/**
* @inheritDoc
*/
public function decode(Message $message): array
{
return [
'body' => $message->payload,
'headers' => [
'type' => TestMessage::class,
'Content-Type' => 'application/json'
]
];
}
/**
* @inheritDoc
*/
public function supports(Message $message): bool
{
return $message->topic_name === 'test';
}
}
You can put any logic in the decode method, to identify, which Class of Message it is.
And tag the service like so:
services:
App\Message\MyDecoder:
tags: ['koco.messenger_kafka.decoder']
Added this documentation to https://github.com/KonstantinCodes/messenger-kafka#custom-message-formats
@angelfs https://github.com/KonstantinCodes/messenger-kafka/pull/17 Thought this could be interesting for you.
@angelfs New method is purely using the Symfony Serializer. That's better and more future proof. Documented here: https://github.com/KonstantinCodes/messenger-kafka#custom-message-formats
There is an error with this $decodeMessage['header'] because payload have not header.