Skip to content

Commit

Permalink
Proper README, composer.json update
Browse files Browse the repository at this point in the history
  • Loading branch information
Pavel Janda committed Jun 22, 2017
1 parent 66b0adc commit 853a740
Show file tree
Hide file tree
Showing 2 changed files with 130 additions and 9 deletions.
123 changes: 119 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,33 @@
Example configuration:
[![Latest Stable Version](https://poser.pugx.org/gamee/nette-rabbitmq/v/stable)](https://packagist.org/packages/gamee/nette-rabbitmq)
[![License](https://poser.pugx.org/gamee/nette-rabbitmq/license)](https://packagist.org/packages/gamee/nette-rabbitmq)
[![Total Downloads](https://poser.pugx.org/gamee/nette-rabbitmq/downloads)](https://packagist.org/packages/gamee/nette-rabbitmq)

# Nette RabbitMQ

Nette extension for RabbitMQ (using composer package [jakubkulhan/bunny](https://github.com/jakubkulhan/bunny))

## Example setup

### Downloading composer package

```
services:
- App\Queue\RabbitMq\Consumer\TestConsumer
composer require gamee/nette-rabbitmq
```

### Extension registration

config.neon:

```
extensions:
rabbitmq: Gamee\RabbitMQ\DI\RabbitMQExtension
```

### Example configuration

```
services:
- TestConsumer
rabbitmq:
connections:
Expand Down Expand Up @@ -32,5 +56,96 @@ rabbitmq:
consumers:
testConsumer:
queue: testQueue
callback: [@App\Queue\RabbitMq\Consumer\TestConsumer, consume]
callback: [@TestConsumer, consume]
```

### Publishing messages

services.neon:

```
services:
- TestQueue(@Gamee\RabbitMQ\Client::getProducer(testProducer))
```

TestQueue.php:

```php
<?php

declare(strict_types=1);

use Gamee\RabbitMQ\Producer\Producer;

final class TestQueue
{

/**
* @var Producer
*/
private $testReindexProducer;


public function __construct(Producer $usersReindexProducer)
{
$this->usersReindexProducer = $usersReindexProducer;
}


public function publish(string $message): void
{
$json = json_encode(['message' => $message]);
$headers = [];

$this->usersReindexProducer->publish($json, $headers);
}

}
```

### Consuming messages

Your consumer callback has to return a confirmation that particular message has been acknowledges (or different states - unack, reject).

TestConsumer.php

```php
<?php

declare(strict_types=1);

use Bunny\Message;
use Gamee\RabbitMQ\Consumer\IConsumer;

final class TestConsumer implements IConsumer
{

public function consume(Message $message): int
{
$messageData = json_decode($message->content);

$headers = $message->headers;

/**
* @todo Some logic here...
*/

return IConsumer::MESSAGE_ACK; // Or ::MESSAGE_NACK || ::MESSAGE_REJECT
}

}
```

### Running a consumer trough CLI

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:

```
php index.php rabbitmq:consumer testConsumer 3600
```

`rabbitmq:staticConsumer` will consume particular amount of messages. Following example will consume just 20 messages:

```
php index.php rabbitmq:staticConsumer testConsumer 20
```
16 changes: 11 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
{
"name": "gamee-backend/gamee-rabbitmq",
"name": "gamee/nette-rabbitmq",
"type": "library",
"homepage": "https://gitlab.cleevio.cz/gamee-backend/gamee-redis",
"license": "MIT",
"homepage": "https://github.com/gameeapp/nette-rabbitmq",
"keywords": ["rabbitmq","rabbit","bunnyphp","bunny","nette","extension","php"],
"support": {
"issues": "https://gitlab.cleevio.cz/gamee-backend/gamee-redis/issues"
"issues": "https://github.com/gameeapp/nette-rabbitmq/issues"
},
"authors": [
{
"name": "Pavel Janda"
}
],
"autoload": {
"psr-4": {
"Gamee\\RabbitMQ\\": "src/"
Expand All @@ -20,7 +27,6 @@
"kdyby/console": "~2.6.1"
},
"require-dev": {
"tracy/tracy": "^2.4",
"nette/tester": "^1.7"
"tracy/tracy": "^2.4"
}
}

0 comments on commit 853a740

Please sign in to comment.