small / broker-client
This package is abandoned and no longer maintained.
No replacement package was suggested.
Implements generic message broker consumers and producers in OpenSwoole environment
0.1
2023-10-21 21:58 UTC
Requires
- php: >=8.2
- php-amqplib/php-amqplib: 3.*
- small/swoole-patterns: >=22.2.0
Requires (Dev)
README
This library is a generic client for message brokers.
Supported messages brokers
- RabbitMq
Produce messages
Create a pool of producer to send messages :
$numProducers = 10;
$pool = new \Small\SwoolePatterns\Pool\Pool(
new \Small\BrokerClient\Manager\RabbitMqProducerManager(
$config = new \Small\BrokerClient\Config\RabbitMqConfig(
'rabbitmq.server',
'guest',
'guest',
),
$topic ='test',
), $numProducers
);
Then use pool :
$message = 'This is a test';
// Get producer
/** @var \Small\BrokerClient\Producer\RabbitMqProducer $producer */
$producer = $pool->get();
// Publish message
$producer->publish(
new Small\BrokerClient\Message\BasicEnvelop($message)
);
// Don't forget to release producer
$pool->put($producer);
Consume messages
Define a class to consume messages :
class Consumer extends \Small\BrokerClient\Consumer\AbstractRabbitMqConsumer
{
public string $test;
protected function execute(\Small\BrokerClient\Message\AbstractEnvelop $envelop): bool
{
$this->test = $envelop->getStringMessage();
return true;
}
}
And run consuming :
$munMessagesToTreat = 10;
(new Consumer())
->consume($config, $topic, 10)
;
Easier producers management with registry
// Create registry
$registry = new \Small\BrokerClient\Registry\ProducerPoolRegistry();
// Register producer
$registry->createPool(
'testProducer'
new \Small\BrokerClient\Config\RabbitMqConfig(
'rabbitmq.server',
'guest',
'guest',
), 'testTopic',
$numConnections = 20,
);
// Use producer
$producer = $registry->getPool('testProducer')->get();
$producer->publish(
new Small\BrokerClient\Message\BasicEnvelop(
'this is a test'
)
);
$registry->getPool('testProducer')->put($producer);