psamatt/service-bus-lite-bundle

Symfony2中的ServiceBus组件实现

安装量: 4,370

依赖者: 0

建议者: 0

安全: 0

星标: 4

关注者: 1

分支: 0

开放问题: 0

类型:symfony-bundle

1.0 2014-01-27 14:32 UTC

This package is not auto-updated.

Last update: 2024-09-24 02:06:16 UTC


README

通过一个包将Service Bus集成到Symfony2中。

使用Composer安装

您可以通过在composer.json文件的require部分添加以下内容来安装此包

    "require": {
        ...
        "psamatt/service-bus-lite-bundle": "*"
    },

然后,在AppKernel中启用该包

<?php
// app/AppKernel.php

public function registerBundles()
{
    $bundles = array(
        // ...
        new Psamatt\ServiceBusLiteBundle\PsamattServiceBusLiteBundle(),
    );
}

如何使用

创建实现ServiceBus\IQueryHandlerServiceBus\ICommandHandler的Query或Command处理器,例如

use \ServiceBus\ICommand;

class FooCommandHandler implements \ServiceBus\ICommandHandler
{
    function handle(ICommand $command)
    {
        
    }
}

然后将处理器注册为服务,并在services.yml中将其标记为ServiceBus处理器

services:
    # Command Handler
    foo.command.handler:
        class: Acme\HelloBundle\CommandHandler\FooCommandHandler
        tags:
            -  { name: servicebus.command_handler }

    # Query Handler 
    foo.query.handler:
        class: Acme\HelloBundle\QueryHandler\FooQueryHandler
        tags:
            -  { name: servicebus.query_handler }

将此服务标记为命令或查询处理器将允许服务总线将此类注册为即将到来的命令的等待处理器。

现在您只需在Controller中初始化一个FooCommand并将其发送到ServiceBus

class FooController extends Controller
{
    
    public function indexAction()
    {
        // we send Commands
        $this->get('ServiceBus.Mediator')->send(new FooCommand('myEventName'));
        
        // we request Queries
        $response = $this->get('ServiceBus.Mediator')->request(new FooQuery('myEventName'));
    }

在后台,ServiceBus将找到相应的处理器,您将在该处理器中编写特定操作所需的逻辑。

更详细的代码示例可以在主仓库的示例文件夹中找到。

注意:为了ServiceBus能够找到相关的CommandHandler,您的CommandCommandHandler类名称必须以Command和CommandHandler结尾。