rezzza/command-bus-bundle

rezzza/command-bus 集成到 Symfony

安装次数: 35,459

依赖者: 0

建议者: 0

安全: 0

星标: 5

关注者: 8

分支: 9

开放问题: 1

类型:symfony-bundle

3.3.3 2017-07-07 08:42 UTC

This package is auto-updated.

Last update: 2024-09-08 06:23:01 UTC


README

CommandBus 库集成到 Symfony2。

安装

composer.json 文件中要求 rezzza/command-bus-bundle

{
    "require": {
        "rezzza/command-bus-bundle": "~2.0"
    }
}

app/AppKernel.php 中注册包

// app/AppKernel.php
public function registerBundles()
{
    return array(
        // ...
        new Rezzza\CommandBusBundle\RezzzaCommandBusBundle()
    );
}

更新 config.yml

framework:
    serializer:
        enabled: true

使用

$bus = $container->get('rezzza_command_bus.command_bus.synchronous'); // synchronous is the name you given to your bus in configuration.
$bus->handle(new FooCommand());

总线

目前支持 Direct, SncRedis 和 OldSoundRabbit。

命令

命令必须继承自 Rezzza\CommandBus\Domain\CommandInterface

注册命令处理器

<service id="...." class="....">
    <tag name="rezzza_command_bus.command_handler" command="FQCN\MyFirstActionCommand" /> <!-- when handle this command, method `myFirstAction` will be handled. -->
    <tag name="rezzza_command_bus.command_handler" command="FQCN\MySecondActionCommand" method="myMethod" />
</service>

事件

事件

- on_consumer_response
- pre_handle_command

注册监听器

<service id="service_id" class="Foo">
    <tag name="rezzza_command_bus.event_listener" event="pre_handle_command" method="onPreHandleCommand" />
    <tag name="rezzza_command_bus.event_listener" event="on_consumer_response" method="onConsumerResponse" />
</service>

失败策略

参见 command-bus 文档

控制台命令

此包提供 Symfony 控制台命令

$ app/console rezzza:command_bus:consume "CommandToConsume"

Options available:

 --consumer             Which consumer should we use ? (default: "default")
 --iteration-limit (-i) Limit of iterations, -1 for infinite.. (default: -1)
 --time-limit           During how many time this command will listen to the queue. (default: 60)
 --usleep               Micro seconds (default: 100000)
 --lock                 Only one command processing ?

为了监视消费者活动,您应该为 monolog 注册控制台处理器

monolog:
    handlers:
        console:
            type:   console
            verbosity_levels:
                VERBOSITY_NORMAL: NOTICE

配置

rezzza_command_bus:
    buses:
      synchronous: direct
      asynchronous:
        rabbitmq:
          #define producer_guesser which allow to determine rigth producer for each command
          #producer name and command class name must be indentical
            #example:
            #producer name : source_entry_update
            #command class name : SourceEntryUpdateCommand
          producer_guesser: rezzza_command_bus.old_sound_rabbit.producer_guesser
          consumer_bus: synchronous #consumer handle command with synchronous bus
        snc_redis:
            client: default # snc redis client.
            read_block_timeout: 1 # see blpop documentation
            consumers:
                default:
                    bus: synchronous
                    fail_strategy:
                        retry_then_fail: # When the command fail, it uses this strategy.
                        # you could use too requeue, none, service.
                            attempts: 10
                            requeue_on_fail: true
    handlers: # Do you want to use handlers provided in this bundle ?
        retry:  synchronous  # If you used retry_then_fail strategy, this handler is linked to Retry commands.
        failed: synchronous # If you used retry_then_fail strategy, this handler is linked to Failed commands.

与 JMS Serializer 一起使用

rezzza_command_bus:
    logger_normalizer: symfony_serializer

services:
    symfony_serializer:
        class: "Symfony\Component\Serializer\Serializer"