|
1 |
| -# magento2-enqueue |
| 1 | +# Magento Enqueue. Quick tour |
| 2 | + |
| 3 | +## Installation |
| 4 | + |
| 5 | +We use [composer](https://getcomposer.org/) and [cotya/magento-composer-installer](https://github.com/Cotya/magento-composer-installer) plugin to install [magento-enqueue](https://github.com/php-enqueue/magento-enqueue) extension. |
| 6 | + |
| 7 | +To install libraries run the commands in the application root directory. |
| 8 | + |
| 9 | +```bash |
| 10 | +composer require "enqueue/magento2-enqueue:*@dev" "enqueue/amqp-ext" |
| 11 | +php bin/magento setup:upgrade |
| 12 | +``` |
| 13 | + |
| 14 | +## Configuration |
| 15 | + |
| 16 | +At this stage we have configure the Enqueue extension in Magento backend. |
| 17 | +The config is here: `Stores -> Configuration -> General -> Enqueue Message Queue`. |
| 18 | +Here's the example of Amqp transport that connects to RabbitMQ broker on localhost: |
| 19 | + |
| 20 | + |
| 21 | + |
| 22 | + |
| 23 | +## Publish Message |
| 24 | + |
| 25 | +To send a message you have to take enqueue helper and call `send` method. |
| 26 | + |
| 27 | +```php |
| 28 | +<?php |
| 29 | + |
| 30 | +$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); |
| 31 | +$enqueueManager = $objectManager->create('Enqueue\Enqueue\Model\EnqueueManager'); |
| 32 | +$enqueueManager->send('a_topic', 'aMessage'); |
| 33 | +``` |
| 34 | + |
| 35 | +## Message Consumption |
| 36 | + |
| 37 | +I assume you have `acme` Magento module properly created, configured and registered. |
| 38 | +To consume messages you have to define a processor class first: |
| 39 | + |
| 40 | +```php |
| 41 | +<?php |
| 42 | +// app/code/Acme/Module/Helper/Async/Foo.php |
| 43 | + |
| 44 | +namespace Acme\Module\Helper\Async; |
| 45 | + |
| 46 | +use Interop\Queue\PsrContext; |
| 47 | +use Interop\Queue\PsrMessage; |
| 48 | +use Interop\Queue\PsrProcessor; |
| 49 | + |
| 50 | +class Foo implements PsrProcessor |
| 51 | +{ |
| 52 | + public function process(PsrMessage $message, PsrContext $context) |
| 53 | + { |
| 54 | + // do job |
| 55 | + // $message->getBody() -> 'payload' |
| 56 | + |
| 57 | + return self::ACK; // acknowledge message |
| 58 | + // return self::REJECT; // reject message |
| 59 | + // return self::REQUEUE; // requeue message |
| 60 | + } |
| 61 | +} |
| 62 | +``` |
| 63 | + |
| 64 | +than subscribe it to a topic or several topics: |
| 65 | + |
| 66 | + |
| 67 | +```xml |
| 68 | +<!-- app/etc/local.xml --> |
| 69 | + |
| 70 | +<config> |
| 71 | + <default> |
| 72 | + <enqueue> |
| 73 | + <processors> |
| 74 | + <foo-processor> |
| 75 | + <topic>a_topic</topic> |
| 76 | + <helper>acme/async_foo</helper> |
| 77 | + </foo-processor> |
| 78 | + </processors> |
| 79 | + </enqueue> |
| 80 | + </default> |
| 81 | +</config> |
| 82 | +``` |
| 83 | + |
| 84 | +and run message consume command: |
| 85 | + |
| 86 | +```bash |
| 87 | +$ php bin/magento enqueue:consume -vvv --setup-broker |
| 88 | +``` |
| 89 | + |
| 90 | +[back to index](../index.md) |
0 commit comments