-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #99 from codeliner/5.0_docs
Bookdown docs
- Loading branch information
Showing
6 changed files
with
126 additions
and
122 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,61 @@ | ||
Async Message Producer | ||
====================== | ||
|
||
[Back to documentation](../README.md#documentation) | ||
|
||
# Async Messaging | ||
|
||
Messaging becomes really interesting when you process your messages asynchronous. For example push your messages on a database queue, | ||
set up a cron job to periodically check the queue for new messages and process them. The bus implementations of PSB can | ||
hide such an asynchronous workflow behind a unified interface. You can start with synchronous message dispatching by | ||
routing your messages directly to message handlers and if you later want to improve response times you can switch to | ||
async processing on a per message basis by routing the appropriate messages to a [Async\MessageProducer](../src/Async/MessageProducer.php) listed below. | ||
|
||
# Available MessageProducer | ||
|
||
- [BernardProducer](https://github.com/prooph/psb-bernard-producer): Queue multi-backend providing different | ||
drivers like Doctrine DBAL and Predis (see http://bernardphp.com for a complete list of drivers) | ||
- [GuzzleHttpProducer](https://github.com/prooph/psb-http-producer): Send messages to a remote system using | ||
HTTP | ||
- [ZeromqProducer](https://github.com/prooph/psb-zeromq-producer): Async message handling using super fast and simple to | ||
set up ZeroMQ | ||
|
||
# Usage | ||
|
||
If you want to set up a bus that handles all messages async you can do so by attaching a [MessageProducerPlugin](plugins.md#messageproducerplugin) | ||
initialized with your message producer of choice to a message bus. | ||
|
||
If you want to decide on a per message basis if the message should be handled async you can use a normal [message router](plugins.md#routers) | ||
and configure your message producer of choice as message handler for the appropriate messages. | ||
|
||
*Note: The [RegexRouter](plugins.md#proophservicebuspluginrouterregexrouter) is a good choice if you want to handle all messages of a specific namespace async.* | ||
|
||
# QueryBus | ||
|
||
A async message producer for the QueryBus needs to provide a response by resolving the handed over deferred. | ||
In a messaging system based on ZeroMQ for example you can make use of request/response mode. | ||
HTTP APIs provide responses naturally. | ||
So these are both good candidates to use for remote querying. | ||
# Async Message Producer | ||
|
||
Messaging becomes really interesting if you process your messages asynchronous. PSB can | ||
hide such an asynchronous workflow behind a unified interface. You can start with synchronous message dispatching by | ||
routing your messages directly to message handlers and if you later want to improve response times you can switch to | ||
async processing on a per message basis by routing the appropriate messages to a message producer which hands them over to | ||
a messaging system like rabbitMQ, zeroMQ, gearman or beanstalkd. | ||
|
||
## Available MessageProducer | ||
|
||
- [BernardProducer](https://github.com/prooph/psb-bernard-producer): Queue multi-backend providing different | ||
drivers like Doctrine DBAL and Predis (see [https://github.com/bernardphp/bernard](https://github.com/bernardphp/bernard) for a complete list of drivers) | ||
- [GuzzleHttpProducer](https://github.com/prooph/psb-http-producer): Send messages to a remote system using | ||
HTTP | ||
- [ZeromqProducer](https://github.com/prooph/psb-zeromq-producer): Async message handling using super fast and simple to | ||
set up ZeroMQ | ||
|
||
## Usage | ||
|
||
If you want to set up a bus that handles all messages async you can do so by attaching a `Prooph\ServiceBus\Plugin\MessageProducerPlugin` | ||
initialized with your message producer of choice to a message bus. | ||
|
||
Let's look at a simple example using the `psb-zeromq-producer` | ||
|
||
```php | ||
//app bootstrap | ||
$container = new Container; | ||
$container['config'] = [ | ||
'prooph' => [ | ||
'zeromq_producer' => [ | ||
'dsn' => 'tcp://127.0.0.1:5555', // ZMQ Server Address. | ||
'persistent_id' => 'example', // ZMQ Persistent ID to keep connections alive between requests. | ||
'rpc' => false, // Use as Query Bus. | ||
] | ||
] | ||
]; | ||
|
||
$factory = \Prooph\ServiceBus\Message\ZeroMQ\Container\ZeroMQMessageProducerFactory; | ||
$zmqProducer = $factory($container); | ||
|
||
$commandBus = new \Prooph\ServiceBus\CommandBus(); | ||
|
||
$messageProducerForwarder = new \Prooph\ServiceBus\Plugin\MessageProducerPlugin($zmqProducer); | ||
|
||
$commandBus->utilize($messageProducerForwarder); | ||
|
||
$echoText = new ExampleCommand('It works'); | ||
$commandBus->dispatch($echoText); | ||
``` | ||
|
||
You can also route individual messages to message producer by using a message router plugin. | ||
|
||
*Note: `Prooph\ServiceBus\Plugin\Router\RegexRouter` is a good choice | ||
if you want to handle all messages of a specific namespace async.* | ||
|
||
## Async Querying | ||
|
||
An async message producer for the QueryBus needs to provide a response by resolving the handed over `React\Promise\Deferred`. | ||
When using a messaging system like ZeroMQ for example you can make use of request/response mode or RPC mode. | ||
HTTP APIs provide responses naturally. | ||
So these are both good candidates to use for remote querying. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.