Magento – How to Use RabbitMq in Magento2 CE

crondatabasemagento2queuerabbit-mq

I have follow this link to knows about RabbitMq is used in Magento2EE. for Instead of Mysql+cron Process in Magento2CE.

I annualized about some drawbacks while using Mysql+Cron in Magento 2 CE. follow this link

Disadvantages if compared to real queue-solutions: first off, cron is running as a cycle so you are not able to immediately react on adding of a record into the queue. Secondly, using such a scheme makes it quite difficult to parallel a queue. Magento 2.0 EE gives you the possibility to use queues not only on the basis of Mysql+cron, but also on the basis of RabbitMq.

I need to overcome these drawback, I saw this statement magento2 devdocs link

RabbitMQ must be installed and configured after Magento Community Edition and before Magento Enterprise Edition.

so I install and integrate rabbitmq in magento2 CE. follow this install rabbitmq and Integrate to magento 2CE after follow this guide to working samples to use Rabbitmq.

Next I Have configured rabbitmq to magento2CE

add my env.php like this:

   'queue' => [
            'amqp' => [
                'host' => 'hostip',
                'port' => '5672',
                'user' => 'user1',
                'password' => 'userpass',
                'virtualhost' => '/',
                'ssl' => ''
            ]
        ]

Next I create a queue.xml in custom module:

Vendor/Module/etc/queue.xml:

    <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework-message-queue:etc/queue.xsd">
        <publisher name="test-publisher-1" connection="rabbitmq" exchange="magento"/>
        <topic name="customer.created" schema="Magento\Customer\Api\Data\CustomerInterface" publisher="test-publisher-1"/>
        <consumer name="customerCreatedListener" queue="test-queue-1" connection="rabbitmq" class="vendor\RabbitMq\Block\Testing" method="processMessage"/>
        <bind queue="test-queue-1" exchange="magento" topic="customer.created" />
    </config>

Consumer Class:

vendor/Module/Block/Testing:

    <?php
      namespace Vendor\Module\Block;

    require_once  '/var/www/public/vendor/autoload.php';
    use PhpAmqpLib\Connection\AMQPStreamConnection;

    class Testing //extends \Magento\Framework\View\Element\Template
    {


      public function processMessage($argv)
      {
              $connection = new AMQPStreamConnection('hostip', 5672, 'user1', 'userpass');
              $channel = $connection->channel();

              $channel->exchange_declare('magento', 'topic', false, false, false);

              list($queue_name, ,) = $channel->queue_declare("test-queue-1", false, false, true, false);

              $binding_keys = array_slice($argv, 0);
              if( empty($binding_keys )) {
                  file_put_contents('php://stderr', "Usage: $argv[0] [binding_key]\n");
                  exit(1);
              }

              foreach($binding_keys as $binding_key) {
                  $channel->queue_bind($queue_name, 'magento', $binding_key);
              }

              echo ' [*] Waiting for logs. To exit press CTRL+C', "\n";

              $callback = function($msg){
                  echo ' [x] ',$msg->delivery_info['routing_key'], ':', $msg->body, "\n";
              };

              $channel->basic_consume($queue_name, '', false, true, false, false, $callback);

              while(count($channel->callbacks)) {
                  $channel->wait();
              }

              $channel->close();
              $connection->close();

      }

    }

    $test = new Testing();
    $arr =array("customer.*");
    $test->processMessage($arr);

I have listen customerCreatedListener, after Configured , I try to create one new customer, But NO queue added in rabbit mq,

Reference:

enter image description here

Suggest Me

  1. Can we use Rabbitmq in magento2 CE?

  2. If use Rabbitmq in magento2 CE, what I miss in my Configuration.

  3. How to use Rabbitmq instead of Mysql+Cron in Magento2 CE

Best Answer

Integration with Rabbitmq is EE only functionality.

But you can use it in CE version if use external library and develop infrastructure by self.

Also, you can create separate group for cron task and write code in style like

$startTime = microtime();
while( microtime(1) - $startTume > 60 * 1000) {
   // process messages
   usleep(100);
}