Manage message queues with MySQL

If you do not want to implement the RabbitMQ solution, you can manage message queues with cron jobs (or an external process manager)and the CLI to ensure that consumers are retrieving messages.

Process management

Cron jobs are the default mechanism to restart consumers. Processes started by cron consume the specified number of messages, then die after that. Re-running cron restarts the consumer.

A magic method, whose name is the same as the consumer name, is used as a callback when declaring new consumer run job in crontab.xml. Using magic methods allows you to pass the name of the consumer implicitly via method name. Alternatively, virtual types based on abstract consumer runner should be declared with concrete consumer name specified as an argument (this approach is more complex and required extra configuration).

The following shows a crontab group entry:

<group id="default">
    <job name="consumerCustomerCreatedListener" instance="Magento\Amqp\Model\ConsumerRunner" method="customerCreatedListener">
        <schedule>0 0 * * *</schedule>
    </job>
</group>

See Configure and run cron for more information about using cron with Magento.

You can also use a process manager such as Supervisor to monitor the status of processes. The manager can use the CLI to restart the processes as needed.

Command line interface

Start consumers

The CLI can be used to start consumers of the messages from the queue. Multiple consumers can be started at a same time.

./bin/magento queue:consumers:start <consumer_name> [--max-messages=<value>]

where:

<consumer_name> is the consumer to start.

--max-messages=<value> defines the maximum number of messages to consume per invocation. If number of messages are less then defined maximum number of messages, then the consumer will receive all the available messages in a queue.

If --max-messages is not defined, the consumer continues to receive endless number of new messages

After getting all available messages, the CLI command terminates. The command can be launched again with cron within a configured period of time, or manually.

List consumers

Use the following command to return a list of message queue consumers:

./bin/magento queue:consumers:list