RabbitMqBundle 连接未建立

RabbitMqBundle connection not enstablished

我在我的 sf2 项目中安装了 https://github.com/videlalvaro/RabbitMqBundle,配置如下:

old_sound_rabbit_mq:
connections:
    default:
        host:     'localhost'
        port:     5672
        user:     'guest'
        password: 'guest'
        vhost:    '/'
        lazy:     false

producers:
    processing:
        connection: default
        exchange_options: { name: 'processing', type: direct }
        class: Assignment\Bundle\MainBundle\RabbitMq\Producer\ProcessingProducer

服务配置:

processing_producer:
    class: Assignment\Bundle\MainBundle\RabbitMq\Producer\ProcessingProducer
    arguments: []

制作者来源:

class ProcessingProducer extends Producer
{

public function __construct()
{
    parent::setContentType("application/json");
}

/**
 * @param string $msgBody
 * @param string $routingKey
 * @param array $additionalProperties
 */
public function publish($msgBody, $routingKey = '', $additionalProperties = array())
{
    $msgBody = json_encode($msgBody);

    parent::publish($msgBody, $routingKey, $additionalProperties);
}
}

我尝试在控制器中进行测试发布:

public function indexAction()
{

    $this->get('old_sound_rabbit_mq.processing_producer')->publish(array('a' => 'b'));


    return $this->render('::Default/index.html.twig');
}

结果是:错误:调用非对象上的成员函数 channel()

[1] Symfony\Component\Debug\Exception\FatalErrorException: Error: Call to a member function channel() on a non-object
at n/a
    in /var/www/vendor/oldsound/rabbitmq-bundle/OldSound/RabbitMqBundle/RabbitMq/BaseAmqp.php line 75

at OldSound\RabbitMqBundle\RabbitMq\BaseAmqp->getChannel()
    in /var/www/vendor/oldsound/rabbitmq-bundle/OldSound/RabbitMqBundle/RabbitMq/BaseAmqp.php line 129

at OldSound\RabbitMqBundle\RabbitMq\BaseAmqp->exchangeDeclare()
    in /var/www/vendor/oldsound/rabbitmq-bundle/OldSound/RabbitMqBundle/RabbitMq/BaseAmqp.php line 167

at OldSound\RabbitMqBundle\RabbitMq\BaseAmqp->setupFabric()
    in /var/www/vendor/oldsound/rabbitmq-bundle/OldSound/RabbitMqBundle/RabbitMq/Producer.php line 45

at OldSound\RabbitMqBundle\RabbitMq\Producer->publish('msgBody' => '', 'routingKey' => '', 'additionalProperties' => '')
    in /var/www/src/Assignment/Bundle/MainBundle/RabbitMq/Producer/ProcessingProducer.php line 26

at Assignment\Bundle\MainBundle\RabbitMq\Producer\ProcessingProducer->publish('msgBody' => '', 'routingKey' => '', 'additionalProperties' => '')
    in /var/www/src/Assignment/Bundle/MainBundle/Controller/DefaultController.php line 15

at Assignment\Bundle\MainBundle\Controller\DefaultController->indexAction()
    in /var/www/app/bootstrap.php.cache line 3022

at call_user_func_array('', '')
    in /var/www/app/bootstrap.php.cache line 3022

at Symfony\Component\HttpKernel\HttpKernel->handleRaw('request' => '', 'type' => '')
    in /var/www/app/bootstrap.php.cache line 2984

at Symfony\Component\HttpKernel\HttpKernel->handle('request' => '', 'type' => '', 'catch' => '')
    in /var/www/app/bootstrap.php.cache line 3133

at Symfony\Component\HttpKernel\DependencyInjection\ContainerAwareHttpKernel->handle('request' => '', 'type' => '', 'catch' => '')
    in /var/www/app/bootstrap.php.cache line 2377

at Symfony\Component\HttpKernel\Kernel->handle('request' => '', 'type' => '', 'catch' => '')
    in /var/www/web/app_dev.php line 28

at {main}()
    in /var/www/web/app_dev.php line 0

问题是在 Producer 构造函数中我没有将连接变量传递给 parent

这是正确的 Producer 构造函数:

public function __construct(AMQPConnection $conn, AMQPChannel $ch = null, $consumerTag = null)
{
    parent::__construct($conn, $ch, $consumerTag);

    parent::setContentType("application/json");
}

此处讨论此主题:Github issue