我使用此配置在我的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);
}
}
在Controller中我尝试进行测试发布:
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
答案 0 :(得分:1)
问题是在Producer构造函数中我没有将连接变量传递给父
这是正确的Producer构造函数:
public function __construct(AMQPConnection $conn, AMQPChannel $ch = null, $consumerTag = null)
{
parent::__construct($conn, $ch, $consumerTag);
parent::setContentType("application/json");
}
此处讨论此主题:Github issue