|
1 |
| -Example configuration: |
| 1 | +[](https://packagist.org/packages/gamee/nette-rabbitmq) |
| 2 | +[](https://packagist.org/packages/gamee/nette-rabbitmq) |
| 3 | +[](https://packagist.org/packages/gamee/nette-rabbitmq) |
| 4 | + |
| 5 | +# Nette RabbitMQ |
| 6 | + |
| 7 | +Nette extension for RabbitMQ (using composer package [jakubkulhan/bunny](https://github.com/jakubkulhan/bunny)) |
| 8 | + |
| 9 | +## Example setup |
| 10 | + |
| 11 | +### Downloading composer package |
2 | 12 |
|
3 | 13 | ```
|
4 |
| -services: |
5 |
| - - App\Queue\RabbitMq\Consumer\TestConsumer |
| 14 | +composer require gamee/nette-rabbitmq |
| 15 | +``` |
| 16 | + |
| 17 | +### Extension registration |
6 | 18 |
|
| 19 | +config.neon: |
| 20 | + |
| 21 | +``` |
| 22 | +extensions: |
| 23 | + rabbitmq: Gamee\RabbitMQ\DI\RabbitMQExtension |
| 24 | +``` |
| 25 | + |
| 26 | +### Example configuration |
| 27 | + |
| 28 | +``` |
| 29 | +services: |
| 30 | + - TestConsumer |
7 | 31 |
|
8 | 32 | rabbitmq:
|
9 | 33 | connections:
|
@@ -32,5 +56,96 @@ rabbitmq:
|
32 | 56 | consumers:
|
33 | 57 | testConsumer:
|
34 | 58 | queue: testQueue
|
35 |
| - callback: [@App\Queue\RabbitMq\Consumer\TestConsumer, consume] |
| 59 | + callback: [@TestConsumer, consume] |
| 60 | +``` |
| 61 | + |
| 62 | +### Publishing messages |
| 63 | + |
| 64 | +services.neon: |
| 65 | + |
| 66 | +``` |
| 67 | +services: |
| 68 | + - TestQueue(@Gamee\RabbitMQ\Client::getProducer(testProducer)) |
| 69 | +``` |
| 70 | + |
| 71 | +TestQueue.php: |
| 72 | + |
| 73 | +```php |
| 74 | +<?php |
| 75 | + |
| 76 | +declare(strict_types=1); |
| 77 | + |
| 78 | +use Gamee\RabbitMQ\Producer\Producer; |
| 79 | + |
| 80 | +final class TestQueue |
| 81 | +{ |
| 82 | + |
| 83 | + /** |
| 84 | + * @var Producer |
| 85 | + */ |
| 86 | + private $testReindexProducer; |
| 87 | + |
| 88 | + |
| 89 | + public function __construct(Producer $usersReindexProducer) |
| 90 | + { |
| 91 | + $this->usersReindexProducer = $usersReindexProducer; |
| 92 | + } |
| 93 | + |
| 94 | + |
| 95 | + public function publish(string $message): void |
| 96 | + { |
| 97 | + $json = json_encode(['message' => $message]); |
| 98 | + $headers = []; |
| 99 | + |
| 100 | + $this->usersReindexProducer->publish($json, $headers); |
| 101 | + } |
| 102 | + |
| 103 | +} |
| 104 | +``` |
| 105 | + |
| 106 | +### Consuming messages |
| 107 | + |
| 108 | +Your consumer callback has to return a confirmation that particular message has been acknowledges (or different states - unack, reject). |
| 109 | + |
| 110 | +TestConsumer.php |
| 111 | + |
| 112 | +```php |
| 113 | +<?php |
| 114 | + |
| 115 | +declare(strict_types=1); |
| 116 | + |
| 117 | +use Bunny\Message; |
| 118 | +use Gamee\RabbitMQ\Consumer\IConsumer; |
| 119 | + |
| 120 | +final class TestConsumer implements IConsumer |
| 121 | +{ |
| 122 | + |
| 123 | + public function consume(Message $message): int |
| 124 | + { |
| 125 | + $messageData = json_decode($message->content); |
| 126 | + |
| 127 | + $headers = $message->headers; |
| 128 | + |
| 129 | + /** |
| 130 | + * @todo Some logic here... |
| 131 | + */ |
| 132 | + |
| 133 | + return IConsumer::MESSAGE_ACK; // Or ::MESSAGE_NACK || ::MESSAGE_REJECT |
| 134 | + } |
| 135 | + |
| 136 | +} |
| 137 | +``` |
| 138 | + |
| 139 | +### Running a consumer trough CLI |
| 140 | + |
| 141 | +There are two consumer commands prepared. `rabbitmq:consumer` wiil consume messages for specified amount of time (in seconds). Following command wiil be consuming messages for one hour: |
| 142 | + |
| 143 | +``` |
| 144 | +php index.php rabbitmq:consumer testConsumer 3600 |
| 145 | +``` |
| 146 | + |
| 147 | +`rabbitmq:staticConsumer` will consume particular amount of messages. Following example will consume just 20 messages: |
| 148 | + |
| 149 | +``` |
| 150 | +php index.php rabbitmq:staticConsumer testConsumer 20 |
36 | 151 | ```
|
0 commit comments