public function publish(array $message, array $options = []) { $message = new Message(time(), $message, []); $this->dispatcher->dispatch(Events::Message($this->name), new MessageEvent($this->name, $message)); $context = ['MessageId' => $message->getId()]; $this->log(200, 'Message received and dispatched on Sync Queue', $context); }
public function setUp() { $this->dispatcher = new EventDispatcher('UTF-8'); $listener = new RequestListener($this->dispatcher); $this->dispatcher->addListener(KernelEvents::REQUEST, [$listener, 'onKernelRequest']); $this->dispatcher->addListener(QPushEvents::Notification('ironmq-test'), [$this, 'IronMqOnNotificationReceived']); $this->dispatcher->addListener(QPushEvents::Notification('aws-test'), [$this, 'AwsOnNotificationReceived']); $this->kernel = $this->getMock('Symfony\\Component\\HttpKernel\\HttpKernelInterface'); }
private function pollQueue($registry, $name) { if (!$registry->has($name)) { return $this->output->writeln(sprintf("The [%s] queue you have specified does not exists!", $name)); } $dispatcher = $this->getContainer()->get('event_dispatcher'); $messages = $registry->get($name)->receive(); foreach ($messages as $message) { $messageEvent = new MessageEvent($name, $message); $dispatcher->dispatch(Events::Message($name), $messageEvent); } $msg = "<info>Finished polling %s Queue, %d messages fetched.</info>"; $this->output->writeln(sprintf($msg, $name, sizeof($messages))); return 0; }
/** * Handles Notifications sent from AWS SNS * * @param GetResponseEvent $event The Kernel Request's GetResponseEvent */ private function handleSnsNotifications(GetResponseEvent $event) { $notification = json_decode((string) $event->getRequest()->getContent(), true); $type = $event->getRequest()->headers->get('x-amz-sns-message-type'); $metadata = ['Type' => $notification['Type'], 'TopicArn' => $notification['TopicArn'], 'Timestamp' => $notification['Timestamp']]; if ($type === 'Notification') { // We put the queue name in the Subject field $queue = $notification['Subject']; $metadata['Subject'] = $queue; $notification = new Notification($notification['MessageId'], $notification['Message'], $metadata); $this->dispatcher->dispatch(Events::Notification($queue), new NotificationEvent($queue, NotificationEvent::TYPE_MESSAGE, $notification)); return "SNS Message Notification Received."; } // For subscription notifications, we need to parse the Queue from // the Topic ARN $arnParts = explode(':', $notification['TopicArn']); $last = end($arnParts); $queue = str_replace('qpush_', '', $last); // Get the token for the Subscription Confirmation $metadata['Token'] = $notification['Token']; $notification = new Notification($notification['MessageId'], $notification['Message'], $metadata); $this->dispatcher->dispatch(Events::Notification($queue), new NotificationEvent($queue, NotificationEvent::TYPE_SUBSCRIPTION, $notification)); return "SNS Subscription Confirmation Received."; }
public function testNotificationEvent() { $event = Events::Notification('test'); $this->assertEquals(sprintf('%s.%s', 'test', Events::ON_NOTIFICATION), $event); }
/** * Handles SNS Notifications * * For Subscription notifications, this method will automatically confirm * the Subscription request * * For Message notifications, this method polls the queue and dispatches * the `{queue}.message_received` event for each message retrieved * * @param NotificationEvent $event The Notification Event * @param string $eventName Name of the event * @param EventDispatcherInterface $dispatcher * @return bool|void */ public function onNotification(NotificationEvent $event, $eventName, EventDispatcherInterface $dispatcher) { if (NotificationEvent::TYPE_SUBSCRIPTION == $event->getType()) { $topicArn = $event->getNotification()->getMetadata()->get('TopicArn'); $token = $event->getNotification()->getMetadata()->get('Token'); $this->sns->confirmSubscription(['TopicArn' => $topicArn, 'Token' => $token]); $context = ['TopicArn' => $topicArn]; $this->log(200, "Subscription to SNS Confirmed", $context); return; } $messages = $this->receive(); foreach ($messages as $message) { $messageEvent = new MessageEvent($this->name, $message); $dispatcher->dispatch(Events::Message($this->name), $messageEvent); } }
public function testPublish() { $this->dispatcher->expects($this->once())->method('dispatch')->with(Events::Message($this->provider->getName()), new \PHPUnit_Framework_Constraint_IsInstanceOf('Uecode\\Bundle\\QPushBundle\\Event\\MessageEvent')); $this->provider->publish(['foo' => 'bar']); }
/** * Polls the Queue on Notification from IronMQ * * Dispatches the `{queue}.message_received` event * * @param NotificationEvent $event The Notification Event */ public function onNotification(NotificationEvent $event) { $message = new Message($event->getNotification()->getId(), $event->getNotification()->getBody(), $event->getNotification()->getMetadata()->toArray()); $this->log(200, "Message has been received from Push Notification.", ['message_id' => $event->getNotification()->getId()]); $messageEvent = new MessageEvent($this->name, $message); $event->getDispatcher()->dispatch(Events::Message($this->name), $messageEvent); }
/** * {@inheritdoc} */ public function receive(array $options = []) { $this->options = array_merge($this->options, $options); if (!empty($options['queues'])) { $results = $options['queues']; } else { $results = $this->repisotory->createQueryBuilder('o')->orderBy('o.id', $this->options['fifo_receive'] ? 'ASC' : 'DESC')->where('o.name = :name')->setParameter('name', $this->getNameWithPrefix())->setMaxResults($this->options['messages_to_receive'])->getQuery()->getResult(); } if (!count($results)) { $this->log(200, "No messages found in queue."); return array(); } $messages = array(); /** @var QueueMessageInterface $message */ foreach ($results as $message) { $message->setReceivedAt(new \DateTime()); $messages[] = new Message($message->getId(), $message->getBody(), array()); $this->log(200, "Message has been received.", ['message_id' => $message->getId()]); $this->eventDispatcher->dispatch(Events::Message($this->name), new MessageEvent($this->name, new Message($message->getId(), $message->getBody(), array()))); // recived then delete. $this->dispatcher->remove($message); } $this->dispatcher->flush(); return $messages; }