Integrate RabbitMQ headers in queue messages
Edit on GitHubThe main goal of RabbitMQ headers in the Advanced Message Queuing Protocol (AMQP) message is to store additional information about the message. The following example shows the RabbitMQ message usage with demo headers.
<?php
$headers = [
'header1' => 'header1',
'header2' => 'header2',
....,
];
$messageTransfer = (new QueueSendMessageTransfer())
->setHeaders($headers)
...;
// OR
$messageTransfer = (new QueueSendMessageTransfer())
->addHeader('header1', 'header1')
->addHeader('header2', 'header2')
...;
Integrate RabbitMQ headers
- Register a new RabbitMQ queue:
<?php
/**
* This file is part of the Spryker Suite.
* For full license information, please view the LICENSE file that was distributed with this source code.
*/
namespace Pyz\Client\RabbitMq;
...
class RabbitMqConfig extends SprykerRabbitMqConfig
{
/**
* @return \ArrayObject
*/
protected function getQueueOptions()
{
$queueOptionCollection = new ArrayObject();
.....
$queueOptionCollection->append($this->createQueueOption({MODULENAME}Constants::DEMO_QUEUE_NAME, {MODULENAME}Constants::'DEMO_ERROR_QUEUE_NAME'));
return $queueOptionCollection;
}
....
}
- Create a queue writer:
<?php
/**
* This file is part of the Spryker Suite.
* For full license information, please view the LICENSE file that was distributed with this source code.
*/
namespace Pyz\Zed\Mail\Business\Model;
...
class DemoQueueWriter implements DemoQueueWriterInterface
{
/**
* @var \Spryker\Client\Queue\QueueClientInterface $queueClient
*/
private $queueClient;
/**
* The constructor of MailQueueHelper
*
* @param \Spryker\Client\Queue\QueueClientInterface $queueClient
*/
public function __construct(QueueClientInterface $queueClient)
{
$this->queueClient = $queueClient;
}
/**
* @param \Generated\Shared\Transfer\DataTransfer $dataTransfer
*
* @return void
*/
protected function sendMessageToQueue(DataTransfer $dataTransfer): void
{
$queueSendMessageTransfer = (new QueueSendMessageTransfer())
->addHeader('header1', 'header1')
->addHeader('header2', 'header2')
->setBody(json_encode($dataTransfer->toArray()));
$this->queueClient->sendMessage(
{MODULENAME}Constants::DEMO_QUEUE_NAME,
$queueSendMessageTransfer
);
}
....
}
Thank you!
For submitting the form