Download the PHP package roslov/queue-bundle without Composer
On this page you can find all versions of the php package roslov/queue-bundle. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package queue-bundle
Queue Bundle
This package provides the classes needed to work with RabbitMQ.
It is based on RabbitMQ bundle.
Requirements
- PHP 7.4 or higher
- Symfony 3.4 or higher
- Doctrine bundle (optional)
- MySQL DB (optional)
TODO
- [ ] RPC client: Allow multiple request calls
- [ ] Doctrine: Add automatic migrations
- [ ] Doctrine: Add automatic entity setup
- [ ] Tests: Add tests
Installation and usage
Default bundle configuration
The package could be installed with composer:
Then change the default settings by creating config/packages/roslov_queue.yaml
with the content below.
RabbitMQ configuration
This package also installs RabbitMQ bundle. So first, you need to configure RabbitMQ bundle. Follow its documentation. For example:
Consumers and producers
Create DTOs that will be used in consumers and producers, and add them to roslov_queue.payload_mapping
(see examples).
Create a consumer that uses Roslov\QueueBundle\Serializer\MessagePayloadSerializer
as a serializer:
Add your consumers to old_sound_rabbit_mq.consumers
or old_sound_rabbit_mq.multiple_consumers
.
Create a producer that extends Roslov\QueueBundle\Producer\BaseProducer
and implement getRoutingKey()
:
Add your producers to old_sound_rabbit_mq.producers
.
Create a producer facade to keep all producer calls in one place, by extending BaseProducerFacade
and injecting
EventProcessor
:
The events are stored in DB and are sent on kernel terminate or after message consuming. So you have to create a DB table for events. Currently, only Doctrine with MySQL are supported:
And add the Event
entity to the Doctrine config:
Now you can send an event by $producerFacade->sendUserCreatedEvent(123)
.
The best way to use the event processor is use in inside a transaction to comply the Transactional Outbox pattern. So you have to call the producer facade somewhere in your code, and then flush all events at the end of the transaction:
Do not forget to enable the event processor in roslov_queue.event_processor.enabled
if you produce messages.
Note that by default, the transactional outbox support is disabled. To enable it, set
roslov_queue.event_processor.instant_delivery
to false
.
In some microservices, you do not need to use transactional outbox, so events can be sent immediately. In this case, set
roslov_queue.event_processor.instant_delivery
to true
, so both BaseProducerFacade::send()
and
EventProcessor::save()
will be sending the events instantly (without interim saving to DB). This is a default
behavior.
For automation tests, you can disable roslov_queue.event_processor.delayed_delivery_subscriber
. In this case, the
events will be stored in DB but not sent. So you can test your DB whether events where created. Note that this will not
work if instant delivery is enabled — the events will be sent instantly.
RPC servers and clients
If you need to use the remote procedure call (RPC), enable roslov_queue.rpc_client.enabled
on your client service and
roslov_queue.rpc_server.enabled
and roslov_queue.rpc_server.exchange
on your server service:
The example of an RPC client use:
For an RPC server, add handlers that process commands and return results:
The example of an RPC server handler:
To run the RPC server, use:
Exception events
This bundle allows automatic sending of events about thrown exceptions.
Note that by default, the exception_subscriber
is disabled. To enable it, set
roslov_queue.exception_subscriber.enabled
to true
.
The exception subscriber uses the routing key exception-thrown
.
Example of exception validator class, that can be passed to the roslov_queue.exception_validator
configuration:
If you want to send an exception event manually, use
\Roslov\QueueBundle\Sender\ExceptionSender::sendExceptionThrownEvent()
.
Resending the message
In case something happened, and you need to resend the same message again to the same queue, use
return ConsumerInterface::MSG_SINGLE_NACK_REQUEUE;
instead of return ConsumerInterface::MSG_ACK;
in your consumer.
Testing
Unit testing
The package is tested with PHPUnit. To run tests:
Code style analysis
The code style is analyzed with PHP_CodeSniffer and PSR-12 Ext coding standard. To run code style analysis:
All versions of queue-bundle with dependencies
ext-json Version *
ext-mbstring Version *
ext-sockets Version *
doctrine/orm Version ^2.7
php-amqplib/rabbitmq-bundle Version ^1.15|^2.10
roslov/log-obfuscator Version ^1.0
symfony/config Version ^3.4|^4.0|^5.0|^6.0|^7.0
symfony/dependency-injection Version ^3.4|^4.0|^5.0|^6.0|^7.0
symfony/framework-bundle Version ^3.4|^4.0|^5.0|^6.0|^7.0
symfony/property-access Version ^3.4|^4.0|^5.0|^6.0|^7.0
symfony/serializer Version ^3.4|^4.0|^5.0|^6.0|^7.0