MqServiceBase
in package
Table of Contents
Properties
- $channel : AMQPChannel|null
- $connection : AMQPConnection|null
- $eventHandlers : array<string|int, mixed>
- $exchange : AmqpExchange|null
- $exchangeName : string
- $host : string
- $password : string
- $port : int
- $queueName : string|null
- $requeueUnhandled : bool
- $user : string
- $vhost : string
Methods
- __construct() : mixed
- Create a new instance of the MqService class.
- __destruct() : mixed
- Destructor to ensure connections are properly closed
- closeConnection() : void
- Close the AMQP connection and channel, and therefore remove all subscriptions.
- openConnection() : AMQPConnection
- Get the AMQP connection instance. If it does not exist, it will be created.
- publish() : void
- Publish a message to the service's queue, including the event name in the headers.
- startConsuming() : void
- Start consuming messages and routing them to registered handlers.
- subscribe() : void
- Register a handler for a specific event type.
- getChannel() : AMQPChannel
- Get the AMQP channel. If it does not exist, it will be created along with the queue.
- handleMessage() : void
- Handle an incoming message by routing it to the appropriate event handler.
Properties
$channel
private
AMQPChannel|null
$channel
= null
$connection
private
AMQPConnection|null
$connection
= null
$eventHandlers
private
array<string|int, mixed>
$eventHandlers
= []
$exchange
private
AmqpExchange|null
$exchange
= null
$exchangeName read-only
private
string
$exchangeName
= 'general'
$host read-only
private
string
$host
= 'localhost'
$password read-only
private
string
$password
= 'guest'
$port read-only
private
int
$port
= 5672
$queueName read-only
private
string|null
$queueName
= null
$requeueUnhandled read-only
private
bool
$requeueUnhandled
= false
$user read-only
private
string
$user
= 'guest'
$vhost read-only
private
string
$vhost
= '/'
Methods
__construct()
Create a new instance of the MqService class.
public
__construct([string $host = 'localhost' ][, int $port = 5672 ][, string $user = 'guest' ][, string $password = 'guest' ][, string $queueName = null ][, bool $requeueUnhandled = false ][, string $vhost = '/' ][, string $exchangeName = 'general' ]) : mixed
It will automatically connect to the AMQP server as soon as a method is called, and disconnect when the object is destroyed or the closeConnection method is called. If you just want to send a message, you still have to call the closeConnection method.
Parameters
- $host : string = 'localhost'
-
The AMQP server hostname
- $port : int = 5672
-
The AMQP server port
- $user : string = 'guest'
-
The AMQP server username
- $password : string = 'guest'
-
The AMQP server password
- $queueName : string = null
-
The queue name for the service, normally the service name
- $requeueUnhandled : bool = false
- $vhost : string = '/'
-
The AMQP server vhost
- $exchangeName : string = 'general'
-
The AMQP exchange name
__destruct()
Destructor to ensure connections are properly closed
public
__destruct() : mixed
closeConnection()
Close the AMQP connection and channel, and therefore remove all subscriptions.
public
closeConnection() : void
openConnection()
Get the AMQP connection instance. If it does not exist, it will be created.
public
openConnection() : AMQPConnection
Return values
AMQPConnection —The connection instance
publish()
Publish a message to the service's queue, including the event name in the headers.
public
publish(string $eventName, string $payload) : void
Parameters
- $eventName : string
-
The event name
- $payload : string
-
The message content
Tags
startConsuming()
Start consuming messages and routing them to registered handlers.
public
startConsuming() : void
This method will block until the channel is closed.
Tags
subscribe()
Register a handler for a specific event type.
public
subscribe(string $eventName, callable $handler) : void
Parameters
- $eventName : string
-
The event name to subscribe to
- $handler : callable
-
Function to process received messages (function(string $payload))
Tags
getChannel()
Get the AMQP channel. If it does not exist, it will be created along with the queue.
private
getChannel() : AMQPChannel
Return values
AMQPChannel —The channel instance
handleMessage()
Handle an incoming message by routing it to the appropriate event handler.
private
handleMessage(AMQPEnvelope $message, AMQPQueue $queue) : void
Parameters
- $message : AMQPEnvelope
-
The incoming AMQP message
- $queue : AMQPQueue