No configured handlers in Symfony Messenger

I’d like to implement Messenger async handlers and be able to queue some tasks in Redis, but I can’t for some reason.

Here is my global config :

  • PHP 7.3.16
  • Symfony 4.4.7
  • Messenger 4.4
  • Redis 5.0.3
  • Predis 1.1

I tried to follow this guide :

https://symfony.com/doc/4.4/messenger.html

Everything is a copy/paste from the doc, except that I replaced my controller with a command.

This dispatch command seems to work :

php bin/console app:dispatch-command

^ SymfonyComponentMessengerEnvelope^ {#5465 -stamps: [] -message: AppMessageSmsNotification^ {#5475
-content: “Look! I created a message!” } }

This command returns no configured handlers :

php bin/console debug:messenger

Messenger

=========

This second command returns an error while trying to consume a message

php bin/console messenger:consume async

TypeError {#174
      #message: "The first argument must be an instance of "SymfonyComponentMessengerRoutableMessageBus"."
      #code: 0
      #file: "./vendor/symfony/messenger/Command/ConsumeMessagesCommand.php"
      #line: 54
      trace: {
        ./vendor/symfony/messenger/Command/ConsumeMessagesCommand.php:54 { …}
        ./var/cache/dev/ContainerM8fc2IB/srcApp_KernelDevDebugContainer.php:3736 {
          ContainerM8fc2IBsrcApp_KernelDevDebugContainer->getConsole_Command_MessengerConsumeMessagesService()^
          › 
          › $this->privates['console.command.messenger_consume_messages'] = $instance = new SymfonyComponentMessengerCommandConsumeMessagesCommand('', ($this->privates['messenger.receiver_locator'] ?? ($this->privates['messenger.receiver_locator'] = new SymfonyComponentDependencyInjectionArgumentServiceLocator($this->getService, [], []))), ($this->services['event_dispatcher'] ?? $this->getEventDispatcherService()), ($this->privates['monolog.logger.messenger'] ?? $this->getMonolog_Logger_MessengerService()), []);
          › 
          arguments: {
            $routableBus: ""
            $receiverLocator: SymfonyComponentDependencyInjectionArgumentServiceLocator {#178 …}
            $eventDispatcher: SymfonyComponentHttpKernelDebugTraceableEventDispatcher {#188 …}
            $logger: SymfonyBridgeMonologLogger {#179 …}
            $receiverNames: []
          }
        }
        ./vendor/symfony/dependency-injection/Container.php:450 { …}
        ./vendor/symfony/dependency-injection/Argument/ServiceLocator.php:40 { …}
        ./vendor/symfony/console/CommandLoader/ContainerCommandLoader.php:45 { …}
        ./vendor/symfony/console/Application.php:541 { …}
        ./vendor/symfony/console/Application.php:634 { …}
        ./vendor/symfony/framework-bundle/Console/Application.php:117 { …}
        ./vendor/symfony/console/Application.php:235 { …}
        ./vendor/symfony/framework-bundle/Console/Application.php:83 { …}
        ./vendor/symfony/console/Application.php:147 { …}
        ./bin/console:42 { …}
      }
    }
    2020-04-20T20:08:02+02:00 [critical] Uncaught Error: The first argument must be an instance of "SymfonyComponentMessengerRoutableMessageBus".

Here are some relevant files…

# .env
MESSENGER_TRANSPORT_DSN=redis://127.0.0.1:6379/messages/?auto_setup=true&serializer=1&stream_max_entries=0&dbindex=0
# config/packages/messenger.yaml
framework:
    messenger:
        transports:
            async: '%env(MESSENGER_TRANSPORT_DSN)%'
        routing:
            'AppMessageSmsNotification': async
# config/services.yaml
parameters:

services:
    _defaults:
        autowire: true
        autoconfigure: true

    App:
        resource: '../src/*'
        exclude: '../src/{DependencyInjection,Entity,Migrations,Tests,Kernel.php}'

    AppController:
        resource: '../src/Controller'
        tags: ['controller.service_arguments']
<?php
// src/Message/SmsNotification.php

namespace AppMessage;

class SmsNotification
{
    private $content;

    public function __construct(string $content)
    {
        $this->content = $content;
    }

    public function getContent(): string
    {
        return $this->content;
    }
}
<?php
// src/MessageHandler/SmsNotificationHandler.php

namespace AppMessageHandler;

use AppMessageSmsNotification;
use SymfonyComponentMessengerHandlerMessageHandlerInterface;

class SmsNotificationHandler implements MessageHandlerInterface
{
    public function __invoke(SmsNotification $message)
    {
        dump('ok!');
        echo('handler');
    }
}
<?php
// src/Command/DispatchCommand

namespace AppCommand;

use SymfonyComponentConsoleCommandCommand;
use SymfonyComponentConsoleInputInputArgument;
use SymfonyComponentConsoleInputInputInterface;
use SymfonyComponentConsoleInputInputOption;
use SymfonyComponentConsoleOutputOutputInterface;
use SymfonyComponentConsoleStyleSymfonyStyle;
use AppMessageSmsNotification;
use SymfonyComponentMessengerMessageBusInterface;

class DispatchCommand extends Command
{
    protected $bus;
    protected static $defaultName = 'app:dispatch-command';

    public function __construct(MessageBusInterface $bus)
    {
        $this->bus = $bus;
    }

    protected function configure()
    {
        $this
            ->setDescription('Dispatch test command')
            ->setHelp('Dispatch test command');
    }

    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $dispatch = $this->bus->dispatch(new SmsNotification('Look! I created a message!'));
        dump($dispatch);
    }

Can someone please help me?

Cheers!

Source: Symfony Questions

Was this helpful?

0 / 0

Leave a Reply 0

Your email address will not be published. Required fields are marked *