【发布时间】:2017-07-04 16:28:20
【问题描述】:
我在 Symfony 2.8 项目中使用RabbitMQBundle,并且我想使用自定义生产者类,它在发布 RabbitMQ 消息之前将实体(消息)保存在数据库中。
我在 config.yml 中定义了自定义生产者类:
old_sound_rabbit_mq:
...
producers:
myproducer:
class: AppBundle\Services\GenericProducer
connection: default
exchange_options: {name: 'my_exchange', type: direct}
还有自定义的 Producer 类:
<?php
namespace AppBundle\Services;
use AppBundle\Entity\Message;
use OldSound\RabbitMqBundle\RabbitMq\Producer;
/**
* Customised Producer, that publishes AMQP Messages
* but also:
* - writes an entry in the Message table
*/
class GenericProducer extends Producer
{
/**
* Entity Manager
*/
protected $em;
public function setEntityManager($entityManager)
{
$this->em = $entityManager;
return $this;
}
/**
* Publishes the message and merges additional properties with basic properties
* And also:
* - writes an entry in the Message table
*
* @param string $action
* @param array $parameters
* @param string $routingKey
* @param array $additionalProperties
* @param array|null $headers
*/
public function publish($action, $parameters = array() , $routingKey = '', $additionalProperties = array(), array $headers = null)
{
$message = new Message();
$message->setAction($action)
->setParameters($parameters);
$this->em->persist($message);
$this->em->flush();
$msgBody = array(
'action' => $action,
'parameters' => $parameters
);
parent::publish($msgBody, $routingKey, $additionalProperties, $headers);
}
}
我如何拨打GenericProducer->setEntityManager,因为生产者没有在 services.yml 中定义,就像其他服务一样?
还有其他方法可以实现吗?
感谢您的宝贵时间。
【问题讨论】:
标签: php symfony rabbitmq symfony-2.8