Download the PHP package erkens/bunny-bundle without Composer
On this page you can find all versions of the php package erkens/bunny-bundle. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package bunny-bundle
Skrz\Bundle\BunnyBundle
Produce and consumer type-safe messages from RabbitMQ queues
Installation
Add as Composer dependency:
Then add BunnyBundle
to Symfony Kernel:
Usage
BunnyBundle
connects Skrz\Meta
and Skrz\Bundle\AutowiringBundle
, so that you can produce and consume type-safe
messages to/from RabbitMQ.
BunnyBundle
creates new 2 new stereotypes (see AutowiringBundle
's description):
@Consumer
- consumer starts listening for messages on given queue/exchange. Whenever message arrives,handleMessage
method is called.@Producer
- producers must inherit fromSkrz\Bundle\BunnyBundle\AbstractProducer
. They publish type-safe messages to specified exchanges.
When BunnyBundle
is added to the Symfony kernel, it registers 3 commands:
bunny:setup
- creates exchanges, queues and bindings between them according to configuration.bunny:consumer
- starts given consumer.bunny:producer
- utility command that takes JSON-serialized message, routing key and sends it using given producer. Useful for debugging.
Setup in services.yml
BunnyBundle
uses bunny
container extension key.
After you have configured all exchanges, queues and bindings between them, run bunny:setup
:
Broker entities should be created as configured.
Note that bunny:setup
does not try to resolve any conflicting declarations, e.g. one time you declare queue as durable
and the seconds time as not durable, you have to resolve these yourself.
Writing producers
Our example will be async processing of changes in data. Suppose you have products an categories and want to automatically
categorize products according to product title and category title. However, the categorization algorithm is quite expensive,
so it has to be done async. We will publish any change in product or category to change
exchange.
Start with data model:
Producer - ChangeProducer
- will publish changes to change
exchange. Producers have beforeMethod
setting - a method
on producer that is called before message is serialized and sent to broker. We will pre-process message and
set $hostname
and $userId
.
meta
points to *Meta
class, that will be used to serialized messages.
You can test producer from command line:
Writing consumers
When writing a consumer using BunnyBundle
, think of following: consumers can fail - should messages of a failed consumer
be redelivered? If so, you should create queue in services.yml
and consume from given queue. If not, you should specify
exchange
in @Consumer
annotation - an anonymous queue will be created on consumer startup.
We want messages to be redelivered, so product_categorize
queue has been created, consumer will consumer from it.
maxMessages
&maxSeconds
- you should always run your consumer under some supervisor, e.g. supervisord. PHP can leak memory, after specified number of messages processed / seconds running, consumer will do clean shutdown (flush all messages, disconnect from RabbitMQ) and exit with code0
- supervisor should automatically restart it.prefetchCount
- if you have more consumer processes consuming from the same queue in parallel, setprefetchCount=1
to evenly distribute the work between consumers
Known limitations
- If processing of a messages takes longer then heartbeat timeout, RabbitMQ will disconnect client a consumer will crash. It is more of a limitation of PHP (no threads). Heartbeat has to be set high enough.
License
The MIT license. See LICENSE
file.
All versions of bunny-bundle with dependencies
bunny/bunny Version >=0.3
skrz/autowiring-bundle Version ~1.1|~2.0
skrz/meta Version >=3.0, <4.0