Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

A lib to consume message from any Broker

License

NotificationsYou must be signed in to change notification settings

swarrot/swarrot

Repository files navigation

Build StatusScrutinizer Quality ScoreLatest Stable VersionLatest Unstable Version

Swarrot is a PHP library to consume messages from any broker.

Installation

The recommended way to install Swarrot is throughComposer. Require theswarrot/swarrot package:

$ composer require swarrot/swarrot

Usage

Basic usage

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();

Using a stack

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:

this

Processors

Official processors

Create your own processor

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.

Deprecated processors & message providers / publishers

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.

Message providers / publishers

  • 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)

Processors

  • SentryProcessor (in 3.5.0)
  • RPC related processors (in 3.5.0)
  • NewRelicProcessor (in 3.7.0)

Inspiration

License

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

Stars

Watchers

Forks

Packages

No packages published

Contributors44


[8]ページ先頭

©2009-2025 Movatter.jp