Skip to content

Latest commit

 

History

History
38 lines (31 loc) · 1.25 KB

Producing.md

File metadata and controls

38 lines (31 loc) · 1.25 KB

Producing

Producing is very simple and done through PublishOperation that is registered as a service and autowired by default by the bundle. The publish method API requires you to pass Connection. It's ready for multiple-connection support that comes in a future.

use Cdn77\RabbitMQBundle\RabbitMQ\Connection;
use Cdn77\RabbitMQBundle\RabbitMQ\Message;
use Cdn77\RabbitMQBundle\RabbitMQ\Operation\PublishOperation;

final class ExampleProducer
{
    /** @var PublishOperation */
    private $publishOperation;

    public function __construct(Connection $connection, PublishOperation $publishOperation)
    {
        $this->publishOperation = $publishOperation;
    }

    /**
     * @param mixed[] $data
     */
    public function publishWithRoutingKey(array $data, string $routingKey) : void
    {
        $message = Message::json(json_encode($data));
        
        // Messages are persistent by default but you can make them transient so they don't persist
        $message->makeTransient();

        $this->publishOperation->handle(
            $this->connection
            $message,
            $routingKey,
            'default_exchange', // Exchange to send the message to
        );
    }
}