Skip to content
Advertisement

Magento 2 Message Queue – Type Error with expected string, null given

I struggle for 3 days now to get a simple Message Queue in Magento 2 to work. Here are my XML files:

communication.xml:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:Communication/etc/communication.xsd">
    <topic name="erp.queue.order"
           request="string">
        <handler name="erp.queue.order"
                 type="TimoGOrderTransferModelQueueConsumer"
                 method="process" />
    </topic>
</config>

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="erp.queue.order"
              queue="erp.queue.order"
              connection="db"
              handler="TimoGOrderTransferModelQueueConsumer::process"/>
</config>

queue_publisher.xml:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework-message-queue:etc/publisher.xsd">
    <publisher topic="erp.queue.order">
        <connection name="db"
                    exchange="magento-db"
                    disabled="false"/>
    </publisher>
</config>

queue_topology.xml:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework-message-queue:etc/topology.xsd">
    <exchange name="magento-db"
              type="topic"
              connection="db">
        <binding id="TimoGErpOrderTransfer"
                 topic="erp.queue.order"
                 destinationType="queue"
                 destination="erp.queue.order"/>
    </exchange>
</config>

So adding messages to the queue works (I see it in the database table “queue_message”), however when I execute php bin/magento queue:consumers:start erp.queue.order I always get the following exception:

Type Error occurred when creating object: MagentoFrameworkMessageQueueConsumerConfigData, Argume
  nt 2 passed to MagentoFrameworkReflectionTypeProcessor::resolveFullyQualifiedClassName() must be o
  f the type string, null given, called in /Users/timog/Sites/magento2.local/vendor/magento/fra
  mework/Reflection/TypeProcessor.php on line 545

My consumer test code:

<?php

namespace TimoGOrderTransferModelQueue;

use Exception;
use PsrLogLoggerInterface;
use MagentoFrameworkSerializeSerializerJson;

class Consumer
{

    private $_logger;
    private Json $_json;


    public function __construct(
        LoggerInterface $logger,
        Json $json
    )
    {
        $this->_json = $json;
        $this->_logger = $logger;
    }


    public function process($request)
    {
        try {
            $test = $this->_json->unserialize($request);
        } catch (Exception $exc) {
            $this->_logger->critical($exc->getMessage());
        }
    }
}

I have no idea what’s wrong and I really need help please… it’s frustrating…

Thank you!

Advertisement

Answer

Probably, after more than 3 months, you already solved your problem, but you just needed to set a type for the $request parameter, or to write the PHP Docblock (the comment with @param and @return) before the process function in your consumer’s class.

Magento 2, most of the time, uses reflections to validate the type of parameters and returns, taking advantage of the comment before each method/property to retrieve the actual data type.

So basically you should just change your method declaration to this:

/**
 * @param string $request
 * @return void
 */
public function process($request)

or this:

public function process(string $request): void
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement