3

going straight to the point:

I want to make some integration Magento 2 EE with some 3rd party. I want to use RabbitMQ. I know the basics, but I'm stucked with a lack of documentation and my experience/knowledge.

From time to time I want to do maintenance on 3rd party and then don't want to have any messages from magento. So I want to have an option in magento admin panel to turn on/off processing of messages in my custom rabbit queue. E.g. I will have rabbitmq queue "custom" working, then I want to disable processing messages (which will be still published to the queue) for an hour, then go back live. After going back I want to process all messages in ASC order by time of approach of the message. questions:

  1. Is Magento2-RabbitMQ queue ordered by something? Can I change a order of a single custom queue? Can it be done from the Magento side?
  2. Can I turn on/off processing of messages? Just processing - I want to make publishing messages to be available all time
1
  1. Messages are ordered by time of adding to queue. No, you cannot change the order
  2. Yes, processing can be turned off by shut down consumers

See more details:

http://devdocs.magento.com/guides/v2.2/extension-dev-guide/message-queues/message-queues.html

  • 1
    ad 1 yes, I've digged that it is FIFO queue. Thank you for confirmation that it is unchangeable ad 2 I've read it, however still I don't know how I could turn on/off a consumer programmatically – wizardOfOz Feb 22 '18 at 12:39
1

up

Anybody know if it is possible to enable/disable rabbitmq consumer on easy way programmatically in magento2?

0

This is for version 2.2.x. All jobs are processing through their consumer instance with the process() function. You can't do a plugin around the existing one Magento\Framework\MessageQueue\BatchConsumer because all of the variables of it are private and inaccessible to a plugin. You can do this by adjusting your consumer. In queue_consumer.xml you'll have something like this:

<consumer name="salesOrderImport" queue="sales_order_import"
            connection="amqp" maxMessages="500"
            consumerInstance="Magento\Framework\MessageQueue\BatchConsumer"
            handler="Yournamespace\Yourmodule\Model\Service\Order\Import\Consumer::processMessage" />

Change consumeInstance to yours like so:

<consumer name="salesOrderImport" queue="sales_order_import"
            connection="amqp" maxMessages="500"
            consumerInstance="Yournamespace\Yourmodule\Model\MessageQueue\BatchConsumer"
            handler="Yournamespace\Yourmodule\Model\Service\Order\Import\Consumer::processMessage" />

In your batch consumer, you should extend the current. Note that all variables are private, so, you'll need to override the __construct to get access to the DI parameters. In my case, I just exposed the configuration so I can do plugins instead of modifying this directly.

<?php

namespace Yournamespace\Yourmodule\Model\MessageQueue;

use Magento\Framework\App\ResourceConnection;
use Magento\Framework\MessageQueue\ConfigInterface as MessageQueueConfig;
use Magento\Framework\MessageQueue\ConsumerConfigurationInterface;
use Magento\Framework\MessageQueue\MergerFactory;
use Magento\Framework\MessageQueue\MessageEncoder;
use Magento\Framework\MessageQueue\MessageProcessorLoader;
use Magento\Framework\MessageQueue\QueueRepository;

class BatchConsumer extends \Magento\Framework\MessageQueue\BatchConsumer
{
    private $configuration;
    public function __construct(
        MessageQueueConfig $messageQueueConfig, MessageEncoder $messageEncoder, QueueRepository $queueRepository,
        MergerFactory $mergerFactory, ResourceConnection $resource, ConsumerConfigurationInterface $configuration,
        int $interval = 5, int $batchSize = 0, MessageProcessorLoader $messageProcessorLoader = null
    ) {
        parent::__construct(
            $messageQueueConfig, $messageEncoder, $queueRepository, $mergerFactory, $resource, $configuration,
            $interval, $batchSize, $messageProcessorLoader
        );

        $this->configuration = $configuration;
    }

    public function getConfiguration()
    {
        return $this->configuration;
    }

    /**
     * {@inheritdoc}
     */
    public function process($maxNumberOfMessages = null)
    {
        echo 'nothing processed!';
        return;
        parent::process($maxNumberOfMessages);
    }

}

If you were to run the queue now it would echo out 'nothing was processed' and you'd be good. If you had multiple consumers around the same topic, in my case orders was a job, invoices was another, and shipments was a third, you'd set each consumerInstance to your custom one.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.