contributte / rabbitmq

🐰 RabbitMQ (AMQP, STOMP, MQTT) using BunnyPHP for Nette Framework (@nette).
https://contributte.org/packages/contributte/rabbitmq.html
MIT License
24 stars 25 forks source link

Consuming doesnt work - Nette 3 compatibility? #24

Closed BigOHenry closed 5 years ago

BigOHenry commented 5 years ago

Hello, i am trying to setup and test run on Nette 3 (with Nette 2.4 was fine).

Adding message to RabbitMq is working, but consuming not. When i executed command for consuming, this will return:

<h1>Redirect</h1>

<p><a href="http:/sign/in">Please click here to continue</a>.</p>

I dont know if i forgot something or if its Nette 3 issue. Any ideas?

config.neon

services:
    - App\RabbitMq\Consumer\TestConsumer
    - App\RabbitMq\Queue\TestQueue(@Gamee\RabbitMQ\Client::getProducer(testProducer))

extensions:
    rabbitmq: Gamee\RabbitMQ\DI\RabbitMQExtension 

rabbitmq:
    connections:
        default: %rabbitmq_connection%
    queues:
        testQueue:
            connection: default
            autoCreate: true

    producers:
        testProducer:
            queue: testQueue
            contentType: application/json
            deliveryMode: 2 # Producer::DELIVERY_MODE_PERSISTENT

    consumers:
        testConsumer:
            queue: testQueue
            callback: [@App\RabbitMq\Consumer\TestConsumer, consume]
            qos:
                prefetchSize: 0
                prefetchCount: 5

TestConsumer

<?php declare(strict_types=1);

namespace App\RabbitMq\Consumer;

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

/**
 * Class TestConsumer
 * @package App\RabbitMq\Consumer
 */
final class TestConsumer implements IConsumer
{

    /**
     * @param Message $message
     * @return int
     */
    public function consume(Message $message): int
    {
        $messageData = json_decode($message->content);

        $headers = $message->headers;

        var_dump($messageData);

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

}

TestQueue

<?php declare(strict_types=1);

namespace App\RabbitMq\Queue;

use Gamee\RabbitMQ\Producer\Producer;

/**
 * Class TestQueue
 * @package App\RabbitMq\Queue
 */
final class TestQueue
{

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

    /**
     * TestQueue constructor.
     * @param Producer $testProducer
     */
    public function __construct(Producer $testProducer)
    {
        $this->testProducer = $testProducer;
    }

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

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

}

index.php


<?php

declare(strict_types=1);

require __DIR__ . '/../vendor/autoload.php';

App\Bootstrap::boot()
             ->createContainer()
             ->getByType(Nette\Application\Application::class)
             ->run();
BigOHenry commented 5 years ago

Problem solved. There is a typo in documentation. This doesnt work (but on Kdyby\RabbitMq it works): php www/index.php rabbitmq:consumer testConsumer

this works: php bin/console.php rabbitmq:consumer testConsumer