Skip to content

Commit 4e2f356

Browse files
authored
Merge pull request #12 from mtrudu/master
Add RabbitMq Command bus
2 parents 4dd289e + 6f6ed37 commit 4e2f356

6 files changed

Lines changed: 98 additions & 1 deletion

File tree

DependencyInjection/Configuration.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,12 @@ public function getConfigTreeBuilder()
5151
->append($this->createConsumersNodeDefinition())
5252
->end()
5353
->end()
54+
->arrayNode('rabbitmq')
55+
->children()
56+
->scalarNode('producer_guesser')->defaultValue('rezzza_command_bus.old_sound_rabbit.producer_guesser')->end()
57+
->scalarNode('consumer_bus')->defaultNull()->end()
58+
->end()
59+
->end()
5460
->arrayNode('service')
5561
->children()
5662
->scalarNode('id')->isRequired()->end()

DependencyInjection/RezzzaCommandBusExtension.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ private function createBus($name, array $config, ContainerBuilder $container, $l
5353
$this->createSncRedisBusCommandBus($commandBusServiceName, $config, $container);
5454
$this->decorateBus($commandBusServiceName, $container, $loggerNormalizer);
5555
break;
56+
case 'rabbitmq':
57+
$this->createRabbitMqBusCommandBus($commandBusServiceName, $config, $container);
58+
$this->decorateBus($commandBusServiceName, $container, $loggerNormalizer);
59+
break;
5660
case 'service':
5761
$container->setAlias($commandBusServiceName, $config['id']);
5862
break;
@@ -62,6 +66,25 @@ private function createBus($name, array $config, ContainerBuilder $container, $l
6266
}
6367
}
6468

69+
private function createRabbitMqBusCommandBus($commandBusServiceName, $config, ContainerBuilder $container)
70+
{
71+
$service = new Definition('%rezzza_command_bus.old_sound_rabbit_bus.class%', [
72+
new Reference($config['producer_guesser']),
73+
]);
74+
75+
$container->setDefinition($commandBusServiceName, $service);
76+
77+
if ($config['consumer_bus'] !== null) {
78+
$consumerDefinition = new Definition('%rezzza_command_bus.old_sound_rabbit_bus.consumer.class%',
79+
[
80+
new Reference($this->getCommandBusServiceName($config['consumer_bus'])),
81+
]
82+
);
83+
84+
$container->setDefinition(sprintf('rezzza_command_bus.old_sound_rabbit_bus.consumer.%s', $config['consumer_bus']), $consumerDefinition);
85+
}
86+
}
87+
6588
private function createSncRedisBusCommandBus($commandBusServiceName, $config, ContainerBuilder $container)
6689
{
6790
$client = new Reference(sprintf('snc_redis.%s_client', $config['client']));
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace Rezzza\CommandBusBundle\Provider\OldSoundRabbit;
4+
5+
use OldSound\RabbitMqBundle\RabbitMq\ConsumerInterface;
6+
use PhpAmqpLib\Message\AMQPMessage;
7+
use Rezzza\CommandBus\Domain\CommandBusInterface;
8+
9+
/**
10+
* Class DefaultConsumer
11+
*/
12+
class DefaultConsumer implements ConsumerInterface
13+
{
14+
protected $bus;
15+
16+
/**
17+
* Constructor.
18+
*
19+
* @param CommandBusInterface $bus
20+
*/
21+
public function __construct(CommandBusInterface $bus)
22+
{
23+
$this->bus = $bus;
24+
}
25+
26+
/**
27+
* {@inheritdoc}
28+
*/
29+
public function execute(AMQPMessage $command)
30+
{
31+
$this->bus->handle(unserialize($command->body));
32+
}
33+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace Rezzza\CommandBusBundle\Provider\OldSoundRabbit;
4+
5+
use Rezzza\CommandBus\Domain\CommandInterface;
6+
7+
/**
8+
* Class NoProducerFoundException
9+
*/
10+
class NoProducerFoundException extends \LogicException
11+
{
12+
public function __construct(CommandInterface $command)
13+
{
14+
$message = sprintf('Producer not found for Command [%s]', get_class($command));
15+
16+
parent::__construct($message);
17+
}
18+
}

README.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ $bus->handle(new FooCommand());
5151
Bus
5252
---
5353

54-
Direct & SncRedis are currently supported.
54+
Direct, SncRedis & OldSoundRabbit are currently supported.
5555

5656
Commands
5757
--------
@@ -126,6 +126,14 @@ rezzza_command_bus:
126126
buses:
127127
synchronous: direct
128128
asynchronous:
129+
old_rabbitmq:
130+
#define producer_guesser which allow to determine rigth producer for each command
131+
#producer name and command class name must be indentical
132+
#example:
133+
#producer name : source_entry_update
134+
#command class name : SourceEntryUpdateCommand
135+
producer_guesser: rezzza_command_bus.old_sound_rabbit.producer_guesser
136+
consumer_bus: synchronous #consumer handle command with synchronous bus
129137
snc_redis:
130138
client: default # snc redis client.
131139
read_block_timeout: 1 # see blpop documentation

Resources/config/services/services.xml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@
77
<parameters>
88
<parameter key="rezzza_command_bus.direct_bus.class">Rezzza\CommandBus\Infra\Provider\Direct\DirectBus</parameter>
99
<parameter key="rezzza_command_bus.snc_redis_bus.class">Rezzza\CommandBusBundle\Provider\SncRedis\SncRedisBus</parameter>
10+
11+
<parameter key="rezzza_command_bus.old_sound_rabbit_bus.class">Rezzza\CommandBus\Infra\Provider\OldSoundRabbit\RabbitMqBus</parameter>
12+
<parameter key="rezzza_command_bus.old_sound_rabbit_bus.consumer.class">Rezzza\CommandBusBundle\Provider\OldSoundRabbit\DefaultConsumer</parameter>
13+
<parameter key="rezzza_command_bus.old_sound_rabbit.producer_guesser.class">Rezzza\CommandBus\Infra\Provider\OldSoundRabbit\ProducerGuesser</parameter>
14+
1015
<parameter key="rezzza_command_bus.event_dispatcher_bus.class">Rezzza\CommandBus\Domain\EventDispatcherBus</parameter>
1116
<parameter key="rezzza_command_bus.logger_bus.class">Rezzza\CommandBus\Domain\LoggerBus</parameter>
1217

@@ -32,6 +37,10 @@
3237
</parameters>
3338

3439
<services>
40+
<service id="rezzza_command_bus.old_sound_rabbit.producer_guesser" class="%rezzza_command_bus.old_sound_rabbit.producer_guesser.class%" public="false">
41+
<argument type="service" id="service_container" />
42+
</service>
43+
3544
<service id="rezzza_command_bus.command_handler_locator.container" class="%rezzza_command_bus.command_handler_locator.container.class%" public="false">
3645
<argument type="service" id="service_container" />
3746
</service>

0 commit comments

Comments
 (0)