From b16bcd54cb716f1746578a7024a9f0de51950187 Mon Sep 17 00:00:00 2001 From: Matyas Somfai Date: Wed, 18 Sep 2019 17:57:55 +0200 Subject: [PATCH] added before publishing and after publishing events --- Event/AMQPEvent.php | 28 ++++++++++++++++++++++++++ Event/AfterPublishingMessageEvent.php | 28 ++++++++++++++++++++++++++ Event/BeforePublishingMessageEvent.php | 28 ++++++++++++++++++++++++++ README.md | 4 ++++ RabbitMq/Producer.php | 4 ++++ 5 files changed, 92 insertions(+) create mode 100644 Event/AfterPublishingMessageEvent.php create mode 100644 Event/BeforePublishingMessageEvent.php diff --git a/Event/AMQPEvent.php b/Event/AMQPEvent.php index 3c9dee11..2188904d 100644 --- a/Event/AMQPEvent.php +++ b/Event/AMQPEvent.php @@ -3,6 +3,7 @@ namespace OldSound\RabbitMqBundle\Event; use OldSound\RabbitMqBundle\RabbitMq\Consumer; +use OldSound\RabbitMqBundle\RabbitMq\Producer; use PhpAmqpLib\Message\AMQPMessage; use Symfony\Component\EventDispatcher\Event; @@ -18,6 +19,8 @@ class AMQPEvent extends Event const ON_IDLE = 'on_idle'; const BEFORE_PROCESSING_MESSAGE = 'before_processing'; const AFTER_PROCESSING_MESSAGE = 'after_processing'; + const BEFORE_PUBLISHING_MESSAGE = 'before_publishing'; + const AFTER_PUBLISHING_MESSAGE = 'after_publishing'; /** * @var AMQPMessage @@ -29,6 +32,11 @@ class AMQPEvent extends Event */ protected $consumer; + /** + * @var Producer + */ + protected $producer; + /** * @return AMQPMessage */ @@ -68,4 +76,24 @@ public function setConsumer(Consumer $consumer) return $this; } + + /** + * @return Producer + */ + public function getProducer() + { + return $this->producer; + } + + /** + * @param Producer $producer + * + * @return AMQPEvent + */ + public function setProducer(Producer $producer) + { + $this->producer = $producer; + + return $this; + } } diff --git a/Event/AfterPublishingMessageEvent.php b/Event/AfterPublishingMessageEvent.php new file mode 100644 index 00000000..27ec9491 --- /dev/null +++ b/Event/AfterPublishingMessageEvent.php @@ -0,0 +1,28 @@ +setProducer($producer); + $this->setAMQPMessage($AMQPMessage); + } +} diff --git a/Event/BeforePublishingMessageEvent.php b/Event/BeforePublishingMessageEvent.php new file mode 100644 index 00000000..3810afdf --- /dev/null +++ b/Event/BeforePublishingMessageEvent.php @@ -0,0 +1,28 @@ +setProducer($producer); + $this->setAMQPMessage($AMQPMessage); + } +} diff --git a/README.md b/README.md index 0c750b2e..8793be69 100644 --- a/README.md +++ b/README.md @@ -261,6 +261,10 @@ If you need to use a custom class for a producer (which should inherit from `Old ... ``` +#### Producer Events #### + +TODO + The next piece of the puzzle is to have a consumer that will take the message out of the queue and process it accordingly. diff --git a/RabbitMq/Producer.php b/RabbitMq/Producer.php index 446bb611..67e6691b 100644 --- a/RabbitMq/Producer.php +++ b/RabbitMq/Producer.php @@ -2,6 +2,8 @@ namespace OldSound\RabbitMqBundle\RabbitMq; +use OldSound\RabbitMqBundle\Event\AfterPublishingMessageEvent; +use OldSound\RabbitMqBundle\Event\BeforePublishingMessageEvent; use PhpAmqpLib\Message\AMQPMessage; use PhpAmqpLib\Wire\AMQPTable; @@ -53,6 +55,7 @@ public function publish($msgBody, $routingKey = '', $additionalProperties = arra $msg->set('application_headers', $headersTable); } + $this->dispatchEvent(BeforePublishingMessageEvent::NAME, new BeforePublishingMessageEvent($this, $msg)); $this->getChannel()->basic_publish($msg, $this->exchangeOptions['name'], (string)$routingKey); $this->logger->debug('AMQP message published', array( 'amqp' => array( @@ -62,5 +65,6 @@ public function publish($msgBody, $routingKey = '', $additionalProperties = arra 'headers' => $headers ) )); + $this->dispatchEvent(AfterPublishingMessageEvent::NAME, new AfterPublishingMessageEvent($this, $msg)); } }