Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Symfony Messenger: Eine Einführung

Symfony Messenger: Eine Einführung

Eine der großen Neuerungen in Symfony 4.1 ist die Messenger-Komponente, die als experimentelles Feature zur Verfügung steht. In meinem Talk möchte ich die Komponente und ihre Funktionsweise an einem praktischen Beispiel vorstellen.

Denis Brumann

October 26, 2018
Tweet

More Decks by Denis Brumann

Other Decks in Programming

Transcript

  1. Denis Brumann
 [email protected]
 @DBRUMANN 9 1. Invite-Code prüfen 2. Neuen

    Benutzer anlegen 3. Invite-Code einlösen 4. Neue Invites erstellen 5. Benutzer benachrichtigen Registrierung
  2. Denis Brumann
 [email protected]
 @DBRUMANN 19 Registration
 Facade InvitationProvider::getOpenInvitation() SecurityController::register() UserCreator::create()

    InvitationRedeemer::redeem() InvitationGenerator::generateMultiple() InvitationNotifier::notifyInvitingUser()
  3. Denis Brumann
 [email protected]
 @DBRUMANN 23 <?php declare(strict_types = 1); namespace

    App\Message; class RegisterUser { public $inviteCode; public $email; public $plainPassword; }
  4. Denis Brumann
 [email protected]
 @DBRUMANN 28 InvitationProvider::getOpenInvitation() CreateUser
 Handler RedeemInvitation
 Handler

    GenerateInvitations
 Handler NotifyInviteOwner
 Handler SecurityController::register() Message Bus RegisterUser UserCreator
 create() InvitationRedeemer redeem() InvitationGenerator
 generateMultiple() InvitationNotifier
 notifyInvitingUser()
  5. Denis Brumann
 [email protected]
 @DBRUMANN 30 <?php namespace Symfony\Component\Messenger\Handler; /** *

    Marker interface for message handlers. * * @author Samuel Roze <[email protected]> * * @experimental in 4.1 */ interface MessageHandlerInterface { }
  6. Denis Brumann
 [email protected]
 @DBRUMANN 32 interface MessageSubscriberInterface extends MessageHandlerInterface {

    /** * yield FirstMessage::class => ['method' => ‘firstMessageMethod']; * yield SecondMessage::class => [ * 'method' => ‘secondMessageMethod', * 'priority' => 20, * ‘bus' => ‘my_bus_name', * ]; */ public static function getHandledMessages(): iterable; }
  7. Denis Brumann
 [email protected]
 @DBRUMANN 33 CreateUser
 Handler RedeemInvitation
 Handler GenerateInvitations


    Handler NotifyInviteOwner
 Handler SecurityController::register() Message Bus RegisterUser RedeemInvitation CreateInvitations NotifyInviteOwner
  8. Denis Brumann
 [email protected]
 @DBRUMANN 34 Das Verhalten des Message Bus

    befindet sich in einem sortierten Middleware-Stack Middleware
  9. Denis Brumann
 [email protected]
 @DBRUMANN 36 framework: messenger: buses: messenger.bus.default: middleware:

    - App\Middleware\MyMiddleware - App\Middleware\AnotherMiddleware # default_middleware: false Konfiguration
  10. Denis Brumann
 [email protected]
 @DBRUMANN # config/packages/messenger.yaml framework: messenger: transports:
 amqp:

    '%env(MESSENGER_TRANSPORT_DSN)%'
 routing:
 App\Message\CreateInvitations: amqp App\Message\InvitationRedeemed: amqp 43 amqp://guest:guest@localhost:5672/%2f/messages
  11. Denis Brumann
 [email protected]
 @DBRUMANN 45 [InvalidArgumentException] No transport supports the

    given DSN
 "amqp://guest:guest@shared_queue:5672/%2f/messages". AMQP-Extension installieren composer require serializer
  12. Denis Brumann
 [email protected]
 @DBRUMANN 47 bin/console messenger:consume-messages Description: Consumes messages

    Usage: messenger:consume-messages [options] [--] <receiver> Arguments: receiver Name of the receiver Options: -l, --limit=LIMIT Limit the number of received messages -m, --memory-limit=MEMORY-LIMIT The memory limit the worker can consume -t, --time-limit=TIME-LIMIT The time limit in seconds the worker can run
  13. Denis Brumann
 [email protected]
 @DBRUMANN 52 Messages enthalten nur skalare Daten


    Im Handler Entities erneut laden Ohne Transport beginnen (synchron) (dev) Messages einzeln konsumieren Good Practices