MessageBus
Raxos\MessageBus\MessageBus owns the AMQP connection and acts as a factory for queues. Construct one per process, create the queues you need from it, and close it when you are done.
final readonly class MessageBus implements MessageBusInterfaceIt implements Raxos\Contract\MessageBus\MessageBusInterface.
Constructor
public function __construct(
string $host,
int $port,
string $username,
string $password,
string $vhost = '/'
)Opens an AMQP stream connection to the given RabbitMQ server. The $vhost defaults to /. If the connection cannot be opened, the constructor throws a MessageBusConnectionException. The host, port, username and password are marked as sensitive parameters so they are redacted in stack traces.
use Raxos\MessageBus\MessageBus;
$bus = new MessageBus(
host: 'localhost',
port: 5672,
username: 'guest',
password: 'guest',
vhost: '/'
);Methods
createQueue()
public function createQueue(string $name = 'task_queue', int $maxMessages = 25): MessageBusQueueInterfaceDeclares a durable, priority aware queue on the connection and returns a MessageBusQueue bound to it. The queue is tracked by the bus so close() can clean it up later. $maxMessages sets how many messages a consumer on the returned queue processes before it stops. Throws a MessageBusTimeoutException if the broker does not respond in time.
$queue = $bus->createQueue('emails', maxMessages: 50);removeQueue()
public function removeQueue(MessageBusQueueInterface $queue): voidStops tracking a queue that was created earlier. This is called for you by MessageBusQueue::close(); you rarely need to call it directly. It does not close the queue's channel on its own.
close()
public function close(): voidCloses every tracked queue and then the underlying AMQP connection. Throws a MessageBusConnectionException if closing fails.
$bus->close();See also
- MessageBusQueue: the queue returned by
createQueue(). - Publishing and consuming: the full lifecycle in context.
- Exceptions: the failures these methods can throw.