Skip to content

Commit

Permalink
support for custom event publishing strategy
Browse files Browse the repository at this point in the history
  • Loading branch information
prgTW committed Oct 20, 2016
1 parent 41cb103 commit da995ef
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 17 deletions.
16 changes: 13 additions & 3 deletions doc/getting_started.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,22 @@ This will log consumed messages to the `asynchronous_command_bus` and `asynchron

## Choose event strategy

When handling events you have two strategies to choose from. Either you publish *all* events to the message queue or
you only publish the events that have a registered subscriber. If your application is the only one that consuming messages
you should consider using the **predefined** strategy. This will reduce the message overhead on the message queue.
When handling events you have two predefined strategies to choose from. Either you publish *all* events to the message
queue (*always* strategy) or you only publish the events that have a registered asynchronous subscriber
(*predefined* strategy). If your application is the only one that is consuming messages you should consider using the
**predefined** strategy. This will reduce the message overhead on the message queue.

```yaml
simple_bus_asynchronous:
events:
strategy: 'predefined' # default: 'always'
```

You can also use Your own strategy by defining custom **strategy_service_id**

```yaml
simple_bus_asynchronous:
events:
strategy:
strategy_service_id: your_strategy_service
```
23 changes: 20 additions & 3 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,27 @@ public function getConfigTreeBuilder()
->arrayNode('events')
->canBeEnabled()
->children()
->enumNode('strategy')
->arrayNode('strategy')
->addDefaultsIfNotSet()
->beforeNormalization()
->ifInArray(['always', 'predefined'])
->then(function ($v) {
$map = [
'always' => 'simple_bus.asynchronous.always_publishes_messages_middleware',
'predefined' => 'simple_bus.asynchronous.publishes_predefined_messages_middleware',
];

return [
'strategy_service_id' => $map[$v]
];
})
->end()
->info('What strategy to use to publish messages')
->defaultValue('always')
->values(['always', 'predefined'])
->children()
->scalarNode('strategy_service_id')
->defaultValue('simple_bus.asynchronous.always_publishes_messages_middleware')
->end()
->end()
->end()
->scalarNode('publisher_service_id')
->info('Service id of an instance of Publisher')
Expand Down
7 changes: 1 addition & 6 deletions src/DependencyInjection/SimpleBusAsynchronousExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,7 @@ private function loadAsynchronousEventBus(array $config, ContainerBuilder $conta
$loader->load('asynchronous_events_logging.yml');
}


if ($config['strategy'] === 'always') {
$eventMiddleware = 'simple_bus.asynchronous.always_publishes_messages_middleware';
} else {
$eventMiddleware = 'simple_bus.asynchronous.publishes_predefined_messages_middleware';
}
$eventMiddleware = $config['strategy']['strategy_service_id'];

// insert before the middleware that actually notifies a message subscriber of the message
$container->getDefinition($eventMiddleware)->addTag('event_bus_middleware', ['priority' => 0]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

use Matthias\SymfonyDependencyInjectionTest\PhpUnit\AbstractExtensionTestCase;
use SimpleBus\AsynchronousBundle\DependencyInjection\SimpleBusAsynchronousExtension;
use Symfony\Component\DependencyInjection\ContainerBuilder;

class SimpleBusAsynchronousExtensionTest extends AbstractExtensionTestCase
{
Expand All @@ -25,7 +24,7 @@ protected function getMinimalConfiguration()
/**
* @test
*/
public function it_uses_strategy_allways_by_default()
public function it_uses_strategy_always_by_default()
{
$this->container->setParameter('kernel.bundles', ['SimpleBusCommandBusBundle'=>true, 'SimpleBusEventBusBundle'=>true]);
$this->load();
Expand All @@ -43,7 +42,18 @@ public function it_uses_strategy_predefined_when_configured()

$this->assertContainerBuilderHasServiceDefinitionWithTag('simple_bus.asynchronous.publishes_predefined_messages_middleware', 'event_bus_middleware', ['priority'=>0]);
}


/**
* @test
* @expectedException \Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException
* @expectedExceptionMessageRegExp ".*custom_strategy.*"
*/
public function it_uses_custom_strategy_when_configured()
{
$this->container->setParameter('kernel.bundles', ['SimpleBusCommandBusBundle'=>true, 'SimpleBusEventBusBundle'=>true]);
$this->load(['events'=>['strategy'=>['strategy_service_id'=>'custom_strategy']]]);
}

/**
* @test
* @expectedException \LogicException
Expand All @@ -54,7 +64,7 @@ public function it_throws_exception_if_command_bus_bundle_is_missing()
$this->container->setParameter('kernel.bundles', ['SimpleBusEventBusBundle'=>true]);
$this->load(['events'=>['strategy'=>'predefined']]);
}

/**
* @test
* @expectedException \LogicException
Expand All @@ -65,4 +75,4 @@ public function it_throws_exception_if_event_bus_bundle_is_missing()
$this->container->setParameter('kernel.bundles', ['SimpleBusCommandBusBundle'=>true]);
$this->load(['events'=>['strategy'=>'predefined']]);
}
}
}

0 comments on commit da995ef

Please sign in to comment.