我正在尝试从 Rabbitmq 消费者分派一个自定义事件。这个过程曾经在 Symfony 3.4 上工作,最近将该项目升级到 Symfony Flex(4.3)。
数据集提交消费者.php
use OldSound\RabbitMqBundle\RabbitMq\ConsumerInterface;
/**
* A consumer of dataset submission messages.
*
* @see ConsumerInterface
*/
class DatasetSubmissionConsumer implements ConsumerInterface
{
/**
* The entity event dispatcher.
*
* @var EntityEventDispatcher
*/
protected $entityEventDispatcher;
/**
* Constructor.
*
* @param EntityEventDispatcher $entityEventDispatcher The entity event dispatcher.
*/
public function __construct(
EntityEventDispatcher $entityEventDispatcher,
) {
$this->entityEventDispatcher = $entityEventDispatcher;
}
/**
* Process a filer message.
*
* @param AMQPMessage $message A filer message.
*
* @return boolean True if success, false otherwise.
*/
public function execute(AMQPMessage $message)
{
$datasetSubmissionId = $message->body;
// Do Something //
$this->entityEventDispatcher->dispatch($datasetSubmission, 'dataset_processed');
return true;
}
}
数据集提交监听器.php
/**
* Listener class for Dataset Submission-related events.
*/
class DatasetSubmissionListener
{
/**
* Method to send an email to DRPM on a dataset_processed event.
*
* @param EntityEvent $event Event being acted upon.
*
* @return void
*/
public function onDatasetProcessed(EntityEvent $event)
{
$datasetSubmission = $event->getEntity();
// Added if-statement so that emails are sent to data-managers only
// Do Something and send Email
}
}
EntityEventDispatcher.php
<?php
namespace App\Event;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
/**
* An entity event dispatcher.
*/
class EntityEventDispatcher
{
/**
* The event dispatcher to use in this entity event dispatcher.
*
* @var EventDispatcherInterface
*/
private $eventDispatcher;
/**
* Constructor.
*
* @param EventDispatcherInterface $eventDispatcher The event dispatcher to use.
*/
public function __construct(EventDispatcherInterface $eventDispatcher)
{
$this->eventDispatcher = $eventDispatcher;
}
/**
* Dispatch an Entity event.
*
* @param Entity $entity The Entity the event is for.
* @param string $entityEventName The name of the entity event.
*
* @return void
*/
public function dispatch(Entity $entity, string $entityEventName)
{
$this->eventDispatcher->dispatch(
'pelagos.entity.' . $entity->getUnderscoredName() . '.' . $entityEventName,
new EntityEvent($entity)
);
}
}
App\Event\DatasetSubmissionListener:
tags:
- { name: kernel.event_listener, event: pelagos.entity.dataset_submission.dataset_processed, method: onDatasetProcessed }
还运行了bin/console debug:event-dispatcher
并且事件是可调用的
"pelagos.entity.dataset_submission.dataset_processed" event
-----------------------------------------------------------
------- ----------------------------------------------------------- ----------
Order Callable Priority
------- ----------------------------------------------------------- ----------
#1 App\Event\DatasetSubmissionListener::onDatasetProcessed() 0
------- ----------------------------------------------------------- ----------
使用 Symfony 4.3、Rabbitmq 3.3.5
在 Rabbitmq 消费者成功退出后,事件侦听器不会捕获此分派的事件。有没有办法调试或使这项工作?
谢谢。