- Notifications
You must be signed in to change notification settings - Fork55
A lib to consume message from any Broker
License
swarrot/swarrot
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
Swarrot is a PHP library to consume messages from any broker.
The recommended way to install Swarrot is throughComposer. Require theswarrot/swarrot
package:
$ composer require swarrot/swarrot
First, you need to create a message provider to retrieve messages from yourbroker. For example, with aPeclPackageMessageProvider
(retrieves messages froman AMQP broker with thepecl amqp package:
useSwarrot\Broker\MessageProvider\PeclPackageMessageProvider;// Create connection$connection =new \AMQPConnection();$connection->connect();$channel =new \AMQPChannel($connection);// Get the queue to consume$queue =new \AMQPQueue($channel);$queue->setName('global');$messageProvider =newPeclPackageMessageProvider($queue);
Once it's done you need to create aProcessor
to process messages retrievedfrom the broker. This processor must implementSwarrot\Processor\ProcessorInterface
. For example:
useSwarrot\Processor\ProcessorInterface;useSwarrot\Broker\Message;class Processorimplements ProcessorInterface{publicfunctionprocess(Message$message,array$options):bool {echosprintf("Consume message #%d\n",$message->getId());returntrue;// Continue processing other messages }}
You now have aSwarrot\Broker\MessageProviderInterface
to retrieve messagesand a Processor to process them. So, ask theSwarrot\Consumer
to do its job :
useSwarrot\Consumer;$consumer =newConsumer($messageProvider,$processor);$consumer->consume();
Heavily inspired bystackphp/builder youcan useSwarrot\Processor\Stack\Builder
to stack your processors.Using thebuilt in processors or bycreating yourown, you can extend the behavior of yourbase processor.In this example, your processor is decorated by 2 other processors. TheExceptionCatcherProcessorwhich decorates your own with a try/catch block and theMaxMessagesProcessorwhich stops your worker when some messages have been consumed.
useSwarrot\Processor\ProcessorInterface;useSwarrot\Broker\Message;class Processorimplements ProcessorInterface{publicfunctionprocess(Message$message,array$options):bool {echosprintf("Consume message #%d\n",$message->getId());returntrue;// Continue processing other messages }}$stack = (new \Swarrot\Processor\Stack\Builder()) ->push('Swarrot\Processor\MaxMessages\MaxMessagesProcessor',newLogger()) ->push('Swarrot\Processor\ExceptionCatcher\ExceptionCatcherProcessor') ->push('Swarrot\Processor\Ack\AckProcessor',$messageProvider);$processor =$stack->resolve(newProcessor());
Here is an illustration to show you what happens when this order is used:
- AckProcessor
- Doctrine related processors (thanks toAdrien Brault)
- ExceptionCatcherProcessor
- InsomniacProcessor (thanks toAdrien Brault)
- InstantRetryProcessor
- MaxExecutionTimeProcessor (thanks toRemy Lemeunier)
- MaxMessagesProcessor (thanks toRemy Lemeunier)
- MemoryLimitProcessor (thanks toChristophe Coevoet)
- RetryProcessor
- ServicesResetterProcessor (thanks toPierrick Vignand)
- SignalHandlerProcessor
- XDeathMaxCountProcessor (thanks toAnthony Moutte)
- XDeathMaxLifetimeProcessor (thanks toAnthony Moutte)
To create your own processor and be able to use it with theStackProcessor
, youjust need to implementProcessorInterface
and to take anotherProcessorInterface
as first argument in constructor.
In order to reduceswarrot/swarrot
dependencies & ease the maintenance, someprocessors & message providers / publishers have been deprecated in 3.x version.They will be deleted in 4.0.
If you use those deprecated classes you could create your own repository tokeep them or we could create a dedicated repository under the swarrotorganisation if you're willing to help to maintain them.
- SQS Message provider (in 3.5.0)
- Stomp message providers (in 3.6.0)
- Stomp message publishers (in 3.7.0)
- Interop message publishers & providers (in 3.7.0)
- SentryProcessor (in 3.5.0)
- RPC related processors (in 3.5.0)
- NewRelicProcessor (in 3.7.0)
Swarrot is released under the MIT License. See the bundled LICENSE file for details.
About
A lib to consume message from any Broker
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.