Getting Results from your Handler

Getting Results from your Handler

When a message is handled, the Symfony\Component\Messenger\Middleware\HandleMessageMiddleware adds a Symfony\Component\Messenger\Stamp\HandledStamp for each object that handled the message. You can use this to get the value returned by the handler(s):

  1. use Symfony\Component\Messenger\MessageBusInterface;
  2. use Symfony\Component\Messenger\Stamp\HandledStamp;
  3. $envelope = $messageBus->dispatch(SomeMessage());
  4. // get the value that was returned by the last message handler
  5. $handledStamp = $envelope->last(HandledStamp::class);
  6. $handledStamp->getResult();
  7. // or get info about all of handlers
  8. $handledStamps = $envelope->all(HandledStamp::class);

Working with Command & Query Buses

The Messenger component can be used in CQRS architectures where command & query buses are central pieces of the application. Read Martin Fowler’s article about CQRS to learn more and how to configure multiple buses.

As queries are usually synchronous and expected to be handled once, getting the result from the handler is a common need.

A Symfony\Component\Messenger\HandleTrait exists to get the result of the handler when processing synchronously. It also ensures that exactly one handler is registered. The HandleTrait can be used in any class that has a $messageBus property:

  1. // src/Action/ListItems.php
  2. namespace App\Action;
  3. use App\Message\ListItemsQuery;
  4. use App\MessageHandler\ListItemsQueryResult;
  5. use Symfony\Component\Messenger\HandleTrait;
  6. use Symfony\Component\Messenger\MessageBusInterface;
  7. class ListItems
  8. {
  9. use HandleTrait;
  10. public function __construct(MessageBusInterface $messageBus)
  11. {
  12. $this->messageBus = $messageBus;
  13. }
  14. public function __invoke()
  15. {
  16. $result = $this->query(new ListItemsQuery(/* ... */));
  17. // Do something with the result
  18. // ...
  19. }
  20. // Creating such a method is optional, but allows type-hinting the result
  21. private function query(ListItemsQuery $query): ListItemsQueryResult
  22. {
  23. return $this->handle($query);
  24. }
  25. }

Hence, you can use the trait to create command & query bus classes. For example, you could create a special QueryBus class and inject it wherever you need a query bus behavior instead of the MessageBusInterface:

  1. // src/MessageBus/QueryBus.php
  2. namespace App\MessageBus;
  3. use Symfony\Component\Messenger\Envelope;
  4. use Symfony\Component\Messenger\HandleTrait;
  5. use Symfony\Component\Messenger\MessageBusInterface;
  6. class QueryBus
  7. {
  8. use HandleTrait;
  9. public function __construct(MessageBusInterface $messageBus)
  10. {
  11. $this->messageBus = $messageBus;
  12. }
  13. /**
  14. * @param object|Envelope $query
  15. *
  16. * @return mixed The handler returned value
  17. */
  18. public function query($query)
  19. {
  20. return $this->handle($query);
  21. }
  22. }

This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.