dispatch() public method

public dispatch ( mixed $event ) : void
$event mixed
return void
 /**
  * Publish recorded events on the event bus
  *
  * @param ActionEvent $actionEvent
  */
 public function onEventStoreCommitPost(ActionEvent $actionEvent)
 {
     $recordedEvents = $actionEvent->getParam('recordedEvents', new \ArrayIterator());
     foreach ($recordedEvents as $recordedEvent) {
         $this->eventBus->dispatch($recordedEvent);
     }
 }
 /**
  * @param ActionEvent $actionEvent
  */
 public function onEventStoreCommitPost(ActionEvent $actionEvent)
 {
     $this->queuedActionEvents[] = $actionEvent;
     if (!$this->inConfirmSelectMode) {
         $this->inConfirmSelectMode = true;
         while ($actionEvent = array_shift($this->queuedActionEvents)) {
             $fallback = new \ArrayIterator();
             $recordedEvents = $actionEvent->getParam('recordedEvents', $fallback);
             if ($fallback !== $recordedEvents) {
                 $this->producer->confirmSelect();
             }
             $countRecordedEvents = 0;
             foreach ($recordedEvents as $recordedEvent) {
                 $this->eventBus->dispatch($recordedEvent);
                 $countRecordedEvents++;
             }
             if ($fallback !== $recordedEvents) {
                 $this->producer->setConfirmCallback(function (int $deliveryTag, bool $multiple) use($countRecordedEvents) {
                     return $deliveryTag !== $countRecordedEvents;
                 }, function (int $deliveryTag, bool $multiple, bool $requeue) use(&$result) {
                     throw new RuntimeException('Could not publish all events');
                 });
                 $this->producer->waitForConfirm($this->timeout);
             }
         }
         $this->inConfirmSelectMode = false;
     }
 }
 /**
  * @param BernardMessage $message
  */
 public function routeMessage(BernardMessage $message)
 {
     $proophMessage = $message->getProophMessage();
     if ($proophMessage->messageType() === Message::TYPE_COMMAND) {
         $this->commandBus->dispatch($proophMessage);
     } else {
         $this->eventBus->dispatch($proophMessage);
     }
 }
Esempio n. 4
0
 /**
  * @interitdoc
  */
 public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next)
 {
     $eventName = $request->getAttribute(self::NAME_ATTRIBUTE);
     if (null === $eventName) {
         return $next($request, $response, new RuntimeException(sprintf('Event name attribute ("%s") was not found in request.', self::NAME_ATTRIBUTE), Middleware::STATUS_CODE_BAD_REQUEST));
     }
     try {
         $event = $this->eventFactory->createMessageFromArray($eventName, ['payload' => $request->getParsedBody(), 'metadata' => $this->metadataGatherer->getFromRequest($request)]);
         $this->eventBus->dispatch($event);
         return $response->withStatus(Middleware::STATUS_CODE_ACCEPTED);
     } catch (\Exception $e) {
         return $next($request, $response, new RuntimeException(sprintf('An error occurred during dispatching of event "%s"', $eventName), Middleware::STATUS_CODE_INTERNAL_SERVER_ERROR, $e));
     }
 }
 /**
  * @interitdoc
  */
 public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next)
 {
     $payload = null;
     $messageName = 'UNKNOWN';
     try {
         $payload = $request->getParsedBody();
         if (is_array($payload) && isset($payload['message_name'])) {
             $messageName = $payload['message_name'];
         }
         MessageDataAssertion::assert($payload);
         $message = $this->messageFactory->createMessageFromArray($payload['message_name'], $payload);
         switch ($message->messageType()) {
             case Message::TYPE_COMMAND:
                 $this->commandBus->dispatch($message);
                 return $response->withStatus(Middleware::STATUS_CODE_ACCEPTED);
             case Message::TYPE_EVENT:
                 $this->eventBus->dispatch($message);
                 return $response->withStatus(Middleware::STATUS_CODE_ACCEPTED);
             case Message::TYPE_QUERY:
                 return $this->responseStrategy->fromPromise($this->queryBus->dispatch($message));
             default:
                 return $next($request, $response, new RuntimeException(sprintf('Invalid message type "%s" for message "%s".', $message->messageType(), $messageName), Middleware::STATUS_CODE_BAD_REQUEST));
         }
     } catch (\Assert\InvalidArgumentException $e) {
         return $next($request, $response, new RuntimeException($e->getMessage(), Middleware::STATUS_CODE_BAD_REQUEST, $e));
     } catch (\Exception $e) {
         return $next($request, $response, new RuntimeException(sprintf('An error occurred during dispatching of message "%s"', $messageName), Middleware::STATUS_CODE_INTERNAL_SERVER_ERROR, $e));
     }
 }
 /**
  * Perform a message
  */
 public function perform()
 {
     $messageClass = $this->args['message_class'];
     /* @var $message \Prooph\Common\Messaging\RemoteMessage*/
     $message = $messageClass::fromArray($this->args['message_data']);
     if ($message->header()->type() === MessageHeader::TYPE_COMMAND) {
         try {
             $this->commandBus->dispatch($message);
         } catch (CommandDispatchException $ex) {
             throw $ex->getPrevious();
         }
     } else {
         try {
             $this->eventBus->dispatch($message);
         } catch (EventDispatchException $ex) {
             throw $ex->getPrevious();
         }
     }
 }
 /**
  * @param ActionEvent $actionEvent
  */
 public function onEventStoreCommitPost(ActionEvent $actionEvent)
 {
     $this->queuedActionEvents[] = $actionEvent;
     if (!$this->inTransaction) {
         $this->inTransaction = true;
         while ($actionEvent = array_shift($this->queuedActionEvents)) {
             $fallback = new \ArrayIterator();
             $recordedEvents = $actionEvent->getParam('recordedEvents', $fallback);
             if ($fallback !== $recordedEvents) {
                 $this->producer->startTransaction();
             }
             foreach ($recordedEvents as $recordedEvent) {
                 $this->eventBus->dispatch($recordedEvent);
             }
             if ($fallback !== $recordedEvents) {
                 $this->producer->commitTransaction();
             }
         }
         $this->inTransaction = false;
     }
 }
Esempio n. 8
0
 /**
  * @test
  */
 public function it_invokes_all_listeners()
 {
     $handler = new MessageHandler();
     $this->eventBus->getActionEventEmitter()->attachListener(MessageBus::EVENT_ROUTE, function (ActionEvent $e) use($handler) {
         if ($e->getParam(MessageBus::EVENT_PARAM_MESSAGE_NAME) === CustomMessage::class) {
             $e->setParam(EventBus::EVENT_PARAM_EVENT_LISTENERS, [$handler, $handler]);
         }
     });
     $customMessage = new CustomMessage("foo");
     $this->eventBus->dispatch($customMessage);
     $this->assertSame($customMessage, $handler->getLastMessage());
     $this->assertEquals(2, $handler->getInvokeCounter());
 }
 /**
  * @test
  */
 public function it_invokes_processing_event_on_workflow_message_handler()
 {
     $userData = array('id' => 1, 'name' => 'Alex', 'address' => array('street' => 'Main Street', 'streetNumber' => 10, 'zip' => '12345', 'city' => 'Test City'));
     $user = UserDictionary::fromNativeValue($userData);
     $wfEvent = WorkflowMessage::newDataCollected($user, 'test-case', NodeName::defaultName());
     $eventBus = new EventBus();
     $eventRouter = new EventRouter();
     $eventRouter->route($wfEvent->messageName())->to($this->workflowMessageHandler);
     $eventBus->utilize($eventRouter);
     $eventBus->utilize(new HandleWorkflowMessageInvokeStrategy());
     $eventBus->dispatch($wfEvent);
     $this->assertSame($wfEvent, $this->workflowMessageHandler->lastWorkflowMessage());
 }
 /**
  * @param Envelope $envelope
  * @param Queue $queue
  * @return DeliveryResult
  */
 public function __invoke(Envelope $envelope, Queue $queue) : DeliveryResult
 {
     $data = json_decode($envelope->getBody(), true);
     if (!isset($data['created_at'])) {
         return DeliveryResult::MSG_REJECT();
     }
     $data['created_at'] = DateTimeImmutable::createFromFormat('Y-m-d\\TH:i:s.u', $data['created_at'], new DateTimeZone('UTC'));
     if (false === $data['created_at']) {
         return DeliveryResult::MSG_REJECT();
     }
     try {
         $event = $this->messageFactory->createMessageFromArray($envelope->getType(), $data);
         $this->eventBus->dispatch($event);
     } catch (\Throwable $e) {
         while ($e = $e->getPrevious()) {
             if ($e instanceof ConcurrencyException) {
                 return DeliveryResult::MSG_REJECT_REQUEUE();
             }
         }
         return DeliveryResult::MSG_REJECT();
     }
     return DeliveryResult::MSG_ACK();
 }
 /**
  * @test
  */
 public function it_sends_a_sub_process_finished_event_via_message_dispatcher_to_a_handler()
 {
     $taskListPosition = TaskListPosition::at(TaskListId::linkWith(NodeName::defaultName(), ProcessId::generate()), 1);
     $wfMessage = $this->getUserDataCollectedTestMessage();
     $wfMessage->connectToProcessTask($taskListPosition);
     $logMessage = LogMessage::logDebugMsg("Just a fake event", $wfMessage);
     $subProcessFinished = SubProcessFinished::record(NodeName::defaultName(), $taskListPosition->taskListId()->processId(), true, $logMessage, $taskListPosition);
     $eventBus = new EventBus();
     $eventRouter = new EventRouter();
     $eventRouter->route(SubProcessFinished::MSG_NAME)->to($this->messageDispatcher);
     $eventBus->utilize($eventRouter);
     $eventBus->utilize(new ForwardToRemoteMessageDispatcherStrategy(new FromProcessingMessageTranslator()));
     $eventBus->dispatch($subProcessFinished);
     /** @var $receivedMessage SubProcessFinished */
     $receivedMessage = $this->receivedMessage;
     $this->assertInstanceOf(get_class($subProcessFinished), $receivedMessage);
     $this->assertTrue($taskListPosition->taskListId()->processId()->equals($receivedMessage->subProcessId()));
     $this->assertTrue($taskListPosition->equals($receivedMessage->parentTaskListPosition()));
     $this->assertTrue($subProcessFinished->uuid()->equals($receivedMessage->uuid()));
     $this->assertTrue($logMessage->uuid()->equals($receivedMessage->lastMessage()->uuid()));
     $this->assertEquals($logMessage->technicalMsg(), $receivedMessage->lastMessage()->technicalMsg());
     $this->assertEquals($subProcessFinished->createdAt()->format('Y-m-d H:i:s'), $receivedMessage->createdAt()->format('Y-m-d H:i:s'));
 }
Esempio n. 12
0
 /**
  * Dispatch a tick event
  *
  * @return array
  */
 public function tickAction()
 {
     $this->eventBus->dispatch(TickOccurred::record());
     return new JsonModel(['success' => true]);
 }
Esempio n. 13
0
 /**
  * @test
  */
 public function it_passes_log_message_to_running_process_using_an_event_bus_with_processor_utilities()
 {
     $wfMessage = $this->getUserDataCollectedTestMessage();
     $this->getTestWorkflowProcessor()->receiveMessage($wfMessage);
     $receivedMessage = $this->workflowMessageHandler->lastWorkflowMessage();
     $logMessage = LogMessage::logInfoDataProcessingStarted($receivedMessage);
     //Set up EventBus
     $eventBus = new EventBus();
     $eventBus->utilize(new SingleTargetMessageRouter($this->getTestWorkflowProcessor()));
     $eventBus->utilize(new WorkflowProcessorInvokeStrategy());
     //Publish LogMessage
     $eventBus->dispatch($logMessage);
     $this->assertNotNull($this->lastPostCommitEvent);
     $recordedEvents = $this->lastPostCommitEvent->getRecordedEvents();
     $eventNames = [];
     foreach ($recordedEvents as $recordedEvent) {
         $eventNames[] = $recordedEvent->messageName();
     }
     $expectedEventNames = ['Prooph\\Processing\\Processor\\Task\\Event\\LogMessageReceived'];
     $this->assertEquals($expectedEventNames, $eventNames);
 }
 public function publishChangesOf(ProcessingConfig $processingConfig)
 {
     foreach ($processingConfig->popRecordedEvents() as $recordedEvent) {
         $this->eventBus->dispatch($recordedEvent);
     }
 }