|
| 1 | +# Async event dispatcher (Symfony) |
| 2 | + |
| 3 | +The doc shows how you can setup async event dispatching in plain PHP. |
| 4 | +If you are looking for the ways to use it in Symfony application [read this post instead](../bundle/async_events.md) |
| 5 | + |
| 6 | +* [Installation](#installation) |
| 7 | +* [Configuration](#configuration) |
| 8 | +* [Dispatch event](#dispatch-event) |
| 9 | +* [Process async events](#process-async-events) |
| 10 | + |
| 11 | +## Installation |
| 12 | + |
| 13 | +You need the async dispatcher library and a one of [the supported transports](../transport) |
| 14 | + |
| 15 | +```bash |
| 16 | +$ composer require enqueue/async-event-dispatcher enqueue/fs |
| 17 | +``` |
| 18 | + |
| 19 | +## Configuration |
| 20 | + |
| 21 | +```php |
| 22 | +<?php |
| 23 | + |
| 24 | +// config.php |
| 25 | + |
| 26 | +use Enqueue\AsyncEventDispatcher\AsyncListener; |
| 27 | +use Enqueue\AsyncEventDispatcher\AsyncProcessor; |
| 28 | +use Enqueue\AsyncEventDispatcher\PhpSerializerEventTransformer; |
| 29 | +use Enqueue\AsyncEventDispatcher\AsyncEventDispatcher; |
| 30 | +use Enqueue\AsyncEventDispatcher\SimpleRegistry; |
| 31 | +use Enqueue\Fs\FsConnectionFactory; |
| 32 | +use Symfony\Component\EventDispatcher\EventDispatcher; |
| 33 | + |
| 34 | +require_once __DIR__.'/vendor/autoload.php'; |
| 35 | + |
| 36 | +// it could be any other enqueue/psr-queue compatible context. |
| 37 | +$context = (new FsConnectionFactory('file://'.__DIR__.'/queues'))->createContext(); |
| 38 | +$eventQueue = $context->createQueue('symfony_events'); |
| 39 | + |
| 40 | +$registry = new SimpleRegistry( |
| 41 | + ['the_event' => 'default'], |
| 42 | + ['default' => new PhpSerializerEventTransformer($context, true)] |
| 43 | +); |
| 44 | + |
| 45 | +$asyncListener = new AsyncListener($context, $registry, $eventQueue); |
| 46 | + |
| 47 | +$dispatcher = new EventDispatcher(); |
| 48 | + |
| 49 | +// the listener sends even as a message through MQ |
| 50 | +$dispatcher->addListener('the_event', $asyncListener); |
| 51 | + |
| 52 | +$asyncDispatcher = new AsyncEventDispatcher($dispatcher, $asyncListener); |
| 53 | + |
| 54 | +// the listener is executed on consumer side. |
| 55 | +$asyncDispatcher->addListener('the_event', function() { |
| 56 | +}); |
| 57 | + |
| 58 | +$asyncProcessor = new AsyncProcessor($registry, $asyncDispatcher); |
| 59 | +``` |
| 60 | + |
| 61 | +## Dispatch event |
| 62 | + |
| 63 | +```php |
| 64 | +<?php |
| 65 | + |
| 66 | +// send.php |
| 67 | + |
| 68 | +use Symfony\Component\EventDispatcher\GenericEvent; |
| 69 | + |
| 70 | +require_once __DIR__.'/vendor/autoload.php'; |
| 71 | + |
| 72 | +include __DIR__.'/config.php'; |
| 73 | + |
| 74 | +$dispatcher->dispatch('the_event', new GenericEvent('theSubject')); |
| 75 | +``` |
| 76 | + |
| 77 | +## Process async events |
| 78 | + |
| 79 | +```php |
| 80 | +<?php |
| 81 | + |
| 82 | +// consume.php |
| 83 | + |
| 84 | +use Enqueue\Psr\PsrProcessor; |
| 85 | + |
| 86 | +require_once __DIR__.'/vendor/autoload.php'; |
| 87 | +include __DIR__.'/config.php'; |
| 88 | + |
| 89 | +$consumer = $context->createConsumer($eventQueue); |
| 90 | + |
| 91 | +while (true) { |
| 92 | + if ($message = $consumer->receive(5000)) { |
| 93 | + $result = $asyncProcessor->process($message, $context); |
| 94 | + |
| 95 | + switch ((string) $result) { |
| 96 | + case PsrProcessor::ACK: |
| 97 | + $consumer->acknowledge($message); |
| 98 | + break; |
| 99 | + case PsrProcessor::REJECT: |
| 100 | + $consumer->reject($message); |
| 101 | + break; |
| 102 | + case PsrProcessor::REQUEUE: |
| 103 | + $consumer->reject($message, true); |
| 104 | + break; |
| 105 | + default: |
| 106 | + throw new \LogicException('Result is not supported'); |
| 107 | + } |
| 108 | + } |
| 109 | +} |
| 110 | +``` |
| 111 | + |
| 112 | +[back to index](../index.md) |
0 commit comments