magento2: Queue consumer handler can't be virtual type

Preconditions (*)

  1. Magento 2.3.3
  2. Rabbit MQ 3.7.8

Steps to reproduce (*)

  1. In custom module add consumer in queue_consumer.xml for any queue
  2. Consumer should be virtual type
  3. Start consumer from console

Expected result (*)

  1. Consumer handles message

Actual result (*)

  1. Consumer is not started
  2. There is error in console: "Service method specified as handler for of consumer “<name>” is not available. Given “<class>::<method>”

The problem sems to be in here vendor/magento/framework-message-queue/Consumer/Config/Validator/Handlers.php. MethodsMap tries to load class with reflection (vendor/magento/framework/Reflection/MethodsMap.php:130) and fails because virtual type in fact doesn’t exists.

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 2
  • Comments: 15 (6 by maintainers)

Most upvoted comments

⬆️ Looks like the issue is not resolved, is being reproduced, and still requires an attention

It’s still possible to reproduce this issue on Magento 2.4.0 Enterprise

How to reproduce

  1. Create some service class like this
namespace Vendor\Module\Model;

class Consumer implements CustomConsumerInterface
{
    private CustomValidatorInterface $validator;

    public function __construct(
        CustomValidatorInterface $validator
    ) {
        $this->validator = $validator;
    }

    public function consume(SomeCustomInterface $foo): void
    {
        $this->validator->validate($foo);
    }
}

  1. Create virtual type based on created class
<virtualType name="Vendor\Module\Model\VirtualConsumer" type="Vendor\Module\Model\Consumer">
        <arguments>
            <argument name="validator" xsi:type="object">Vendor\Module\Model\ConcreteValidator</argument>
        </arguments>
</virtualType>
  1. Use virtual consumer in queue_consumer.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework-message-queue:etc/consumer.xsd">
    <consumer name="testConsumer"
              queue="test.queu"
              connection="amqp"
              maxMessages="1"
              handler="Vendor\Module\Model\VirtualConsumer::consume" />
</config>

  1. Make sure setup:di:compile finished successfully
  2. Run bin/magento queue:consumers:start testConsumer and see the error
In Handlers.php line 74:
                                                                                                                                                                              
  Service method specified as handler for of consumer "testConsumer" is not available. Given "Vendor\Module\Model\VirtualConsumer::consume"

This happens because it tries to create ClassReflection for virtual type in vendor/magento/framework/Reflection/MethodsMap.php:137 And I see that code which is responsible for that is still exists in the latest magento 2.4-develop branch - https://github.com/magento/magento2/blob/2.4-develop/lib/internal/Magento/Framework/MessageQueue/Consumer/Config/Validator/Handlers.php#L72

@engcom-Hotel I didn’t have time recently to check it again. I was waiting for your response for over one year, so I don’t think this should be closed just like this. I am going to examine that in two next week, so you can reopen this one.