Close [x]

Configure message queue topology

Edit this page on GitHub

The message queue topology can only be configured after Magento Community Edition has been installed and before Magento Enterprise Editions has been installed.

Each module that is to be a publisher must be configured as such. If you want a module to use the MQF, create a <module>/etc/queue.xml file and define the publisher, consumers, exchanges and bindings.

Edit the queue.xml file

The queue.xml file can contain the following elements:

  • publisher
  • topic
  • consumer
  • bind

Required elements

Each queue.xml file must contain the following lines:

<?xml version="1.0"?>

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework-message-queue:etc/queue.xsd">
.
.
.
</config>

publisher element

The publisher element configures the type of connection and the exchange to publish to. By default, Magento uses one exchange. The name of exchange is a part of the publisher configuration. However multiple exchanges are supported, based on the AMQP model.

ParameterDescription
name A unique identifer for the publisher. The value is specified in a topic element. The default system publisher name is `default`.
connection If RabbitMQ is to used to manage the queue, then the value must be rabbitmq. The value can also be db or the name of a custom adapter.
exchange The name of the exchange to publish to. The value is referenced from the bind element. The default system exchange name is `magento`.

topic element

Configuring the topic element defines the interface that processes the message and assigns a publisher.

Parameter

Description

name

The name assigned to the topic. The format should be object.action You can further distinguish topic names by appending .subaction to the end of the name. Use the past tense for all verbs, to indicate the event has already happened.

Examples: customer.created, customer.sent.email

The value is specified in a bind element.

schema

The interface that describes the structure of the message. It should be in the format of a Data Interface from the Service Contracts. For example, Magento\Customer\Api\Data\CustomerInterface.

You can also specify a service method signature, such as Magento\Customer\Api\CustomerRepositoryInterface::save. In this case, format the message as an array of all service method parameters, like for a call_user_func_array call. The consumer's callback should expect each message part to be passed as a separate parameter.

publisher The name of a publisher.

consumer element

Each consumer elements maps the receiver of a message to a specific queue. The class and method parameters indicate what receives and processes the message.

ParameterDescription
name The name of the consumer. The value should be the same as the magic method that to be used as a callback.
queue Defines the queue name to send the message to. This value is used in the definition of a bind element.
connection Must be rabbitmq or other value specified in the `connection` parameter in of a publisher.
class The path to a Magento class that consumes the message.
method The method within the specified class that processes the message.
max_messages Specifies the maximum number of messages to consume.

bind element

The bind elements link topics to queues and exchanges, defining the message queue topology. A topic can be sent to any number of queues.

ParameterDescription
queue The name of a queue defined in a consumer element.
exchange The name of an exchange defined in a publisher element.
topic The name of a topic defined in a topic element. You can specify an asterisk (*) or pound sign (#) as wildcards.

Sample `queue.xml` file

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../../../../../lib/internal/Magento/Framework/Amqp/etc/queue.xsd">
    <publisher name="test-publisher-1" connection="rabbitmq" exchange="magento"/>
    <publisher name="test-publisher-2" connection="db" exchange="magento"/>
    <topic name="customer.created" schema="Magento\Customer\Api\Data\CustomerInterface" publisher="test-publisher-1"/>
    <topic name="customer.deleted" schema="Magento\Customer\Api\Data\CustomerInterface" publisher="test-publisher-2"/>
    <consumer name="customerCreatedListener" queue="test-queue-1" connection="rabbitmq" class="Data\Type" method="processMessage"/>
    <consumer name="customerDeletedListener" queue="test-queue-2" connection="db" class="Other\Type" method="processMessage2" max_messages="98765"/>
    <bind queue="test-queue-1" exchange="magento" topic="customer.created" />
    <bind queue="test-queue-2" exchange="magento" topic="customer.deleted" />
</config>