没有消息“App\Message\Event\EventMessage”的处理程序。 (两个独立的消息总线)

问题描述 投票:0回答:1

github 仓库: https://github.com/00if23/symfony-messenger-testcase

就跑吧

bin/console testcase

我需要两个独立的消息总线才能将其与不同(不兼容)的中间件集一起使用。 我有两套消息、中间件和处理程序类:

CommandMessage -> CommandMessageMiddlware -> CommandMessageHandler
EventMessage -> EventMessageMiddlware -> EventMessageHandler

我做

config/packages/messenger.yaml

framework:
    messenger:
        default_bus: command.bus
        buses:
            event.bus:
                middleware:
                    - App\Middleware\EventMessageMiddlware
            command.bus:
                middleware:
                    - App\Middleware\CommandMessageMiddlware

并添加到

config/services.yaml


services
    ...
    ...
    App\Message\:
        resource: '../src/Message'
        autoconfigure: false

    App\MessageHandler\:
        resource: '../src/MessageHandler'
        autoconfigure: false

    App\MessageHandler\Event\EventMessageHandler:
        autoconfigure: false
        tags: 
            -   name: messenger.message_handler
                bus: event.bus
                handles: App\Message\Event\EventMessage


    App\MessageHandler\Command\CommandMessageHandler:
        autoconfigure: false
        tags: 
            -   name: messenger.message_handler
                bus: command.bus
                handles: App\Message\Command\CommandMessage

结果是:

        // works as expected with CommandMessageMiddlware
        // on default "command.bus"
        $this->bus->dispatch(new CommandMessage()); 

        // BUT! There I have 'No handler for message "App\Message\Event\EventMessage"' 
        $this->bus->dispatch(new EventMessage());

每次我在非默认消息总线上分派任何事件时,都会出现错误“No handler for message XXX

这是消息发送器:调试命令:

Messenger
=========

command.bus
-----------

 The following messages can be dispatched:

 ------------------------------------------------------------------------------------------------------ 
  Messages for COMMAND bus                                                                              
  App\Message\Command\CommandMessage                                                      
      handled by App\MessageHandler\Command\CommandMessageHandler (when bus=command.bus)                                                                                             
 ------------------------------------------------------------------------------------------------------ 

event.bus
---------

 The following messages can be dispatched:

 ---------------------------------------------------------------------------------------------------- 
  Messages for EVENT bus                                                                             
  App\Message\Event\EventMessage                                                    
      handled by App\MessageHandler\Event\EventMessageHandler (when bus=event.bus)  
 ---------------------------------------------------------------------------------------------------- 
php symfony message-bus
1个回答
0
投票

您在命令中注入的

MessageBusInterface
是应用程序的默认消息总线。在您的情况下,它是
command.bus
,这就是在您的示例中调度
CommandMessage
起作用的原因。

EventMessage
必须通过事件消息总线调度。你必须注入相应的
MessageBusInterface
,并用它来调度。

有多种方法可以注入不同的接口。 Symfony 可以方便地将您的总线绑定到变量名称。如果您注入

MessageBusInterface $eventBus
,您将自动获得事件总线服务。
MessageBusInterface $commandBus
MessageBusInterface $bus
(因为命令总线是默认的)将为您提供命令总线。您可以查看
bin/console debug:container
了解更多详情。

我这样更新了你的命令:

#[AsCommand(
    name: 'testcase',
)]
class TestCaseCommand extends Command
{
    public function __construct(
        private readonly MessageBusInterface $commandBus,
        private readonly MessageBusInterface $eventBus
    )
    {
        parent::__construct();
    }

    protected function execute(InputInterface $input, OutputInterface $output): int
    {
        $io = new SymfonyStyle($input, $output);

        $this->commandBus->dispatch(new CommandMessage());
        $this->eventBus->dispatch(new EventMessage());

        $io->success('Done');

        return Command::SUCCESS;
    }
}

并得到以下输出:

➜  symfony-messenger-testcase git:(main) ✗ bin/console testcase                           
App\Middleware\Command\CommandMiddleware in effect
App\MessageHandler\Command\CommandMessageHandler in effect
App\Middleware\Event\EventMiddleware in effect
App\MessageHandler\Event\EventMessageHandler in effect

                                                                                                                        
 [OK] Done                                                                                                                                                                                                                                      
© www.soinside.com 2019 - 2024. All rights reserved.